How to deal with 404 in struts2?

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.