在应用@valid之前的Spring MVC过程对象 [英] spring mvc process object before @valid is applied

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

问题描述

我有一个像下面这样的spring mvc控制器

I have a spring mvc controller like the following

@RequestMapping(value="/new", method=RequestMethod.POST)
public String createBooking(@Valid Booking booking, BindingResult bindingResult, Model model, Principal principal)
{
    if(bindingResult.hasErrors()) {
        return "booking/edit";
    }
    //Store Booking in db...
    ...

问题是我从POST获得的Booking对象是Spring构造的,但是验证器所需的属性之一无法填充,因为该属性不存在于表单中.所以我的问题是,在@Valid标记处理程序处理预订以添加此必需属性之前,我是否可以拦截它?

The problem is the Booking object i get from the POST is constructed by Spring, but one of the properties required by the validator cannot be populated, as the property is not present in the form. So my question is is there a way for me to intercept the Booking before it gets processed by the @Valid tag handler to add this required property?

干杯! NFV

推荐答案

我不确定我是否理解这个问题,但是听起来您想在命令对象绑定之前将命令对象中的字段设置为一个值表单提交.如果是这样,您可以在控制器中添加如下方法...

I'm not sure I understand the question, but it sounds like you want to set a field in the command object to a value before your command is bound by the form submission. If so, you can add a method in your controller as follows...

@ModelAttribute
public Booking getBooking() {
    Booking booking = new Booking();
    booking.setMyRequiredProperty("some value");
    return booking;
}

希望有帮助

这篇关于在应用@valid之前的Spring MVC过程对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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