Struts2核心 攔截器(初識攔截器)———struts2第五講

攔截器 struts2 核心css

注:本文系做者在看了浪曦的風中葉老師的struts2視頻的我的總結,但願能幫助廣大struts2的初學者。 

第一步:(這一步和其餘同樣,這裏從簡)依舊是新建一個web project,命名爲interceptor1,導入struts2必須的包。在src目錄下新建struts.xml,修改web.xml文件。 

第二步:在前面幾講中已經對一個struts2的web project有了一個具體的說明 這裏只給代碼: 

目錄結構爲: 

 

web.xml 
html

Xml代碼  收藏代碼java

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

  2. <web-app version="2.4"   apache

  3.     xmlns="http://java.sun.com/xml/ns/j2ee"   瀏覽器

  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   tomcat

  5.     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   app

  6.     http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">  框架

  7.   

  8.     <filter>  jsp

  9.         <filter-name>struts2</filter-name>  

  10.         <!-- 控制器 -->  

  11.         <filter-class>  

  12.         org.apache.struts2.dispatcher.FilterDispatcher  

  13.         </filter-class>  

  14.     </filter>                                                                 

  15.                

  16.                

  17.              <filter-mapping>  

  18.                 <filter-name>struts2</filter-name>  

  19.                 <!-- 任何請求均有過濾器 -->  

  20.                 <url-pattern>/*</url-pattern>  

  21.              </filter-mapping>  

  22. </web-app>  


test.jsp 

Jsp代碼  收藏代碼

  1. <%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>  

  2. <%  

  3. String path = request.getContextPath();  

  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  

  5. %>  

  6. <%@ taglib prefix="s" uri="/struts-tags" %>  

  7.   

  8. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  

  9. <html>  

  10.   <head>  

  11.     <base href="<%=basePath%>">  

  12.       

  13.     <title>My JSP 'index.jsp' starting page</title>  

  14.     <meta http-equiv="pragma" content="no-cache">  

  15.     <meta http-equiv="cache-control" content="no-cache">  

  16.     <meta http-equiv="expires" content="0">      

  17.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  

  18.     <meta http-equiv="description" content="This is my page">  

  19.     <!--  

  20.     <link rel="stylesheet" type="text/css" href="styles.css">  

  21.     -->  

  22.   </head>  

  23.     

  24.   <body>  

  25.                 <s:form action="test_interceptor">  

  26.                           

  27.                       <s:submit name="submit"></s:submit>  

  28.                           

  29.                 </s:form>  

  30.   </body>  

  31. </html>  


success.jsp 

Jsp代碼  收藏代碼

  1. <%@ page language="java" import="java.util.*" pageEncoding="gbk"%>  

  2. <%  

  3. String path = request.getContextPath();  

  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  

  5. %>  

  6.   

  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  

  8. <html>  

  9.   <head>  

  10.     <base href="<%=basePath%>">  

  11.       

  12.     <title>My JSP 'success.jsp' starting page</title>  

  13.       

  14.     <meta http-equiv="pragma" content="no-cache">  

  15.     <meta http-equiv="cache-control" content="no-cache">  

  16.     <meta http-equiv="expires" content="0">      

  17.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  

  18.     <meta http-equiv="description" content="This is my page">  

  19.     <!--  

  20.     <link rel="stylesheet" type="text/css" href="styles.css">  

  21.     -->  

  22.   

  23.   </head>  

  24.     

  25.   <body>  

  26.   去看看控制檯吧  

  27.   </body>  

  28. </html>  



Test_InterceptorAction 

Java代碼  收藏代碼

  1. package cn.edu.hdu.action;  

  2.   

  3. import com.opensymphony.xwork2.ActionSupport;  

  4.   

  5. public class Test_InterceptorAction extends ActionSupport    

  6.   

  7. {  

  8.       

  9.     private String username;  

  10.   

  11.     public String getUsername() {  

  12.         return username;  

  13.     }  

  14.   

  15.     public void setUsername(String username) {  

  16.         this.username = username;  

  17.     }  

  18.       

  19.       

  20.     public String execute() throws Exception  

  21.     {  

  22.         return SUCCESS;  

  23.     }  

  24.       

  25.     public String test() throws Exception  

  26.     {  

  27.           

  28.         System.out.println("此時全部攔截器完畢,已經調用了action中的test方法");  

  29.         return SUCCESS;  

  30.     }  

  31.   

  32. }  



以上幾個文件基本沒有什麼花頭,接下去咱們寫本身定義的攔截器MyInterceptor.java 
如今src下建包 cn.edu.hdu.interceptor 在裏面建攔截器(說白了就是一個類) 
代碼以下:注意一些註釋: 

Java代碼  收藏代碼

  1. package cn.edu.hdu.interceptor;  

  2.   

  3. import com.opensymphony.xwork2.ActionInvocation;  

  4. import com.opensymphony.xwork2.interceptor.Interceptor;  

  5.   

  6. public class MyInterceptor implements Interceptor {  

  7.   

  8.     public void destroy() {  

  9.   

  10.         System.out.println("destroy");  

  11.     }  

  12.   

  13.     public void init() {                 

  14.   

  15.                        System.out.println("攔截器已經被加載");  

  16.     }  

  17.   

  18.     public String intercept(ActionInvocation invocation) throws Exception {  

  19.   

  20.                                      System.out.println("調用intercept方法");  

  21.                                        

  22.                                      String result = invocation.invoke();       // invocation.invoke()方法檢查是否還有攔截器 有的話繼續調用餘下的攔截器 沒有了  執行action的業務邏輯  

  23.         return result;  

  24.     }  

  25.   

  26. }  



這個類使咱們本身定義的。 

1.繼承interceptor接口中的三個方法 
public void init(); 
public void destroy(); 
public String interceptor(ActionInvocation invocation); 
2.在interceptor中調用invocation.invoke(); 當執行完當前攔截器。檢查是否還有攔截器須要存在。 

好了。攔截器寫好了。要怎樣將它插入action中去呢? 

struts.xml 
注意一些註釋 

Xml代碼  收藏代碼

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

  2. <!DOCTYPE struts PUBLIC  

  3.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  

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

  5.   

  6.   

  7. <struts>  

  8.         <package name="interceptor1" extends="struts-default">  

  9.                             

  10.                           <!-- 定義攔截器 -->  

  11.                           <interceptors>  

  12.                                 <interceptor name="myInterceptor" class="cn.edu.hdu.interceptor.MyInterceptor"></interceptor>  

  13.                           </interceptors>  

  14.                            

  15.                         <!-- 配置action -->  

  16.                         <action name="test_interceptor" calss="cn.edu.hdu.action.Test_InterceptorAction">  

  17.                             <result name="success">/success.jsp</result>  

  18.                             <result name="input">/test.jsp</result>  

  19.                             <!-- 將聲明好的攔截器插入action中 -->  

  20.                                 <interceptor-ref name="myInterceptor"></interceptor-ref>  

  21.                         </action>  

  22.                           

  23.                           

  24.         </package>  

  25. </struts>  



好了。如今來檢測一下吧: 

首先是啓動tomcat 
注意控制檯的信息,以下: 

 

看到中文那行了吧,說明什麼? 本身想咯…… 
瀏覽器中輸入: 
http://localhost:8080/interceptor1/test.jsp 
單擊submit 
發現控制檯有什麼? 
刷新試試看? 
再刷? 
再試試看? 
哈哈 

附件中有源碼

Struts2輸入校驗2(框架效驗)———stru ... | struts2 核心攔截器2 (微微進階)——stru ...

相關文章
相關標籤/搜索