spring mvc中的表单验证问题

时间:2015-12-05 08:37:35

标签: java validation spring-mvc

我有一个实体

@Entity
public class Foo {   

    @Id
    @GeneratedValue
    private Integer id;

    @Size(min = 5,max = 50, message = "Valid title is 5-50 chars")
    private String title;

    @Lob
    @Column(length=1000000)
    private String description;
    @NotNull(message = "Location must be specified")
    private String location;
// getters & setters go here
}

我有一个将Foo保存为

的方法
@RequestMapping(value = "/add", method = RequestMethod.POST)
    public String addDetail(@ModelAttribute("foo") Foo foo) {               
            fooService.persistFoo(foo); 
            return "redirect:/";            
    } 

此时如果我提交包含无效数据的表单,我会得到例外

HTTP Status 500 - Request processing failed; nested exception is javax.validation.ConstraintViolationException: Validation failed for classes [com.rhcloud.pro.entity.Foo] during persist time for groups [javax.validation.groups.Default, ]

现在我想将表单验证实现为

@RequestMapping(value = "/add", method = RequestMethod.POST)
    public String addDetail(@ModelAttribute("foo") @Valid Foo foo, BindingResult bindingResult) {

        if (!bindingResult.hasErrors()) {               
            fooService.persistFoo(foo);
            return "redirect:/";
        }else{              
            return "add";                           
        }           
    } 

和add.jsp是

<form:errors path="foo"/>                           
<form:form commandName="foo" class="form" id="add">                         
    <div class="form-group form-group-label">
        <div class="row">
            <div class="col-md-10 col-md-push-1">
                <label class="floating-label" for="login-username">Title</label>
                <form:input name="title" path="title" cssClass="form-control" />
                <span class="form-help form-help-msg text-red"><i class="form-help-icon icon icon-error"></i></span>
            </div>
        </div>
    </div>  
    <div class="form-group form-group-label">
        <div class="row">
            <div class="col-md-10 col-md-push-1">
                <label class="floating-label" for="login-username">Location</label>
                <form:input name="location" path="location" cssClass="form-control" />
            </div>
        </div>
    </div>
    <div class="form-group form-group-label">
        <div class="row">
            <div class="col-md-10 col-md-push-1">
                <label class="floating-label" for="login-username">Description</label>
                <form:textarea name="description" path="description" cssClass="form-control" />
            </div>
        </div>
    </div>

    <div class="form-group">
        <div class="row">
            <div class="col-md-10 col-md-push-1">
                <button
                    class="btn btn-block btn-blue waves-button waves-effect waves-light">Add</button>
            </div>
        </div>
    </div>
</form:form>

现在,如果我提交包含无效数据的表单,我不会得到异常,我会重定向到我的添加页面,但我没有看到任何错误消息,但如果我提供正确的数据到表单,那么表单被重定向到添加页面,我无法弄清楚如何正确完成表单验证。

0 个答案:

没有答案