Struts解决重复提交问题

 
Insert.jsp代码如下:
<%@ page contentType = "text/html;charset=UTF-8" language = "java" %>
<%@ taglib uri = "/WEB-INF/struts-bean.tld" prefix = "bean" %>
<%@ taglib uri = "/WEB-INF/struts-html.tld" prefix = "html" %>
<%@ taglib uri = "/WEB-INF/struts-logic.tld" prefix = "logic" %>
< html:html locale = "true" >
  < head >
    < html:base />
     < title > insert.jsp </ title >
  </ head >
   < body >
  < html:form action = "insert" method = "post" >
    < bean:message key = "label.name" />< html:text property = "name" />
   < bean:message key = "label.phone" />< html:text property = "phone" />
    < bean:message key = "label.address" />< html:text property = "address" />
    < html:submit >< bean:message key = "modify.add" /></ html:submit >
     < html:reset >< bean:message key = "button.reset" /></ html:reset >
  </ html:form >
< html:errors />
  </ body >
</ html:html >
PrepareInsertAction 代码如下:
public class PrepareInsertAction extends Action {
    public ActionForward execute(
        ActionMapping mapping,
        ActionForm form,
        HttpServletRequest request,
        HttpServletResponse response) {
        System.out.print("hello PrepareInsertAction");
        // 创建一个新的令牌
            saveToken(request);
            return mapping.findForward("inserttoken");
    }
}
InsertAction 代码如下:
public class InsertAction extends Action {
    public ActionForward execute(
        ActionMapping mapping,
        ActionForm form,
        HttpServletRequest request,
        HttpServletResponse response) throws Exception {
        ModifyForm modifyForm = (ModifyForm) form;
        // 得到 token
        String token_request =request.getParameter("org.apache.struts.taglib.html.TOKEN");
        // 打出来看看是什么东东
        System.out.println("token in request is:"+ token_request);
        ActionErrors errors = new ActionErrors();
        // 判断 token 是否有效 , 如果为假证明有重复提交,声明一个错误信息,并反馈出来
        if(!isTokenValid(request)){
         errors.add("insettoken",new ActionError("error.invalid.token"));
 
           saveErrors(request, errors);
           saveToken(request);
           return (new ActionForward(mapping.getInput()));
     }
        // 当用户首次提交时返 !isTokenValid() 返回 true
     else{
            resetToken(request);
     } 
        int result=new ListBean().add(modifyForm);
       
        if(result==1){ 
        return mapping.findForward("addlist");
        }else{
            return mapping.findForward("error");   
        }
    }
 
}
ModifyForm 代码如下:
public class ModifyForm extends ActionForm {
    public void reset(ActionMapping mapping, HttpServletRequest request) {
this.address=null;
this.name=null;
this.phone=null;
    }
    public String getPhone() {
        return phone;
    }
    public void setPhone(String phone) {
        this.phone = phone;
    }
    public String getAddress() {
        return address;
    }
 
    public void setAddress(String address) {
        this.address = address;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
}
Struts-config.xml 配置如下:
< struts-config >
  < data-sources />
  < form-beans >
    < form-bean name = "modifyForm" type = "com.lyx.struts.form.ModifyForm" />
  </ form-beans >
  < global-exceptions />
  < global-forwards >
         < forward name = "inserttoken"  path = "/tokeninsert.jsp" />
  </ global-forwards >
  < action-mappings >
     <!-- 插入记录时 token 检验第一个跳的 action -->
    < action path = "/prepareInsert" type = "com.lyx.struts.action.PrepareInsertAction" />
<!-- 插入记录时 token 检验后的第二个 action -->
< action
      attribute = "modifyForm"
      input = "/tokeninsert.jsp"
      name = "modifyForm"
      path = "/insert"
      scope = "request"
      type = "com.lyx.struts.action.InsertAction" />
    </ action-mappings >
  < message-resources parameter = "com.lyx.struts.ApplicationResources" />
</ struts-config >
Applicationresources.properties 配置如下:
error.invalid.token= repeat submit
label.user= username
lable.phone=phone
label.address= address
modify.add= add
label.tokeninsert= token insert
button.reset=reset
 
 
 
 
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值