在Spring MVC中验证 [英] Validation in Spring MVC

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

问题描述

如何在验证器类中获取请求对象,因为我需要验证内容,即请求对象中存在的参数。

how to get the request object in the validator class, as i need to validate the contents ie the parameters present in the request object.

推荐答案

不是100%确定我正确地关注了你的问题,但是使用Spring MVC,你将对象传递给方法并且注释它(至少在Spring 3中),如下所示:

Not 100% sure I'm following your question correctly, but with Spring MVC, you pass the object into the method and annotate it (at least with Spring 3), like so:

@RequestMethod(value = "/accounts/new", method = RequestMethod.POST)
public String postAccount(@ModelAttribute @Valid Account account, BindingResult result) {
    if (result.hasErrors()) {
        return "accounts/accountForm";
    }

    accountDao.save(account);
}

这里的相关注释是@Valid,它是JSR-303的一部分。包括BindingResult参数,以便您可以检查错误,如上所示。

The relevant annotation here is @Valid, which is part of JSR-303. Include the BindingResult param as well so you have a way to check for errors, as illustrated above.

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

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