struts2实现简单action时间拦截器

//jsp index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %>

<!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>
<a href="login.jsp">[登录]</a>
  </body>
</html>

//login.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">

    <title>My JSP 'login.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>
  <form action="users!execute" method="post">
  用户名:<input type="text" name="username"/>
      密码:<input type="password" name="password"/>
      <input type="submit" value="登录"/>&nbsp;&nbsp;
      <input type="reset" value="重置" />
  </form>
  </body>
</html>

//时间拦截器

package com.kero99.ygc.interceptor;

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



public class MyTimerInterceptor extends AbstractInterceptor{

    @Override
    public String intercept(ActionInvocation invocation) throws Exception {
        //执行action之前的工作时间,获取开始的时间.
        long startTime=System.currentTimeMillis();
        System.out.println("执行action之前的工作,开始时间:"+startTime);
        //执行后续拦截器或Action
        String result=invocation.invoke();
        //执行action之后的工作:计算并输出执行时间
        long endTime=System.currentTimeMillis();
        long execTime=endTime-startTime;
        System.out.println("执行Action之后的工作,结束时间:"+endTime+",执行耗时:"+execTime+"ms");
        return result;  
    }

}

//struts.xml

    <action name="users" class="com.kero99.ygc.javabean.UsersAction2" >

            <result name="success">
                /helloStruts.jsp
            </result>
                    <interceptor-ref name="defaultStack"/>
            <interceptor-ref name="myTimer"/>
        </action>   

//UsersAction2

package com.kero99.ygc.javabean;

import com.opensymphony.xwork2.ModelDriven;

public class UsersAction2 implements ModelDriven{
    private User user=new User();

    public User getModel() {

        return user;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }
    public String execute(){
        System.out.println("action2");
        return "success";
    }
}

你可能感兴趣的:(java,struts2.0)