token机制 防止重复提交
场景:使用Struts的Token机制防止重复提交的有关问题的简单测试
使用Struts的Token机制防止重复提交的问题的简单测试!
参考各位老大的经验,测试代码如下:
input.jsp:
success.jsp:
<%@ page contentType="text/html; charset=GBK" %>
<html><head><title>success</title></head>
<body bgcolor="#ffffff">
success!
</body>
</html>
TestForm:
package test;
import javax.servlet.http.*;
import org.apache.struts.action.*;
public class TestForm extends ActionForm {
private String name;
private String pass;
public void setPass(String pass) {
this.pass = pass;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public String getPass() {
return pass;
}
public ActionErrors validate(ActionMapping actionMapping,
HttpServletRequest httpServletRequest) {
/** @todo: finish this method, this is just the skeleton.*/
return null;
}
public void reset(ActionMapping actionMapping,
HttpServletRequest servletRequest) {
}
}
TestAction:
package test;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.Action;
import org.apache.log4j.Logger;
public class TestAction extends Action {
private static final transient Logger log = Logger.getLogger(TestAction.class);
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
TestForm testForm = (TestForm) form;
if (testForm.getName() == null) {
saveToken(request);
return mapping.getInputForward();
}
if (!isTokenValid(request)) {
saveToken(request);
log.debug("不能重复提交!");
return mapping.getInputForward();
}
log.debug("添加成功!");
resetToken(request);
return mapping.findForward("success");
}
}
struts-config.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">
<struts-config>
<form-beans>
<form-bean name="testActionForm" type="test.TestForm"/>
</form-beans>
<action-mappings>
<action name="testActionForm" path="/testAction" type="test.TestAction" scope="request" input="/index.jsp" validate="false">
<forward name="success" path="/success.jsp"/>
</action>
</action-mappings>
<message-resources parameter="ApplicationResources"/>
</struts-config>
编写完上边的代码后:http://localhost:8080/test/testAction.do可以进行测试!
因为要在action里面判断token是否合法
使用Struts的Token机制防止重复提交的问题的简单测试!
参考各位老大的经验,测试代码如下:
input.jsp:
引用
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ page contentType="text/html; charset=GBK" %>
<html:html>
<head><title>Test</title></head>
<body>
<html:form action="/testAction.do" method="post">
<br>
<html:text property="name"/>
<br>
<html:text property="pass"/>
<br>
<html:submit property="submit" value="Submit"/><br>
</html:form>
</body>
</html:html>
<%@ page contentType="text/html; charset=GBK" %>
<html:html>
<head><title>Test</title></head>
<body>
<html:form action="/testAction.do" method="post">
<br>
<html:text property="name"/>
<br>
<html:text property="pass"/>
<br>
<html:submit property="submit" value="Submit"/><br>
</html:form>
</body>
</html:html>
success.jsp:
引用
<%@ page contentType="text/html; charset=GBK" %>
<html><head><title>success</title></head>
<body bgcolor="#ffffff">
success!
</body>
</html>
TestForm:
引用
package test;
import javax.servlet.http.*;
import org.apache.struts.action.*;
public class TestForm extends ActionForm {
private String name;
private String pass;
public void setPass(String pass) {
this.pass = pass;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public String getPass() {
return pass;
}
public ActionErrors validate(ActionMapping actionMapping,
HttpServletRequest httpServletRequest) {
/** @todo: finish this method, this is just the skeleton.*/
return null;
}
public void reset(ActionMapping actionMapping,
HttpServletRequest servletRequest) {
}
}
TestAction:
引用
package test;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.Action;
import org.apache.log4j.Logger;
public class TestAction extends Action {
private static final transient Logger log = Logger.getLogger(TestAction.class);
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
TestForm testForm = (TestForm) form;
if (testForm.getName() == null) {
saveToken(request);
return mapping.getInputForward();
}
if (!isTokenValid(request)) {
saveToken(request);
log.debug("不能重复提交!");
return mapping.getInputForward();
}
log.debug("添加成功!");
resetToken(request);
return mapping.findForward("success");
}
}
struts-config.xml:
引用
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">
<struts-config>
<form-beans>
<form-bean name="testActionForm" type="test.TestForm"/>
</form-beans>
<action-mappings>
<action name="testActionForm" path="/testAction" type="test.TestAction" scope="request" input="/index.jsp" validate="false">
<forward name="success" path="/success.jsp"/>
</action>
</action-mappings>
<message-resources parameter="ApplicationResources"/>
</struts-config>
编写完上边的代码后:http://localhost:8080/test/testAction.do可以进行测试!
1 楼
wensky222
2007-04-20
如果使用velocity模板,而不使用struts的标签(velocity),在struts2下怎么解决重复提交问题呢?
2 楼
sunsy
2007-04-20
WEBWORK以前在页面上可以用标签来声明一个TOKEN,现在STRUTS要写在ACTION里吗?
3 楼
cozone_柯中
2007-04-20
sunsy 写道
WEBWORK以前在页面上可以用标签来声明一个TOKEN,现在STRUTS要写在ACTION里吗?
因为要在action里面判断token是否合法