• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

Struts解决重复提交问题

servlet/jsp 搞代码 7年前 (2018-06-18) 183次浏览 已收录 0个评论
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

 

欢迎大家阅读Struts解决重复提交问题》,跪求各位点评,若觉得好的话请收藏本文,by 搞代码


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Struts解决重复提交问题
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址