本文實例為大家分享了Struts2框架攔截器實例的示例代碼,供大家參考,具體內容如下
在看攔截器的小例子的前我們先來看看sturts2的原理
struts2自己是有攔截器的,通過攔截器可以攔截用戶請求,并作出處理
攔截器作用有很多,譬如:
1.Action里面有個屬性,這個屬性我想在action執行之前改成別的值,可以用攔截器解決。
2.比如每個人執行action之前,我可以查看他們有沒有這個權限執行這個action。
如果不設置攔截器,你要在每種action方法之前設置判定程序,非常繁瑣。
攔截器interceptor體現了一種編程理念,叫做AOP(面向切面編程)
實例1:使用token攔截器控制重復提交
token是用來解決下面的問題:
一旦有人通過表單提交數據,在提交表單的時候頁面提交速度太慢,用戶一直不停的刷新,如果不做一種機制防止他刷新的話,那么數據庫中就會多出好多垃圾數據。
表單提交一般都要寫成post(第一種解決方式,瀏覽器會提醒你是否重復提交)
攔截器解決方法:
struts2定義了一個攔截器(interceptor)叫--token
token的意思是“令牌”,你要提交數據,我先發給你一個令牌,你的令牌要是和我能對上,你就提交,對不上就不允許提交
token為什么可以防止重復提交?
答:當訪問界面時,在服務器那邊的session里面,生成一個隨機數,然后再把隨機數寫到form里,提交數據時session就會被帶到服務器去。提交完成后session里面的值被清空,再次重復提交的時候,發現此token值在session不存在,說明已經被提交過了,這個時候就會顯示友好界面提示用戶。
實現代碼:
struts.xml:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
< package name = "test" namespace = "/javaee" extends = "struts-default" > < action name = "pinput" class = "cn.edu.hpu.action.PinputAction" > < result >/input.jsp</ result > </ action > < action name = "person" class = "cn.edu.hpu.action.PersonAction" > < result >/addOK.jsp</ result > < interceptor-ref name = "defaultStack" ></ interceptor-ref > < interceptor-ref name = "token" ></ interceptor-ref > < result name = "invalid.token" >/error.jsp</ result > </ action > </ package > |
PersonAction.java:
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 cn.edu.hpu.action; import com.opensymphony.xwork2.ActionSupport; public class PersonAction extends ActionSupport { private String name; private int age; @Override public String execute() throws Exception { System.out.println( "a person added!" ); return super .execute(); } public String getName() { return name; } public void setName(String name) { this .name = name; } public int getAge() { return age; } public void setAge( int age) { this .age = age; } } |
input.jsp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
<%@ 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%>" rel="external nofollow" > < title >My JSP 'input.jsp' starting page</ title > </ head > < body > < form action="<%=basePath %>javaee/person" method="post"> name:< input name = "name" > age:< input name = "age" > < input type = "submit" value = "add" > </ form >< br /> </ body > </ html > |
addOK.jsp:
1
2
3
4
5
6
7
8
9
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> < html > < head > < title >My JSP 'addOK.jsp' starting page</ title > </ head > < body > add ok!! < br /> </ body > </ html > |
error.jsp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<%@ 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 'error.jsp' starting page</title> </head> <body> 嚴禁做重復的事!!! <br> </body> </html> |
結果:
填寫name與age之后,會跳入界面addOK.jsp,控制臺會輸出a person added!
返回再次提交時,就會跳轉到error.jsp界面,無法重復提交
如果在表單中加<s:token></s:token>,則會看到源碼:
1
2
3
|
< input type = "hidden" name = "struts.token.name" value = "struts.token" /> < input type = "hidden" name = "struts.token" value = "PZOQNKARYVQYDEVGNKTWFBF17735K6AI" /> <!--相當于生成了一個隨機數--> |
所原理是:在提交頁面形成了一個token,這個token在服務器端對應的session里面已經有了,當我一點提交的時候,由于加了<interceptor-ref name="token"></interceptor-ref>(
token的攔截器),服務器就會幫我攔截,看看session里面有沒有token的值,如果之前沒有提交,session里面是有這個token值的,如果上次提交過了,session就會將token值清除掉。當發現頁面的token值在服務器的session中找不到時,服務器發現出錯了,重定向到error.jsp,顯示錯誤信息
實例2:自定義攔截器
struts.xml:
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
|
< pre name = "code" class = "html" >< pre name = "code" class = "html" >< pre name = "code" class = "html" >< pre name = "code" class = "html" ><? xml version = "1.0" encoding = "GBK" ?> <!--指定struts2配置文件的DTD信息--> <!DOCTYPE struts PUBLIC "-//apache Software Foundation//DTD Struts Configuation 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <!-- struts 是struts2配置文件的根元素--> < struts > < constant name = "struts.devMode" value = "true" ></ constant > < constant name = "struts.i18n.encoding" value = "UTF-8" ></ constant > <!--允許靜態方法的執行--> < constant name = "struts.ognl.allowStaticMethodAccess" value = "true" ></ constant > < package name = "test" namespace = "/" extends = "struts-default" > < interceptors > < interceptor name = "my" class = "cn.edu.hpu.interceptor.MyInterceptor" ></ interceptor > </ interceptors > < action name = "test" class = "cn.edu.hpu.action.TestAction" > < result >/test.jsp</ result > < interceptor-ref name = "my" ></ interceptor-ref > < interceptor-ref name = "defaultStack" ></ interceptor-ref > </ action > </ package > </ struts > |
TestAction.java:
1
2
3
4
5
6
7
8
9
10
|
package cn.edu.hpu.action; import com.opensymphony.xwork2.ActionSupport; public class TestAction extends ActionSupport{ @Override public String execute() throws Exception { // TODO Auto-generated method stub return super .execute(); } } |
MyInterceptor.java:
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
|
package cn.edu.hpu.interceptor; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.Interceptor; public class MyInterceptor implements Interceptor{ public void destroy() { } public void init() { } //寫好了一個攔截(計算了一個action運行的時間) public String intercept(ActionInvocation invocation) throws Exception { long start=System.currentTimeMillis(); String r=invocation.invoke(); long end=System.currentTimeMillis(); System.out.println( "Action Time=" +(end-start)); return r; } } |
訪問:http://localhost:8080/struts2_LanJieQi/test后
控制臺輸出:
Action Time=200
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對服務器之家的支持。
原文鏈接:https://blog.csdn.net/acmman/article/details/47111009