主要内容:
1、在login.jsp页面使用token标签。
2、在配置文件struts.xml文件内添加token拦截器到拦截器栈中。
3、在配置文件struts.xml文件内action下要有一个result(name="
invalid.token"),当重复提交后的错误访问。
范例:
login.jsp
<%@
page
language
=
"java"
contentType
=
"text/html; charset=UTF-8"
pageEncoding
=
"UTF-8"
%>
<%@
taglib
uri
=
"/struts-tags"
prefix
=
"s"
%>
<!
DOCTYPE
html
PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN"
"
http://www.w3.org/TR/html4/loose.dtd"
;
>
<
html
>
<
head
>
<
meta
http-equiv
=
"Content-Type"
content
=
"text/html; charset=UTF-8"
>
<
title
>
Insert title here
</
title
>
</
head
>
<
body
>
<
form
action
=
"login.action"
method
=
"post"
>
<
s:token
/>
用户名:
<
input
type
=
"text"
name
=
"name"
><
br
>
手机号:
<
input
type
=
"text"
name
=
"phone"
><
br
>
<
input
type
=
"submit"
value
=
"提交"
>
</
form
>
</
body
>
</
html
>
struts.xml
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
<!
DOCTYPE
struts
PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
<
struts
>
<
package
name
=
"user"
namespace
=
"/"
extends
=
"struts-default"
>
<
interceptors
>
<
interceptor-stack
name
=
"myInterceptor"
>
<
interceptor-ref
name
=
"token"
/>
<
interceptor-ref
name
=
"defaultStack"
></
interceptor-ref
>
</
interceptor-stack
>
</
interceptors
>
<
default-interceptor-ref
name
=
"myInterceptor"
/>
<
action
name
=
"login"
class
=
"com.action.LoginAction"
>
<
result
name
=
"success"
>
/welcome.jsp
</
result
>
<
result
name
=
"invalid.token"
>
/500.
jsp
</
result
>
</
action
>
</
package
>
</
struts
>