Struts2中拦截器的简单实现流程

jopen 8年前

Struts2中拦截器的简单实现流程

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>   <!-- 配置常量使用国际化设置 -->   <constant name="struts.custom.i18n.resources" value="mess" />   <!-- 设置该应用使用的解码集 -->   <constant name="struts.i18n.encoding" value="UTF-8" />   <package name="front" namespace="/" extends="struts-default">      <interceptors>     <!-- 配置拦截器,指向自定义的拦截器类interceptor.ActionInterceptorAbstract -->     <interceptor name="ActionInterceptorAbstract" class="interceptor.ActionInterceptorAbstract">      <!-- 默认的区分的属性赋值 -->      <param name="name">计时拦截器</param>     </interceptor>    </interceptors>        <!-- 配置hello.action指向的action类action.HelloWord -->    <action name="hello" class="action.HelloWordAction">     <!-- 配置在执行到result之前要执行的拦截器 -->     <!-- 通过 name="ActionInterceptorAbstract"进行匹配 -->     <interceptor-ref name="ActionInterceptorAbstract">      <!-- 为拦截器中的身份标识进行赋值 -->      <param name="name">hello计时</param>     </interceptor-ref>     <!-- 执行完毕拦截器后根据拦截器返回的结果选择执行result -->     <result>/index.jsp</result>      </action>   </package>      </struts>

自定义拦截器的配置

package interceptor;    import java.util.Date;    import com.opensymphony.xwork2.ActionInvocation;  import com.opensymphony.xwork2.interceptor.AbstractInterceptor;    //继承抽象的拦截器类  public class ActionInterceptorAbstract extends AbstractInterceptor {     /**    *     */   private static final long serialVersionUID = 1L;     // 定义身份区分标识,接收struts.xml中<param name="name">XXX</param>标签体的内容   private String name;     // 接收配置文件传入的数据   public void setName(String name) {    this.name = name;   }     @Override   public String intercept(ActionInvocation invocation) throws Exception {      // 记录执行action中excute方法前的时间    long start = System.currentTimeMillis();    System.out.println(name + "调用前:" + new Date());      // 调用excute方法    // 并接受excute返回的结果    String actionResult = invocation.invoke();      // 记录执行action中excute方法后的时间    long end = System.currentTimeMillis();      System.out.println(name + "调用后:" + new Date());      System.out.println(name + "执行用时:" + (end - start));      // 将excute执行结果返回给配置文件的result    return actionResult;   }    }

action的配置

package action;    import com.opensymphony.xwork2.ActionSupport;    public class HelloWordAction extends ActionSupport {     //定义一个字符串用例验证   private String message;     public String getMessage() {    return message;   }     public void setMessage(String message) {    this.message = message;   }     @Override   public String execute() throws Exception {    //赋值操作    message = "Helloworld";    System.out.println(message);    return SUCCESS;   }    }

index.jsp页面显示的配置

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  <%   String path = request.getContextPath();   String basePath = request.getScheme() + "://"     + request.getServerName() + ":" + request.getServerPort()     + path + "/";  %>  <%@taglib prefix="s" uri="/struts-tags"%>  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  <html>  <head>  <base href="<%=basePath%>">    <title>My JSP 'index.jsp' starting page</title>  <meta http-equiv="pragma" content="no-cache">  <meta http-equiv="cache-control" content="no-cache">  <meta http-equiv="expires" content="0">  <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  <meta http-equiv="description" content="This is my page">  <!--   <link rel="stylesheet" type="text/css" href="styles.css">   -->  </head>    <body>   <!-- 从域中取出message -->   ${message}  </body>  </html>

来自:http://blog.csdn.net/dong_martin/article/details/20285739