如何在struts 2中实现表单重新提交

时间:2014-08-07 05:05:03

标签: jsp struts2

任何一个帮助我实现令牌以避免struts 2中的重新提交问题一步一步

行动编码

public String enquiry_process()
{


    qty=new ArrayList<BigDecimal>();
    matcd=new ArrayList<Integer>();




            purchaseRequisitionDets=procurementService.getPurchaseDet();


            for(int i=0;i<orderCreationBean.getChk_no().length;i++)
            {

                Integer chkno=new Integer(i);
                chkno=orderCreationBean.getChk_no()[i];
                for(PurchaseRequisitionDet purchaseRDet:purchaseRequisitionDets)
                {
                    if(purchaseRDet.getPurch_req_no().equals(chkno))
                    {
                        PurchaseRequisitionDet  objPurchaseRequisitionDet =new PurchaseRequisitionDet();
                        objPurchaseRequisitionDet.setQty(purchaseRDet.getQty());
                        objPurchaseRequisitionDet.setMat_cd(purchaseRDet.getMat_cd());
                        objPurchaseRequisitionDet.setUnit(purchaseRDet.getUnit());
                        objPurchaseRequisitionDet.setUnit_price(purchaseRDet.getUnit_price());
                        objPurchaseRequisitionDet.setTotal(purchaseRDet.getTotal());
                        objprbeanarrays.add(objPurchaseRequisitionDet);
                    }
                }
            }

            HttpSession session=request.getSession(true);
            Integer id = (Integer) session.getAttribute("userId");

            procurementService.saveenqdet(enquiryBean.getRef_no(),objprbeanarrays,orderCreationBean,id);



            enquiryHeaderdets=procurementService.getenqhdr();
            for(EnquiryHeaderdet headerdet:enquiryHeaderdets)
            {
                if(headerdet.getRef_no().equals(enquiryBean.getRef_no()))
                {
                Long enq_no=headerdet.getEnq_no();
                qty=procurementService.createenq(enq_no);
                matcd=procurementService.getmatcd(enq_no);

                }
            }
            Long enqno=enquiryBean.getRef_no();

            for(int i=0;i<qty.size();i++)
            {   
                enquiryBean=new EnquiryBean();
                enquiryBean.setQty(qty.get(i).doubleValue());
                enquiryBean.setMat_cd(matcd.get(i));
                MaterialMaster mMaster=procurementService.getMaterial(matcd.get(i));
                enquiryBean.setMat_desc(mMaster.getMat_name());
                enquiryBeans.add(enquiryBean);

            }

            for(EnquiryHeaderdet headerdet:enquiryHeaderdets)
            {
                enquiryBean2=new EnquiryBean();
                if(headerdet.getRef_no().equals(enqno))
                {
                    enquiryBean2.setEnq_no(headerdet.getEnq_no());
                    break;
                }

            }

            orderCreationBean=new OrderCreationBean();
            enquiryBean=new EnquiryBean();
            objprbeanarrays=new ArrayList<PurchaseRequisitionDet>();


    return "success";

}

struts.xml中

    <result name="success">/pages/procurement/EnquiryProcess.jsp</result>

    </action>   

    <result name="success">/pages/procurement/EnquiryDetails.jsp</result>


</action>

当我刷新此EnquiryDetails.jsp时,它在页面之前执行(EnquiryProcess.jsp)我提交了一个值,再次重新提交并保存在数据库中。

1 个答案:

答案 0 :(得分:0)

第1步:struts.xml

中为该操作设置无效令牌的结果
 <result name="invalid.token">somePage.jsp</result>

注意:不要忘记在行动中添加令牌拦截器

<action name="tokenAction" class="yourPackageName.yourActionClass">
     <interceptor-ref name="token"></interceptor-ref>
        <!--
        OR <interceptor-ref name="tokenSession"></interceptor-ref>
         -->
     <interceptor-ref name="defaultStack"></interceptor-ref>
     <result name="success" >/success.jsp</result>
     <result name="invalid.token">/invalid_token.jsp</result>
</action>

第2步:在表单中的jsp中使用<s:token />

<s:form action="tokenAction" method="post">
   <s:token />
   ....//other form elements
</s:form>

invalid_token.jsp

中的第3步:
<html>
<body>
   Please do not resubmit. Resubmission is banned on this site.
</body>
</html>

仔细阅读此示例以了解token ins struts2。您还可以逐步找到许多教程。

注意: 您也可以使用注释执行相同操作 Check this example