Question

Is it possible to handle the error pages which defined in web.xml, as the global-exception-mappings in Struts 2 actions. For example, instead of

<error-page>
    <error-code>404</error-code>
    <location>/common/jsp/FilkeNotFine.jsp</location>
</error-page>

Define something like below:

<global-exception-mppings>
    <exception-mapping ErrorCode="404" result="internernal-error" /> 
</global-exception-mppings>

Are there any plugin or interceptor that can do that?

Was it helpful?

Solution

Ofcourse you can

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

<global-exception-mappings>
  <exception-mapping exception="java.lang.Exception" result="error"/>
</global-exception-mappings>

the interceptor is exception defined in the package struts-default and the part of the defaultStack.

OTHER TIPS

I searched and with my own knowledge, IMHO there's no way to do that. Struts2 is not the one throwing 404, it's the web-server. Struts2 will throw the exception.

RomanC's answer explains clear enough on how to handle exception, but what you're looking forward to, can't be met 100%.

Instead you can try something like this :

<error-page>
    <error-code>404</error-code>
    <location>/common/jsp/my404.action</location>
</error-page>

and then you can have my404 mapped to some action in your struts xml.

Although I'm not certain that it may work, I guess it's server dependent. IMHO dynamic resource can't be used in such cases since the server requires for that file to be physically present.

If that happens, you can simply create a file named my404.action in your web-root and then struts2 will handle the rest, if you understand what I'm talking about.

Not tested, but should work.

You must change first the devMode into false from your struts.xml so you can now use your 404 Page.

<constant name="struts.devMode" value="false" /> 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top