我照着Max的例子做了struts2拦截器的测试,测试的是timer拦截器,
java代码:
struts.xml
Timer.jsp代码,按照max文章中说的这个随便写
按照文章中访问:http://localhost:8080/Struts2/Timer.action,正常显示,而控制台这边应该显示调用这个action所用的时间,但是什么都没显示。
我的struts2 版本是 2.0.11.1,jdk 1.6_u5,j2ee 1.4库,myeclipse 5.5+eclipse 3.2开发环境
java代码:
//2008-5-12 下午07:08:14 by fornever
package com.meteora.myInterceptor;
import com.opensymphony.xwork2.ActionSupport;
public class Timer extends ActionSupport {
private static final String SUCCESS = "success";
public String execute(){
try {
Thread.sleep(1000);// 模拟耗时的操作
} catch (InterruptedException e) {
e.printStackTrace();
}
return SUCCESS;
}
}
struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- 该文件主要放置Struts 2 的Action定义 -->
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<!-- struts 是 Struts 2 配置文件的根元素 -->
<struts>
<!-- Struts 2 的 Action 必须放在指定的包空间下定义 -->
<package name="Struts2" extends="struts-default">
<!-- 定义login的Action,指定类为com.meteora.login.LoginAction类 -->
<action name="Login" class="com.meteora.login.LoginAction">
<result name="success">/success.jsp</result>
<result name="failure">/failure.jsp</result>
<result name="input">/login.jsp</result>
</action>
<action name="GetBooks"
class="com.meteora.login.GetBooksAction">
<result name="success">/showBooks.jsp</result>
<result name="login">/empty.jsp</result>
</action>
<action name="Fileupload"
class="com.meteora.fileupload.FileuploadAction">
<interceptor-ref name="fileUpload"></interceptor-ref>
<result name="success">/ShowUpload.jsp</result>
</action>
</package>
<package name="myInterceptor" extends="struts-default">
<interceptors>
<!-- 定义一个拦截器 -->
<interceptor name="auth" class="com.meteora.myInterceptor.AuthorizationInterceptor" />
</interceptors>
<action name="RoleLogin" class="com.meteora.myInterceptor.Login">
<result type="chain">AuthorizationAccess</result>
</action>
<action name="AuthorizationAccess" class="com.meteora.myInterceptor.AuthorizationAccess">
<interceptor-ref name="auth" /><!-- 引用上面定义的拦截器 -->
<result name="login">/myInterceptor/Login.jsp</result>
<result name="success">/myInterceptor/ShowUser.jsp</result>
</action>
<action name="Timer" class="com.meteora.myInterceptor.Timer">
<interceptor-ref name="completeStack"/>
<interceptor-ref name="timer">
</interceptor-ref>
<result name="success">/myInterceptor/Timer.jsp</result>
<!-- 如果为Action指定了一个拦截器,则系统默认的拦截器栈将会失去作用。
为了继续使用默认拦截器,所以上面配置文件中手动引入了默认拦截器。 -->
</action>
</package>
</struts>
Timer.jsp代码,按照max文章中说的这个随便写
<%@ page language="java" contentType="text/html;charset=utf-8" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>授权用户</title>
</head>
<body>
<h1>你的角色是:<s:property value="role"/></h1>
</body>
</html>
按照文章中访问:http://localhost:8080/Struts2/Timer.action,正常显示,而控制台这边应该显示调用这个action所用的时间,但是什么都没显示。
我的struts2 版本是 2.0.11.1,jdk 1.6_u5,j2ee 1.4库,myeclipse 5.5+eclipse 3.2开发环境