程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> Struts解決重復提交問題

Struts解決重復提交問題

編輯:關於JAVA
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=repeatsubmit
label.user=username
lable.phone=phone
label.address=address
modify.add=add
label.tokeninsert=tokeninsert
button.reset=reset
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved