Struts2中用标签防止重复提交问题
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<package name="weiye" namespace="/test" extends="struts-default">
<!-- name action的名字,访问时使用helloworld.action访问,class:实现类 -->
<action name="weiye" class="com.weiye.PersonAction" method="execute">
<interceptor-ref name="defaultStack"/>
<interceptor-ref name="token"/>
<result name="invalid.token">/index.jsp</result>
<result name="success">/WEB-INF/personlist.jsp</result>
</action>
</package>
</struts>
package com.weiye;
public class PersonAction {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String execute(){
return "success";
}
}
index.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>
<s:form action="weiye" namespace="/test" method="post">
姓名:<s:textfield name="name"/><s:token></s:token>
<input type="submit" value="发送">
</s:form>
</body>
</html>
personlist.jsp
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<s:property value="name"/>
<br/><%=new Date() %>
</body>
</html>
异常如下:type Exception report
message
description The server encountered an internal error that prevented it from fulfilling this request.
exception
java.lang.NullPointerException
com.opensymphony.xwork2.util.LocalizedTextUtil.findText(LocalizedTextUtil.java:692)
com.opensymphony.xwork2.util.LocalizedTextUtil.findText(LocalizedTextUtil.java:661)
com.opensymphony.xwork2.TextProviderSupport.getText(TextProviderSupport.java:206)
com.opensymphony.xwork2.TextProviderSupport.getText(TextProviderSupport.java:135)
org.apache.struts2.interceptor.TokenInterceptor.getErrorMessage(TokenInterceptor.java:179)
org.apache.struts2.interceptor.TokenInterceptor.handleInvalidToken(TokenInterceptor.java:163)
org.apache.struts2.interceptor.TokenInterceptor.handleToken(TokenInterceptor.java:148)
org.apache.struts2.interceptor.TokenInterceptor.doIntercept(TokenInterceptor.java:139)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
<s:token />标签防止重复提交,用法如下:
第一步:在表单中加入<s:token />
<s:form action="helloworld_other" method="post" namespace="/test">
<s:textfield name="person.name"/><s:token/><s:submit/>
</s:form>
第二步:
<action name="helloworld_*" class="cn.itcast.action.HelloWorldAction" method="{1}">
</action>
以上配置加入了“token”拦截器和“invalid.token”结果,因为“token”拦截器在会话的token与请求的token不一致时,将会直接返回“invalid.token”结果。