1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
package csg.struts2.action; /** * * @author 小夜的傳說 * @2014-4-18 * @struts * @csg.struts2.action * @StrutsAction * @2014-4-18上午1:03:37 */ public class StrutsAction { private String message; public String getMessage() { return message; } public void setMessage(String message) { this .message = message; } public String execute() { message = "execute方法" ; return "success" ; } public String addUI() { message = "addUI方法" ; return "success" ; } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> < html > < head > < title >攔截器</ 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" > </ head > < body > <% request.getSession().setAttribute("user", "login"); %> 用戶已登錄 </ body > </ html > |
1
2
3
4
5
6
7
8
9
10
11
12
|
<? 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 > < package name = "struts2" namespace = "/test" extends = "struts-default" > <!-- 通配符形式訪問action --> < action name = "list_*" class = "csg.struts2.action.StrutsAction" method = "{1}" > < result name = "success" >/WEB-INF/page/success.jsp</ result > </ action > </ package > </ struts > |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
package csg.struts2.interceptor; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.Interceptor; /** * * @author 小夜的傳說 * @2014-4-18 * @struts * @csg.struts2.interceptor * @LoginInterceptor * @2014-4-18上午1:03:29 */ public class LoginInterceptor implements Interceptor { // 方法在攔截器被垃圾回收之前調用,用來回收init方法初始化的資源 public void destroy() { } // 方法在攔截器被創建之後,在對Action鏡像攔截之前調用,使用這個方法主要是給攔截器做類似初始化的操作 public void init() { } public String intercept(ActionInvocation invocation) throws Exception { Object user = ActionContext.getContext().getSession().get( "user" ); if (user != null ) { // 如果user不爲null,代表用戶已經登錄,允許執行action中的方法 這個結果就代表返回驗證通過 return invocation.invoke(); } else { //提示用戶登錄,我這裏直接提示,在實際項目中,肯定會返回到登錄頁面 ActionContext.getContext().put( "message" , "你沒有登錄" ); return "success" ; } } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
<? 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 > < package name = "struts2" namespace = "/test" extends = "struts-default" > < interceptors > < interceptor name = "logininterceptor" class = "csg.struts2.interceptor.LoginInterceptor" /> <!-- 自定義一個攔截器棧,下面你攔截只需要用這個棧名字就行了 --> < interceptor-stack name = "loginstack" > <!-- 這個是系統攔截器棧,不這樣寫,就是當你單獨一個action引用攔截器的時候就無法使用攔截器的中的核心功能 --> < interceptor-ref name = "defaultStack" /> <!-- 這個引用你自己寫的攔截器 --> < interceptor-ref name = "logininterceptor" /> </ interceptor-stack > </ interceptors > <!-- 這樣寫的意思是 在這個package包下面的所有action方法都需要攔截器 --> < default-interceptor-ref name = "loginstack" ></ default-interceptor-ref > <!-- 全局視圖,這個就是說攔截到你沒登錄的時候提示你登錄 ,你做的話可以提示返回登錄界面--> < global-results > < result name = "success" >/WEB-INF/page/message.jsp</ result > </ global-results > <!-- 通配符形式訪問action --> < action name = "list_*" class = "csg.struts2.action.StrutsAction" method = "{1}" > < result name = "success" >/WEB-INF/page/success.jsp</ result > <!-- 也可以這樣寫 ,但是這樣寫就比較麻煩了,假如你下面還有action,這樣你就每個action都需要引用,所以我注掉了--> <!-- <interceptor-ref name="loginstack"></interceptor-ref> --> </ action > </ package > </ struts > |
1
2
3
4
|
<!-- 這個是系統攔截器棧,不這樣寫,就是當你單獨一個action引用攔截器的時候就無法使用攔截器的中的核心功能 --> < interceptor-ref name = "defaultStack" /> <!-- 這個引用你自己寫的攔截器 --> < interceptor-ref name = "logininterceptor" /> |