2017-09-21 73 views
1

這是我的豆validation.but情況下的代碼示例是形式提交的錯誤信息沒有顯示如何做Bean驗證

//controller class 
     @RequestMapping(value= "/offer.create", method=RequestMethod.POST) 
     public String doActions(@Valid @ModelAttribute ("offers") Offer offer, BindingResult result, 
       @RequestParam String action, Map<String, Object> map, 
       HttpServletRequest request){ 

     // 

      if(result.hasErrors()) { 
       /*map.put("offers", offerservice.getAlloffers()); 
       map.put("Offer", new Offer()); 
       map.put("SeasonTypes", new SeasonTypes()); 
       map.put("timeBased", new TimeBased()); 
       map.put("seasons", offerservice.getAllSeasons());*/ 
       System.out.println("inside the error section!!"); 
    //   System.out.println(merchantService.getAllMerchant().size()); 
       System.out.println("The error "+result); 

       return "create-offer"; 



      } 

// ------------ -------------------------------------------------- -------------- 這是JSP形式

<div class="panel-group accordion" id="accordion" role="tablist" aria-multiselectable="true"> 
        <div class="panel panel-default red box"> 
         <div class="panel-heading" role="tab" id="heading-1"> 

           <h4 class="panel-title">Single Image</h4> 


         </div> 

          <div class="panel-body"> 
           <p>Upload or drag and drop image of your choice</p> 
                <div class="row"> 
                 <div class="col-md-12" style="margin-bottom:20px;width:100%;"> 
                  <form action="upload.image1" class="dropzone dropzone-mini" style="width:145px;" method="post" enctype="multipart/form-data"></form> 
                 </div> 
                </div> 


              <form:form action="offer.create" method="POST" commandName="Offer" id="formID" enctype="multipart/form-data"> 

                <div class="form-group"> 
                <label for="exampleInputEmail1"> Merchant Name</label> 
                <form:input path="offerHeading" class="form-control normtxtin" /> 
                <form:errors path="offers.*" cssClass="text-warning"></form:errors> 
                </div> 

                <div class="form-group"> 
                <label for="exampleInputEmail1"> Heading</label> 
                <form:input path="offerSubheading" class="form-control normtxtin" /> 
                <form:errors path="offers.*" cssClass="text-warning"></form:errors> 
                </div> 

                <div class="form-group"> 
                <label for="exampleInputEmail1">Description</label> 
                <form:textarea path="offerDescription" rows="5" cols="30" class="form-control normtxtin"/> 
                </div> 

                <div class="form-group"> 
                <label for="exampleInputEmail1">Issue Date</label> 
                <form:input path="issueDateTime" type="date" class="form-control normtxtin" placeholder="Select date"/> 
                </div> 

                <div class="form-group"> 
                <label for="exampleInputEmail1">Expiration Date</label> 
                <form:input path="expiryDate" type="date" class="form-control normtxtin" placeholder="Select date"/> 
                </div> 

                <div class="form-group"> 
                <label for="exampleInputEmail1">Offer Discount</label> 
                <form:input path="offerDiscount" class="form-control normtxtin" placeholder="offer Discount for the Entire offer"/> 
                </div> 

                <div class="form-group"> 
                <label for="exampleInputEmail1">Allocated points </label> 
                <form:input path="offerPrice" class="form-control normtxtin" /> 
                </div> 

                <div class="form-group"> 
                <label for="exampleInputEmail1">Offer Price</label> 
                <form:input path="pointsAllocated" class="form-control normtxtin" /> 
                </div> 

                <div class="form-group"> 
                <label for="exampleInputEmail1">offer Claim Limit</label> 
                <form:input path="offerClaimLimit" class="form-control normtxtin" /> 
                </div> 

                <div class="form-group"> 
                <label for="exampleInputEmail1">Available Branches</label> 

                <form:input path="offerAvailableBranches" class="form-control normtxtin" placeholder="Add branches seperated by a ',' Ex: galle, colombo, Kandy" /> 
                </div> 

                <div class="col-md-6"></div> 
                <div class="col-md-6"> 
                 <button type="submit" class="btn btn-primary" style="width:100%;font-size:16px;padding:10px;" name="action" value="Add">Create Offer</button> 
                </div> 
               </form:form> 
         </div> 
         </div> 

這是模型類

public class Offer implements java.io.Serializable { 

    private long offerid; 
    private Merchant merchant; 
    private MerchantUser merchantUserByAddedByRef; 
    private MerchantUser merchantUserByLastModifiedByRef; 
    private String issueDateTime; 
    private BigDecimal pointsAllocated; 
    private String expiryDate; 
    private String offerHeading; 
    @NotEmpty 
    private String offerSubheading; 
    @NotEmpty 
    private String offerDescription; 
    private String offerFilterTypes; 
    private String videoUrl; 

這是message.property文件

NotEmpty.Offer.offerSubheading = Heading field can't be Empty! 
NotEmpty.Offer.offerDescription = Heading field can't be Empty! 

在這裏jsp頁面,點擊後按鈕將在進行驗證後會出現相關form.when,如果他們有一些錯誤重定向到jsp頁面然後不應該有這種形式的形式here.because後,才按一下按鈕。所以這個錯誤味精不能顯示

以下圖片看來,.JSP視圖.jsp page view before press single image buttonafter click that single image button

+0

對不起 - 有問題嗎? –

+0

是的。我需要重定向表單。但是在發生驗證錯誤後,此處不會重定向該表單。上面的鏈接顯示的jsp頁面我需要重定向圖像顯示在第二鏈接[2]不是[1] [1] jsp頁面視圖之前按下單個圖像按鈕[2]後單擊該單個圖像按鈕 – kaushi

回答

0

終於我做我的工作 問題是@ModelAttribute( 「offers」)---> offers and commandName =「Of fer「彼此不匹配 :)