Search code examples
javajspstruts2error-handlingactionresult

Struts 2 what to do when user inserts action which does not exist


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 NullpointerExceptions caused by one of your existing actions, not one that doesn't exist).


Solution

  • 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.

    Documentation