web153

----

为Action对象配置输出执行时间的拦截器,查看执行Action所需要的时间。

----

1、配置Struts2开发环境

2、创建类TestAction

package dog;

import com.opensymphony.xwork2.ActionSupport;

public class TestAction extends ActionSupport{
    private static final long serialVersionUID=1L;
    public String execute() throws Exception{
        //线程睡眠1s
        Thread.sleep(1000);
        return SUCCESS;
    }
}

3、配置struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
    <!-- 开发模式打开 -->
    <constant name="struts.devMdoe" value="true"/>
    <!-- 声明常量 (在Struts2的配置文件修改后,自动加载)-->
    <constant name="struts.configuration.xml.reload" value="true"/>
    <!-- 声明包 -->
    <package name="myPackage" extends="struts-default">
        <!-- 定义action -->
        <action name="testAction" class="dog.TestAction">
            <!-- 配置拦截器 -->
            <interceptor-ref name="timer"/>
            <!-- 定义成功的映射页面 -->
            <result>success.jsp</result>
        </action>
    </package> 
</struts>

4、创建success.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 'success.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>
    this is timer?
  </body>
</html>

 

修改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="testAction.action">定时器</a>
  </body>
</html>

5、部署,访问:http://localhost:8080/jwrm153-Intercepter/

点击“定时器”

第一次访问TestAction时,需要进行一些初始化的操作,在以后的访问中就可以看到执行时间变成1000ms。

----

完毕

 
posted @ 2016-07-28 21:39  摘丶星  阅读(272)  评论(0编辑  收藏  举报
我要啦免费统计