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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您有两种选择:
对于 JSR 303,您需要 Spring 3.0,并且必须使用 JSR 303 注解来注解您的 Model 类,并在参数前面写入 @Valid Web 控制器处理程序方法。 (就像威利·惠勒在他的回答中展示)。另外,您必须在配置中启用此功能:
对于 Spring 验证器,您需要编写验证器(请参阅Jigar Joshi 的答案)实现了 org.springframework.validation.Validator 接口。您必须在控制器中注册您的验证器。在 Spring 3.0 中,您可以使用 WebDataBinder
.setValidator
(setValidator 它是一种方法超类DataBinder
)有关更多详细信息,请参阅 Spring 参考,章节 5.7.4 Spring MVC 3 验证。
顺便说一句:在 Spring 2 中,SimpleFormController 中有类似
setValidator
属性的东西。You have two choices:
For JSR 303 you need Spring 3.0 and must annotate your Model class with JSR 303 Annotations, and write an @Valid in front of you parameter in the Web Controller Handler Method. (like Willie Wheeler show in his answer). Additionaly you must enable this functionality in the configuration:
For Spring Validators, you need to write your Validator (see Jigar Joshi's answer) that implements the org.springframework.validation.Validator Interface. The you must register your Validator in the Controller. In Spring 3.0 you can do this in a
@InitBinder
annotated Method, by using WebDataBinder.setValidator
(setValidator it is a method of the super classDataBinder
)For more details, have a look at the Spring reference, Chapter 5.7.4 Spring MVC 3 Validation.
BTW: in Spring 2 there was someting like a
setValidator
property in the SimpleFormController.使用简单验证器(您的自定义验证器)
您不需要请求对象来获取验证器中的参数。你可以直接从.
例如:这将检查请求中名称为
name
和age
的字段另请参阅
Using simple validator (your custom validator)
You don't need request object to get param there in Validator. You can directly have it from.
For example : This will check field from request with name
name
andage
Also See
不是 100% 确定我正确地理解了你的问题,但是使用 Spring MVC,你将对象传递到方法中并对其进行注释(至少在 Spring 3 中),如下所示:
这里的相关注释是 @Valid,它是JSR-303。还包括 BindingResult 参数,以便您可以检查错误,如上所示。
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:
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.
您可以轻松地使用 HttpServletRequest 参数添加另一个方法。
请注意,您并没有重写此处的方法
You could easily add another method with HttpServletRequest parameter.
Note that you are not overriding a method here
当我使用 spring validator 验证验证码时,我也遇到同样的问题。
在验证器实现器中,我想从 HttpSession 获取正确的验证码(从 HttpServletRequest 获取 HttpSession)。
没有找到任何好的代码可以在验证器中获取它,太糟糕了!
有一些折衷方案如下:
在绑定表单 DTO 中添加一个附加字段(调用: CorrectCaptcha),在 Controller 方法中设置 HttpSession 中的字段值,然后您可以在验证器中进行验证
在绑定形成DTO,然后可以在验证器中使用它。
I also have the same issue when i use spring validator validate captcha.
In the validator implementor, i want to get the correct-captcha from HttpSession(from HttpServletRequest get HttpSession).
Not found any good codes for get it in validator, so bad!!!
There are some compromise proposal as follow:
In the binding form DTO add an additional field (call: correctCaptcha), in the Controller method set the field value from HttpSession , then you can validate in the validator
Add the HttpServletRequest reference in the binding form DTO, then can use it in the validator.