简体   繁体   中英

How to Implement custom 404 page in Struts2

I am learning struts2 app development, appreciate some help implementing custom 404 page. In my application,

web.xml has the following:

<error-page>
    <error-code>404</error-code>
    <location>/jsp/pub/error404.jsp</location>
</error-page>

struts.xml has :

    <package name="actions-default" extends="struts-default">

    <global-results>
        <result name="exception">/jsp/pub/exception.jsp</result>
    </global-results>

    <global-exception-mappings>
        <exception-mapping exception="java.lang.Exception"
            result="exception" />
        <exception-mapping exception="java.lang.RuntimeException"
            result="exception" />
    </global-exception-mappings>

</package>

Also, in struts.xml devMode set to false

<constant name="struts.devMode" value="false" />

error404.jsp : Please click here to see the jsp source at jsfiddle

Problem:

When I hit application with an invalid url or unmapped action, I am seeing the following message instead of my custom 404 page

"HTTP Status 404 - There is no Action mapped for namespace [/] and action name [invalidurl] associated with context path"

At the application console, I see the following:

SEVERE: Exception Processing ErrorPage[errorCode=404, location=/jsp/pub/error404.jsp] org.apache.jasper.JasperException: An exception occurred processing JSP page /jsp/pub/error404.jsp at line 6

6: <s:url value='/' var="contextPrefix" />

If I remove all of the 's' tags and make error404.jsp as plain jsp page, then I can see custom 404 page. Where I am doing wrong? Any help to fix this would be of great help.

Thanks in advance

S

Most S2 JSP tags expect to be run in the context of an S2 request. Directly referring to a JSP bypasses the S2 request process, so there's no valuestack, so most tags break.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM