Spring MVC的3 - >验证 [英] Spring MVC 3 -> Validation

查看:169
本文介绍了Spring MVC的3 - >验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Spring MVC应用程序,它与AJAX / JSON的前端通信,我在前端Web应用程序使用HTML

I have a Spring MVC application which communicates with the frontend with AJAX / JSON, and I have in the frontend a web application with HTML.

有关添加元素到数据库中,我这样做后端系统:

For adding an element to the database, I do this in the backend system:

@RequestMapping(value="add/", method=RequestMethod.POST)
public @ResponseBody SerializePerson addProject(@RequestBody Person person) {
    Person p = this.personService.addPerson(person);
    return new SerializePerson(p.getId(), p.getName(), p.getEmail());
}

但现在我有这个问题(这是一个非常简单的例子),有人可以创建一个项目没有名字,所以这个名字=和一个非有效的电子邮件地址。 我的问题是,我想验证的后端系统等领域。

But now I have the problem (this is a very simple example), that someone can create a project without a name, so the name = "" and a non valid email address. My problem is, that I want to validate the fields in the backend system.

所以,我发现了一个Spring MVC的展示位置:<一href="https://src.springsource.org/svn/spring-samples/mvc-showcase/src/main/java/org/springframework/samples/mvc/validation/" rel="nofollow">https://src.springsource.org/svn/spring-samples/mvc-showcase/src/main/java/org/springframework/samples/mvc/validation/

So I found a Spring MVC showcase here: https://src.springsource.org/svn/spring-samples/mvc-showcase/src/main/java/org/springframework/samples/mvc/validation/

他们这样做是:

@RequestMapping("/validate")
public @ResponseBody String validate(@Valid JavaBean bean, BindingResult result) {
    if (result.hasErrors()) {
        return "Object has validation errors";
    } else {
        return "No errors";
    }
}

所以,这是最好的方法是什么?所以,我必须做两步骤:

So, is this the best way? So I have to do two steps:

  1. 验证Person对象(如果没有发生错误,则转到步骤2,否则显示错误信息给用户)
  2. 写Person对象的datbase

是不是可以结合在一个步骤这两个步骤?我怎样才能把从前端的人POST对象的验证方式在后台,看看哪场失败(姓名或电子邮件),因为只有告诉对象有验证错误不是那么好:-)?

Isn't it possible to combine these two steps in one step? And how can I put the Person POST object from the frontend to the "validate" method in the backend and to see which field fails (name or email), because telling only "Object has validation errors" is not so good :-)?

最好的问候。

推荐答案

我做了以下示例所示:的 http://blog.springsource.com/2010/01/25/ajax-simplifications-in-spring-3-0/

I did it shown in this example: http://blog.springsource.com/2010/01/25/ajax-simplifications-in-spring-3-0/

@RequestMapping(method=RequestMethod.POST)
public @ResponseBody Map<String, ? extends Object> create(@RequestBody Account account, HttpServletResponse response) {
    Set<ConstraintViolation<Account>> failures = validator.validate(account);
    if (!failures.isEmpty()) {
        response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
        return validationMessages(failures);
    } else {
        accounts.put(account.assignId(), account);
        return Collections.singletonMap("id", account.getId());
    }
}

这篇关于Spring MVC的3 - &GT;验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆