Struts用Token解決重複提交

主要流程:

index.jsp-->preInsertAction-->insert.jsp-->insertAction

詳細解釋:


1.在頁面中加入跳轉到preInsertAction的鏈接

<html:link action="preInser">
 插入
</html:link>
或者
<html:link page="preinsert" path="preInsert.do">
 插入
</html:link>
或者
<html:link forward="preinsert">
 插入
</html:link>
一般是都是用<html:link>,其中常用的跳轉標記有三種page,forward,action
當然action使用最簡單,其它兩種還要在struts-config.xml中的全局跳轉配置一個forword,如下:
  <global-forwards >
 <forword name="" path="preInsert.do"/>
  </global-forwards>

2.struts-config.xml的配置

<action
 attribute="insertForm"
 input="/insert.jsp"
 name="insertForm"
 path="/insert"
 scope="request"
 type="......InsertAction" />
<action path="/perpareInsert" type=".......PreInsertAction" >
     <forward name="insert" path="/insert.jsp"/>
</action>

3.PreInsertAction

 public ActionForward execute(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response) {
  //創建一個新的令牌
  this.saveToken(request);
  return mapping.findForward("insert");
 }


4.InsertAction

 public ActionForward execute(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response) {
  InsertForm insertForm = (InsertForm) form;// TODO Auto-generated method stub

  if(!isTokenValid(request)){
   ActionMessages errors=new ActionMessages();
   errors.add("insertToken",new ActionMessage("error.token"));
   this.saveErrors(request, errors);
   this.saveToken(request);
   return  mapping.getInputForward();
  
  }else{
   this.resetToken(request);
  }
  //邏輯應該寫在這裏
  return mapping.findForward(" ");
 }

主要流程:

index.jsp-->preInsertAction-->insert.jsp-->insertAction

詳細解釋:


1.在頁面中加入跳轉到preInsertAction的鏈接

<html:link action="preInser">
 插入
</html:link>
或者
<html:link page="preinsert" path="preInsert.do">
 插入
</html:link>
或者
<html:link forward="preinsert">
 插入
</html:link>
一般是都是用<html:link>,其中常用的跳轉標記有三種page,forward,action
當然action使用最簡單,其它兩種還要在struts-config.xml中的全局跳轉配置一個forword,如下:
  <global-forwards >
 <forword name="" path="preInsert.do"/>
  </global-forwards>

2.struts-config.xml的配置

<action
 attribute="insertForm"
 input="/insert.jsp"
 name="insertForm"
 path="/insert"
 scope="request"
 type="......InsertAction" />
<action path="/perpareInsert" type=".......PreInsertAction" >
     <forward name="insert" path="/insert.jsp"/>
</action>

3.PreInsertAction

 public ActionForward execute(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response) {
  //創建一個新的令牌
  this.saveToken(request);
  return mapping.findForward("insert");
 }


4.InsertAction

 public ActionForward execute(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response) {
  InsertForm insertForm = (InsertForm) form;// TODO Auto-generated method stub

  if(!isTokenValid(request)){
   ActionMessages errors=new ActionMessages();
   errors.add("insertToken",new ActionMessage("error.token"));
   this.saveErrors(request, errors);
   this.saveToken(request);
   return  mapping.getInputForward();
  
  }else{
   this.resetToken(request);
  }
  //邏輯應該寫在這裏
  return mapping.findForward(" ");
 }

 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章