In a Struts 2 application when user inserts an URL which is not related with any of your actions a java.lang.NullPointerException
arises
in these cases I want to display a nice screen so I have added in my struts.xml
<global-exception-mappings>
<exception-mapping exception="java.lang.NullPointerException" result="exception"/>
</global-exception-mappings>
<global-results>
<result name="exception">/WEB-INF/jsp/exception.jsp</result>
</global-results>
but it doesn't work. I also have tried without success
<action name="exception">
<result>/WEB-INF/jsp/exception.jsp</result>
</action>
so, I'm starting to think that all URLs have to correspond with an action, otherwise there is no mechanism to handle these cases when users insert URLs which don't correspond to any action (so this mechanism is to handle NullpointerException
s caused by one of your existing actions, not one that doesn't exist).
Here you go :
<default-action-ref name="index"/>
This action will be called if no match is found.
I use this in all my apps to avoid unwanted errors/warnings in the logs.
In fact exceptions are caught by the exception
interceptor which is the first interceptor in the defaultStack
that is allowing to handle exceptions occurred not only in action but also during interceptors invocations.
For this purpose you have already defined a configuration.
If you enter an URL that isn't mapped to any action configuration usually a dispatcher returns 404 error code. If you get a NullPointerException
prior that result then it's something wrong with the project configuration.
It's difficult to tell what's going on without stacktrace. But if you want to handle cases where unknown URL is issued instead of dispatcher that handles it before, then you should look at this answer that could help you to define unknownHandler
.
Read the note about supported version of Struts that has this feature.
来源:https://stackoverflow.com/questions/20445940/struts-2-what-to-do-when-user-inserts-action-which-does-not-exist