Struts2中攔截器的簡單實現

scalad發表於2015-05-10
      Struts2的攔截器和Servlet過濾器類似。在執行Action的execute方法之前,Struts2會首先執行struts.xml中引用
的攔截器,在執行完所有引用的攔截器的intercept方法後,會執行Action的execute方法。在Struts2的攔截器體系中,
Struts2的內建攔截器完成了該框架的大部分操作,所以實際的開發過程中通常都是使用系統的攔截器而已。當然我們也可以開發自己的攔截器,類似於過濾器的開發。
     原理:
     Struts2攔截器的實現原理相對簡單,當請求Struts2的Action時,Struts2會查詢配置檔案,並根據配置例項化相對
的攔截器物件,然後串成一個列表,最後一個一個的呼叫列表中的攔截器。
     實現使用者自定義攔截器的過程:
     1.自定義攔截器類,實現com.opensymphony.xwork2.interceptor.Interceptor介面或繼承com.opensymphony.xwork2.
     interceptor.AbstractInterceptor類。
     2.在struts.xml中定義自定義的攔截器。
     3.在struts.xml的Action中使用攔截器。
示例:
helloAction.java該例項中的Action類,對錶單資料進行檢驗
package com.action;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;

public class HelloAction extends ActionSupport {
    private String username;
    private String pass1;
    private String pass2;
    
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPass1() {
return pass1;
}
public void setPass1(String pass1) {
this.pass1 = pass1;
}
public String getPass2() {
return pass2;
}
public void setPass2(String pass2) {
this.pass2 = pass2;
}
public String execute() throws Exception {
if(username!=null&&getPass1().equals(getPass2())&&!getUsername().trim().equals("")){
System.out.println("正在執行action...");
return Action.SUCCESS;
}
else{
System.out.println("正在執行action...");
return Action.INPUT;
}
}
  
}

HelloInterceptor.java是攔截器類
package com.inter;

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

public class HelloInterceptor extends AbstractInterceptor {
private static final long serialVersionUID = 1L;
@Override
public String intercept(ActionInvocation arg0) throws Exception {
System.out.println("the interceptor is running...");
String result = arg0.invoke();
System.out.println("the interceptor is ending...");
return result;
}
}
使用者自定義的攔截器必須實現interceptor介面或者直接繼承AbstractInterceptor。intercept方法的主要作用是實現攔截的動作
,引數ActionInvocation是攔截Action的一個引用,可以通過呼叫該引數的invoke方法,將控制權傳遞給下一個攔截器或Action的execute方法。


在struts.xml配置:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
   <package name="hellpinterceptor" extends="struts-default">
      <interceptors>
         <interceptor name="helloInterceptor" class="com.inter.HelloInterceptor"></interceptor>
      </interceptors>
      <action name="helloaction" class="com.action.HelloAction">
         <result name="success">/success.jsp</result>
         <result name="input">/reg.jsp</result>
         <interceptor-ref name="defaultStack"></interceptor-ref>
         <interceptor-ref name="helloInterceptor"></interceptor-ref>
      </action>
   </package>
</struts>    
所需要的攔截器被配置在interceptor標記中,interceptor標記用來配置一個具體的攔截器,HelloInterceptor就配置在這裡。攔截器的使用格式在每一個具體 
的Action中,在Action標記中的interceptor-ref標記的使用寂靜在interceptor標記中定義好的攔截器。這就是strtus.xml檔案。


reg.jsp程式碼:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix = "s"  uri ="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>struts2 攔截器應用</title>
</head>
<body>
<h1>使用者註冊</h1>
<s:form id="id" action="helloaction">
<s:textfield name="username" label="使用者名稱"></s:textfield>
<s:password name="pass1" label="密碼"></s:password>
<s:password name="pass2" label="重複密碼"></s:password>
<s:submit value="註冊"></s:submit>
</s:form>
</body>
</html>

success.jsp程式碼:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>struts2 攔截器應用</title>
  </head>
  <body>
<h2>使用者名稱:<s:property value="username"/></h2>
<h2>密碼:<s:property value="pass1"/></h2>
  </body>
</html>



執行的部分資訊如下:
資訊: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
the interceptor is running...
正在執行action...

the interceptor is ending...


攔截器中實現ntercept方法時,可以獲得ActionInvocation引數,其作用是獲取被攔截的Action例項,一旦獲得了該例項,即可實現將HTTP請求中的引數解析出來,並設定成Action的屬性,也可以直接將HTTP請求中的HttpServletRequest例項和HttpServletResponse例項傳輸給Action。

相關文章