struts2自定义Interceptor拦截器
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html> 4 <html> 5 <head> 6 <meta charset="UTF-8"> 7 <title>struts2的一个例子</title> 8 </head> 9 <body> 10 <a href="${pageContext.request.contextPath }/hello">hello</a><br/> 11 12 </body> 13 </html>
1 package com.xiaostudy.web; 2 3 import com.opensymphony.xwork2.ActionSupport; 4 5 public class Hello extends ActionSupport { 6 7 public String print() { 8 System.out.println("Hello类的print方法执行了。。。。"); 9 return SUCCESS; 10 } 11 }
1 package com.xiaostudy.web; 2 3 import com.opensymphony.xwork2.ActionInvocation; 4 import com.opensymphony.xwork2.interceptor.AbstractInterceptor; 5 6 public class Demo1_Interceptor extends AbstractInterceptor { 7 8 @Override 9 public String intercept(ActionInvocation invocation) throws Exception { 10 System.out.println("Demo1_Interceptor调用之前>>>>>>>"); 11 String str = invocation.invoke();//放行 12 System.out.println(str); 13 System.out.println("Demo1_Interceptor调用之后<<<<<<<<"); 14 return str; 15 } 16 17 }
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 4 "http://struts.apache.org/dtds/struts-2.3.dtd"> 5 <struts> 6 <constant name="struts.devMode" value="true"/> 7 8 <package name="hello" extends="struts-default"> 9 <interceptors><!-- 自定义拦截器声明 注意:name里面不能有下划线_ --> 10 <interceptor name="demo1Interptor" class="com.xiaostudy.web.Demo1_Interceptor"></interceptor> 11 </interceptors> 12 <action name="hello" class="com.xiaostudy.web.Hello" method="print"> 13 <interceptor-ref name="demo1Interptor"></interceptor-ref><!-- 使用自定义拦截器 --> 14 <result name="success" >/ok.jsp</result> 15 <result name="input">/err.jsp</result> 16 </action> 17 </package> 18 </struts>