Strutsグローバル例外処理---ブロッキング+リスナー構成


1、構成

<!--   -->
        <interceptors>
            <interceptor name="showException"
                class="com.yihaodian.pis.interceptor.ShowExceptionIterceptor">
            </interceptor>
            <!--   -->
            <interceptor-stack name="mydefault">
                <interceptor-ref name="defaultStack" />
                <interceptor-ref name="showException" />
            </interceptor-stack>
        </interceptors>
        <default-interceptor-ref name="mydefault" />
        <!--   -->
        <global-results>
            <!--  login , /login.jsp  -->
            <result name="login" type="freemarker">
                /WEB-INF/templates/admin/login.ftl
            </result>
            <result name="noSecurity" type="freemarker">
                /WEB-INF/templates/noSecurity.ftl
            </result>
            <result name="error" type="dispatcher">
                error.jsp
            </result>
        </global-results>
        <global-exception-mappings>
            <exception-mapping result="error" name="error"
                exception="com.yihaodian.pis.util.ShowException"></exception-mapping>
        </global-exception-mappings> 

2、カスタム例外:

package com.yihaodian.pis.util;

public class ShowException extends Exception{

    private static final long serialVersionUID = 0xc1a865c45ffdc5f9L;

    public ShowException(String frdMessage)

    {

        super(createFriendlyErrMsg(frdMessage));
       
    }

    public ShowException(Throwable throwable)

    {

        super(throwable);

    }

    public ShowException(Throwable throwable, String frdMessage)

    {

        super(throwable);

    }

    private static String createFriendlyErrMsg(String msgBody) {

        String prefixStr = " ,";

        String suffixStr = "  !";

        StringBuffer friendlyErrMsg = new StringBuffer("");

        friendlyErrMsg.append(prefixStr);

        friendlyErrMsg.append(msgBody);

        friendlyErrMsg.append(suffixStr);
        return friendlyErrMsg.toString();

    }

}
 
 

3、遮断器


 
public class ShowExceptionIterceptor extends  AbstractInterceptor{

    /*
     * (non-Javadoc)
     *
     * @see
     * com.opensymphony.xwork2.interceptor.AbstractInterceptor#intercept(com
     * .opensymphony.xwork2.ActionInvocation)
     */
    @Override
    public String intercept(ActionInvocation invocation) throws Exception {
       
        String result=null;
        ActionSupport actionSupport= (ActionSupport) invocation.getAction();
        try {

            invocation.addPreResultListener(new MyListener());
            System.out.println(" ");
            result=invocation.invoke();
            System.out.println(" ");
           
        }catch(Exception e){
            System.out.println(" "+e.getMessage());
            actionSupport.addActionError(e.getMessage());
            return Action.ERROR;
        }
        return result;
    }

}
 

4、ページ表示

<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<%@ include file="../../webpage/include/mytaglib.jsp"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<title>1 </title>
</head>
<body>
<h2> </h2>
<s:if test="hasActionErrors()">
<div>
<span>
<s:iterator value="actionErrors"><s:property/></s:iterator>
</span>
</div>
</s:if>
</body>
</html>
 
 

5、リスナー(有無可)

package com.yihaodian.pis.interceptor;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.PreResultListener;

public class MyListener implements PreResultListener{
    public void beforeResult(ActionInvocation invocation, String resultCode) {
        System.out.println(" :"+resultCode);
    }

}
 
 
わからないことがあれば、QQ 526151410に連絡して解決することができます...