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
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!