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

ε祈祈猫儿з 提交于 2020-01-14 02:57:09

问题


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


回答1:


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




回答2:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!