参考各位老大的经验,测试代码如下:
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>
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可以进行测试!
分享到:
相关推荐
struts token机制解决表单重复提交
JavaEE Struts2利用tokenSession防止重复提交
struts2防止表单重复提交,利用struts的拦截器tokenSession,轻轻松松解决表单重复提交的问题。 附件为源代码,后台延迟了3秒,可直接在web服务器下部署运行,输入用户名和密码后,多点几次提交按钮,然后看控制台...
利用Token机制解决重复重复提交
详细介绍struts+token机制解决表单重复提交问题。附带相关代码
用struts使用Token机制来实现防重复提交
用struts的token解决重复提交问题
在struts 中可以通过token 来解决重复提交的问题。
提供源代码和思想,整体架构都在,只需加入你自己的业务逻辑即可。
struts2_token控制刷新重复提交
这里是Struts2 中放置表单重复提交的例子。
详解struts2的token机制和cookie来防止表单重复提交 今天在做一个投票系统时要实现防止表单重复提交! 当时就想到了用struts2提供的token机制 struts2的token机制防止表单重复提交: 首先需要在提交的jsp页面(要...
Struts 之旅 - 重复提交 token
token-springMVC 防止重复提交
防止表单重复提交的方法(简单的token方式),内附实现代码及实现思路。
本文实例讲述了PHP使用token防止表单重复提交的方法。分享给大家供大家参考,具体如下: <?php /* * PHP使用token防止表单重复提交 * 此处理方法纯粹是为了给初学者参考 */ session_start(); function set_token...
主要讲解了在structs怎样通过Token令牌解决表单重复提交的问题。附带了擦参考项目。
Struts2 防重复提交Demo 利用Struts2自带的s:token写的防数据重复提交策略.
Struts2拦截器的实现原理相对简单,当请求struts2的action时,Struts 2会查找配置文件,并根据其配置实例化相对的 拦截器对象,然后串成一个列表,最后一个一个地调用列表中的拦截器 三、定义Struts2拦截器。 ...
struts2中token限制表单多次提交