struts核心拦截器(5)

第一步:(这一步和其他一样,这里从简)依旧是新建一个web project,命名为interceptor1,导入struts2必须的包。在src目录下新建struts.xml,修改web.xml文件。 

第二步:在前面几讲中已经对一个struts2的web project有了一个具体的说明 这里只给代码: 

目录结构为: 

 

web.xml 
Xml代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.4"   
  3.     xmlns="http://java.sun.com/xml/ns/j2ee"   
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  5.     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   
  6.     http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">  
  7.   
  8.     <filter>  
  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 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

沙漏无语

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值