程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> JSP編程 >> 關於JSP >> struts2 攔截器

struts2 攔截器

編輯:關於JSP

一、如何獲取異常信息的String類型信息

(1)java類的寫法

  try {

      result = invocation.invoke();

} catch (Exception e) {

StringWriter sw=new StringWriter();

     PrintWriter pw=new PrintWriter(sw);

     e.printStackTrace(pw);

     String exceptionMessage = sw.toString();

}

(2)jsp頁面如何打印異常信息

<%@ pagelanguage="java" contentType="text/html;charset=UTF-8"

    pageEncoding="UTF-8"%>

<%@ page isErrorPage="true"import="java.io.*"%>

<%@ taglib uri="http://java.sun.com/jsp/jstl/core"prefix="c"%>

<%=exception.getMessage()%><br>

<%=exception.getLocalizedMessage()%>

<%

StringWritersw=new StringWriter();

PrintWriterpw=new PrintWriter(sw);

exception.printStackTrace(pw);

response.setStatus(500);

out.print(sw);

%>

二、C標簽if語句的寫法

<%if (exception !=null ){%>

  <%=exception.getMessage()%><br>

<% }else {%>

               error!

<% }%>

三、設置全局錯誤頁面

步驟一、web.xml配置

<error-page>

                 <error-code>500</error-code>

                 <location>/error.jsp</location>

       </error-page>

步驟二、編寫錯誤頁面

<%@ page language="java" contentType="text/html;charset=UTF-8"

    pageEncoding="UTF-8"%>

<%@ pageisErrorPage="true" import="java.io.*"%> 

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<%=exception.getMessage()%><br>

<%=exception.getLocalizedMessage()%>

<%

StringWriter sw=new StringWriter();

PrintWriter pw=new PrintWriter(sw);

exception.printStackTrace(pw);

response.setStatus(500);

out.print(sw);

%>

四、如何為div賦值

    <div id="urlDetailWindow" class="easyui-dialog" title="訪問路徑詳情"  style="width:500px;height:200px;padding:10px" closed="true"shadow="true" modal="true">

          <div id="urlDetailIdDiv"></div>

</div>

function urlDetail(){

                 var row =$('#datalist').datagrid('getSelected');

                    if(row){

                        $("#urlDetailIdDiv").text(row["url"]);

                        $("#urlDetailWindow").window('open');

                    }

}

或者

document.getElementById("urlDetailIdDiv ").innerHTML=" aaaaa ";   

五、自定義攔截器的使用

(1)struts.xml配置

<?xmlversion="1.0" encoding="UTF-8" ?>

<!DOCTYPEstruts PUBLIC

    "-//Apache Software Foundation//DTDStruts Configuration 2.0//EN"

   "http://struts.apache.org/dtds/struts-2.0.dtd">

 

<struts>

       <constantname="struts.ognl.allowStaticMethodAccess" value="true" />

       <constantname="struts.multipart.maxSize" value="100000000"/>

 

       <package name="default" extends="struts-default" namespace="/">

          <interceptors>

<interceptor name="exceptionInterceptor" class="com.qiyi.cms.web.interceptor.ExceptionInterceptor">

                 <paramname="includeMethods">execute</param>

</interceptor>

 

<interceptorname="loginInterceptor" class="com.qiyi.cms.web.interceptor.LoginInterceptor"/>

             <interceptor-stack name="cmsDefaultStack">

                <interceptor-ref name="loginInterceptor"/>

                <interceptor-ref name="defaultStack"/>

            </interceptor-stack>

         </interceptors>

 

        <default-interceptor-ref name="cmsDefaultStack"></default-interceptor-ref>

 

        <global-results>

          <result name="login" type="redirectAction">index</result>

       </global-results>

 

<action name="compile" class="com.qiyi.cms.web.action.CompileAction">

            <result name="exec">/WEB-INF/generate/${jspFileName}</result>

            <interceptor-ref name="cmsDefaultStack"/>

            <interceptor-ref name="exceptionInterceptor"/>

        </action>

</package>

           

</struts>

 

(2)編寫自定義攔截器類

   publicclassExceptionInterceptor extends MethodFilterInterceptor{

       protected Log log =LogFactory.getLog(this.getClass());

       /**

        * @FieldsserialVersionUID : TODO(用一句話描述這個變量表示什麼)

        */

       privatestaticfinallongserialVersionUID = -6071035934377365730L;

      

    @Override

    public StringdoIntercept(ActionInvocation invocation) throwsException {

        String result;

        try {

            result = invocation.invoke();

        } catch(Exception e) {

//對捕獲到的異常進行處理,例如將異常存入庫中(上述一)

//處理完後可將異常拋出,通過設置的全局錯誤頁面(上述三),將異常捕獲到顯示到頁面

          thrownewException(e.getMessage());

        }

        returnresult;

}

}

(3)被攔截的action方法

publicclass CompileActionextends BaseAction {

       @Override

       public Stringexecute() throws Exception {

//邏輯代碼,有異常的時候拋出

System.out.print("execute 方法");

return"exec";

       }

 

}

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved