在struts2中使用<s:token>可以防止表單的重複提交;
用法如下:
1,在表單中添加<s:token>標籤:
<s:form action="helloworld_other" method="post" namespace="/test">
<s:textfield name="person.name"/><s:token/><s:submit/>
</s:form>
2,在struts.xml中配置攔截器:
第二步:
<action name="helloworld_*" class="com.xasxt.action.HelloWorldAction" method="{1}">
<interceptor-ref name="defaultStack" />
<interceptor-ref name="token" />
<result name="invalid.token">/WEB-INF/page/message.jsp</result>
<result>/WEB-INF/page/result.jsp</result>
</action>
以上配置加入了“token”攔截器和“invalid.token”結果,因爲“token”攔截器在會話的token與請求的token不一致時,將會直接返回“invalid.token”結果。