本文引用不少网上资料,如没写着引用出处,请原凉。
先说说tokenInterceptor的作用,防止客户重复提交表单,可以解决,客户在同一表单中点击两个提交,后退后,再提交,在提交完成后,对跳转的页面直接进行刷新,避免两次写入数据库。都可以很好的解决。
先说说什么是拦截器,拦截器,在AOP(Aspect-Oriented Programming)中用于在某个方法或字段被访问之前,进行拦截然后在之前或之后加入某些操作。拦截是AOP的一种实现策略。上面这段话是Max说的。觉得不错,很清晰明了。
我先来举个例子,大家一看就会明白的
struts.xml
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <include file="struts-default.xml"/> <package name="default" extends="struts-default"> <default-interceptor-ref name="paramsPrepareParamsStack"/> </package> <include file="strutsconfig/struts-workticket.xml" /> </struts>
struts-workticket.xml
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="demo" extends="struts-default" namespace="/demo"> <!-- 错误提示页面 --> <global-results> <result name="error">/errInfo.jsp</result> </global-results> <global-exception-mappings> <exception-mapping result="error" exception="java.lang.Throwable"/> </global-exception-mappings> <action name="tokenInterceptor_*" class="com.hz.struts2.demo.web.action.TokenInterceptorAction" method="{1}"> <result name="demo">/web/demo.jsp</result> <result name="login">/index.jsp</result> <!-- 注意:作用,如果出现重要提交的时候,跳转的页面 --> <result name="invalid.token">/overPageInfo.jsp</result> <!-- 注意:一定要写上,否则无法运行。原因:如果不写的话, 就不会调用默认的拦截器,因为被覆盖啦 --> <interceptor-ref name="defaultStack" /> <interceptor-ref name="token"> <!-- 在action中,需要进行拦截的方法 --> <param name="includeMethods">execute,checkLogin</param> <!-- 在action中,不需要进行拦截的方法 --> <param name="excludeMethods">toLogin</param> </interceptor-ref> </action> </package> </struts>
com.hz.struts2.demo.web.action.TokenInterceptorAction.java
这个没什么意思,不需要做任何改动,这就可以体现出拦截器的好出,大大减少啦代码量,提高了代码的复用性。
package com.hz.struts2.demo.web.action;
public class TokenInterceptorAction extends AbstractAction {
@Override
public String execute() throws Exception {
System.out.println(" :: execute " + new java.util.Date());
return "demo";
}
/**
* Goto login page
*/
public String toLogin() throws Exception {
System.out.println(" :: toLogin " + new java.util.Date());
//return "invalid.token";
return "login";
}
public String checkLogin() throws Exception {
System.out.println(" :: checkLogin " + " " + new java.util.Date());
return "login";
}
}
HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>index</title>
<script type="text/javascript">
function submitForm(){
var url = "/demo/tokenInterceptor_checkLogin.action";
var token = "struts.token.name=struts.token";
var token2 = "struts.token=";
token2 += document.getElementsByName("struts.token")[0].value;
url += "?" + token + "&";
url += token2;
document.location.href = url;
}
</script>
</head>
<body>
<form action="/demo/tokenInterceptor_checkLogin.action" method="post">
<s:token id="tokenId" />
<br>
<input type="submit">
<input type="button" οnclick="submitForm()" value="非表单提交">
</form>
</body>
</html>
这是有表单的情况,下面提供一段js是没有表单的情况。正好在我系统里用到了这样的需求,一样粘上来这是我做的一个简单的demo,也是一刚开始学的时候,自己学着写的。在这过程中,我过我常容易出现错误的几个问题,都做了记号
总结
要想让自己的技术不会被淘汰,只有靠自己。我想作为一名IT人员都明白一个道理,没有自学能力的人,是没法成为IT精英的。