如何完成bean验证

时间:2017-09-21 05:56:18

标签: javascript java jsp

这是我的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页面中,点击按钮后会出现相关的表格。做完验证后,如果他们有一些错误,它会重定向到jsp页面,那么这里不应该有那个表格。因为那个表格只会出现在按钮之后单击.so错误消息无法显示

以下图片是.jsp查看.jsp page view before press single image button after click that single image button

1 个答案:

答案 0 :(得分:0)

最后我做了我的工作 问题是@ModelAttribute(“offer”)---&gt; offer和commandName =“Offer”彼此不匹配 :)