Tag: struts2
Currently, we are working on a network application, struts2 + spring + hibernate, and the server is tomcat. It is expected that all undefined URLs can be captured by the program when the IE address bar is randomly typed, and then transferred to a self-made 404 error handling page.
First, check the information on the Internet and find that you can add such a section in Web. xml:
<Error-page>
<Error-code> 404 </error-code>
<Location>/error/404.jsp</location>
</Error-page>
In this way, all unfound 404 errors will be forwarded to/error/404. jsp for processing.
However, after testing, I found that the URL Ending with. action will be first captured by the struts2 framework, rather than directly transferred to the processing page configured in Web. xml. In this way, if the corresponding action is not configured in struts. XML, an error will be output in Tomcat:
Severe: cocould not find action or result
There is no Action mapped for namespace/and action name ******.-[unknown location]
Although it can be transferred to the Web. the processing page configured in XML is output in the command station, which is very inconvenient for program management and debugging. Therefore, you have to find a way for the struts2 framework to process undefined actions.
Check the online materials and find that you can use struts. in XML, add a default package, and then add a default action to the default package to redirect the action to the 404 error handling page:
<Package name = "default" extends = "struts-Default">
<Default-action-ref name = "notfound"/>
<Action name = "notfound">
<Result>/error/404.jsp</result>
</Action>
</Package>
The default package does not define the namespace attribute, so that all undefined namespaces will be switched here. Name = "default" is used to facilitate reading. In fact, name can be anything, or it can be blank: Name = "".
Then add the default action in other defined packages to go to The 404 error handling page. In this way, all the 404 errors can be completely handled.