Struts2全局结果和全局异常不起作用

时间:2018-12-12 01:56:38

标签: struts2 struts2-interceptors

我试图通过struts2获取异常,以显示全局结果jsp,但无法正常工作,而是在控制台中获取了Java异常显示。还想补充一点,如果我对某项操作分别使用interceptor-ref异常,则它可以正常工作,但在全局上无法按预期工作。

这是我的struts.xml,只是添加了一个简单的全局结果和异常。

    <?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>

我的UserAction类,引发异常的地方。

    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;
        }


    }

我的login.jsp,我的登录页面

    <%@ 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>

1 个答案:

答案 0 :(得分:1)

一旦指定了任何拦截器,就必须指定 all 拦截器。