Domanda

I am working on a Struts2 project which encounter HTTP errors like 404: page not found or 500: Internal server Error. I just want to handle all HTTP errors in Struts 2 programmatically without configuration.

Perhaps I would detect HTTP error code in filter or interceptor and then forward request to another page which especially design to display proper messages. How can I design such a program?

È stato utile?

Soluzione

Take a look at the Struts2 Exception Handling Wiki.

You can define global results for specific Exceptions.

<global-results>
 <result name="securityerror">/securityerror.jsp</result>
 <result name="error">/error.jsp</result>
</global-results>

<global-exception-mappings>
 <exception-mapping exception="org.apache.struts.register.exceptions.SecurityBreachException" result="securityerror" />
  <exception-mapping exception="java.lang.Exception" result="error" />
</global-exception-mappings>

Altri suggerimenti

you can handle the such HTTP errors in the web.xml , not the struts framework like the following :

<web-app>
  ...
  <error-page>
    <error-code>404</error-code>
    <location>/error404page.jsp</location>
  </error-page>

</web-app>

When system hit the 404 error, it will forward to your custom 404 error page “/error404pag.jsp“.

Hope that Helps .

These are not errors, just response codes. I don't know how you manage the connection, but the plain http connection works this way:

   HttpURLConnection httpConnection = (HttpURLConnection) connection;
   int code = httpConnection.getResponseCode();

In this class are missing some codes.

Since Java 6 javax.servlet.http.HttpServletResponse has the response codes. Also in Response.Status you have the response codes in a very readable way.

In your application - up tou the response code you can redirect where you want. How will you get the response code in an interceptor - I don't know, I'm not on my work computer to test some code. But there is a class that could help you - org.apache.struts2.dispatcher.HttpHeaderResult

Hope this helps.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top