struts2struts2-interceptors

Struts2 Global Result and global exception not working


I am trying to get the exception via struts2 to display the global results jsp but its not working and instead I am getting a java exception show in console. Also would like to add that If i use interceptor-ref exception individually to an action its working fine but globally not working as intended.

Here is my struts.xml, just addded a simple global results and exceptions.

    <?xml version="1.0" encoding="UTF-8"?>

    <!DOCTYPE struts PUBLIC
       "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
       "http://struts.apache.org/dtds/struts-2.0.dtd">

    <struts>
        <constant name="struts-devmode" value="true"></constant>

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

            <global-results>
                <result name="myresult">globalresult.jsp</result>
            </global-results>

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



            <action name="UserAction" class="actionclasses.UserAction"
                method="execute">
                <interceptor-ref name="timer"></interceptor-ref>
                <interceptor-ref name="params"></interceptor-ref>
                <result name="input">index.jsp</result>
                <result name="success">success.jsp</result>
            </action>

            <action name="LoginAction">
                <result type="redirect">login.jsp</result>
            </action>

        </package>

    </struts>

My UserAction class from where exception is raised.

    package actionclasses;

    import com.opensymphony.xwork2.ActionSupport;

    public class UserAction extends ActionSupport{

        /**
         * 
         */
        private static final long serialVersionUID = 1L;

        String userName;
        String passWord;

        public String execute()
        {

            System.out.println(userName);

            int a=10/0;


            return "success";
        }


        public String getUserName() {
            return userName;
        }

        public void setUserName(String userName) {
            this.userName = userName;
        }

        public String getPassWord() {
            return passWord;
        }

        public void setPassWord(String passWord) {
            this.passWord = passWord;
        }

        public static long getSerialversionuid() {
            return serialVersionUID;
        }


    }

My login.jsp, my login page

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@taglib prefix='s' uri='/struts-tags' %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>User Login</title>
</head>
<body>
<h1>Login to Web page</h1>
<s:form action="UserAction" method="post">
<s:textfield name="userName" label="Enter UserName" />
<s:password name="passWord" label="Enter Password" />
<s:submit value="submit" />
</s:form>
</body>
</html>

Solution

  • Once you specify any interceptors you must specify all interceptors.