Hello all:

I find that the tag "<error-page>" could not be executed in my struts project.
It could not be forwarded to the "error.jsp" when I try to access a not exist page or a wrong url.Why?
The web.xml is like this:

<?xml version="1.0" encoding="UTF-8"?>
 
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
    version="2.4">
 
    <filter>
        <filter-name>Set Character Encoding</filter-name>
        <filter-class>com.youcompany.struts.util.SetCharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>Set Character Encoding</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
 
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
 
    <error-page>
        <error-code>404</error-code>
        <location>/error.jsp</location>
    </error-page>
 
    <error-page>
        <error-code>500</error-code>
        <location>/error.jsp</location>
    </error-page>
 
    <servlet>
        <servlet-name>action</servlet-name>
        <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
        <init-param>
            <param-name>config</param-name>
            <param-value>
                /WEB-INF/struts-config.xml, 
                /WEB-INF/struts-config-test.xml
            </param-value>
        </init-param>
        <init-param>
            <param-name>debug</param-name>
            <param-value>3</param-value>
        </init-param>
        <init-param>
            <param-name>detail</param-name>
            <param-value>3</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>action</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
</web-app>

I am facing the same problem, Please let me know if you found any solution.
Thanks,
Bhaskar

Well I have a workaround. I just add normal html pages as error pages BUT, in those particular html error pages, I do a redirect as follows:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>

    <META HTTP-EQUIV="Refresh" CONTENT="0;URL=../portal/unauthorized.action">
    
</head>

<body>
</body>
</html>

PAY ATTENTION: The ../blah blah/blah is important! because depending on the path where the user is coming from, it will work or it won't! So, either put all secured content in 1 and the same location to guarantee the path is always correct OR replace URL by:
URL=/context-root/action namespace/action.

I am talking about struts2 here.

The latter is NOT ideal because if the context-root is changed in your config, it won't work anymore! But I haven't found a better way yet. I will be lookin into the struts2 interceptors when I have time. I was also faced with the shit that when including jsp fragments in the error page mentioned in web.xml, I had nullPointerExceptions in the Struts2 tags!! I guess that's a nasty bug! Hope it will help you.


Cheers [email snipped]

Edited 6 Years Ago by Ezzaral: Added code tags and snipped email.

This article has been dead for over six months. Start a new discussion instead.