@valid 在 Spring REST 中 [英] @valid in Spring REST

查看:63
本文介绍了@valid 在 Spring REST 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Spring 3.x 中开发 REST API.出于验证的目的,@Valid 似乎符合我的要求.如何从 has.error() 中检索错误?有没有办法自定义错误信息?

解决方案

为了显示错误消息,您可以在 JSP 页面上使用 标签.请参阅下面的完整示例.

1) 在控制器上启用验证

@RequestMapping(value = "/addCollaborator", method = RequestMethod.POST)public String submitCollaboratorForm(@ModelAttribute("newCollaborator") @Valid Collaborator newCollaborator, BindingResult 结果) 抛出异常 {如果(结果.hasErrors()){返回 "collaboratorform";}collaboratorService.addCollaborator(newCollaborator);返回重定向:/listCollaborators";}

2) 在域对象中定义约束并自定义错误消息.

公共类协作者{私人长ID;@Pattern(regexp="91[0-9]{7}", message="无效的电话号码.必须以 91 开头,并且必须有 9 位数字.")私人字符串电话号码;公共合作者(){}//...}

3) 在 JSP 页面上:collaboratorform.jsp

<代码>...<div class="容器"><h3>添加协作者</h3><form:form modelAttribute="newCollaborator" class="form-horizo​​ntal"><div class="form-group"><label class="col-sm-2 control-label" for="phoneNumber">PhoneNumber:</label><div class="col-sm-10"><form:input type="text" class="form-control" id="phoneNumber" path="phoneNumber" placeholder="91 XXX XXXX"/><!-- 呈现与 phoneNumber 字段相关联的错误消息.--><form:errors path="phoneNumber" cssClass="text-danger"/>

<button class="btn btn-success" type="submit" value="addCollaborator"><span class="glyphicon glyphicon-save"></span>添加</form:form>

...

I'm trying to develop a REST API in Spring 3.x. For the purpose of validation, @Valid seems to fit my requirement. How to retrieve the errors from the has.error()? Is there a way for customized error message?

解决方案

In order to display the error messages, you can use <form:errors> tag on your JSP page. See the complete example bellow.

1) Enable Validation on the Controller

@RequestMapping(value = "/addCollaborator", method = RequestMethod.POST)
public String submitCollaboratorForm(@ModelAttribute("newCollaborator") @Valid Collaborator newCollaborator, BindingResult result)  throws Exception {

    if(result.hasErrors()) {
        return "collaboratorform";
    }

    collaboratorService.addCollaborator(newCollaborator);

    return "redirect:/listCollaborators";
}

2) Define constraints in your domain object and customize your error messages.

public class Collaborator {

    private long id;

    @Pattern(regexp="91[0-9]{7}", message="Invalid phonenumber. It must start with 91 and it must have 9 digits.")
    private String phoneNumber;

    public Collaborator(){

    }

    //...
}

3) On JSP page: collaboratorform.jsp

...
<div class="container">

    <h3>Add Collaborator</h3>      

    <form:form modelAttribute="newCollaborator" class="form-horizontal">

      <div class="form-group">  
          <label class="col-sm-2 control-label" for="phoneNumber">PhoneNumber:</label>
          <div class="col-sm-10">
            <form:input type="text" class="form-control" id="phoneNumber" path="phoneNumber" placeholder="91 XXX XXXX" />

            <!-- render the error messages that are associated with the phoneNumber field. -->
            <form:errors path="phoneNumber" cssClass="text-danger"/>
          </div>
      </div>

        <button class="btn btn-success" type="submit" value ="addCollaborator">
            <span class="glyphicon glyphicon-save"></span> Add
        </button>

    </form:form>

</div>

...

这篇关于@valid 在 Spring REST 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆