可以在运行@Validated之前修改@ModelAttribute [英] Possible to modify @ModelAttribute before @Validated is run

查看:183
本文介绍了可以在运行@Validated之前修改@ModelAttribute的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以在通过@Validated验证@ModelAttribute之前对其进行修改.

Is is possible to modify a @ModelAttribute before it is validated via @Validated.

@RequestMapping(value = "/doSomething", method = RequestMethod.POST)
public final ModelAndView save(
        @Validated(value = {myGroup.class}) @ModelAttribute("myObject") MyObject myObject)

我需要在执行@Validated之前更改myObject的状态

I need to change the state of myObject before @Validated is executed

推荐答案

如何添加ModelAttribute填充方法?

What about add a ModelAttribute populate method?

@ModelAttribute("myObject")
public MyObject modifyBeforeValidate(
        @ModelAttribute("myObject") MyObject myObject) {
    //modify it here
    return myObject;
}

副作用是,如果我没记错的话,该方法将在每个@RequestMapping方法之前调用.

The side affect is this method will be invoked before every @RequestMapping method if I'm not mistaken.

Update1:​​示例

Update1: example

@ModelAttribute("command")
public ChangeOrderCommand fillinUser(
        @ModelAttribute("command") ChangeOrderCommand command,
        HttpServletRequest request) {
    command.setUser(securityGateway.getUserFrom(request));
    return command;
}

@RequestMapping(value = "/foo/bar", method = RequestMethod.POST)
public String change(@ModelAttribute("command") ChangeOrderCommand command,
        BindingResult bindingResult, Model model, Locale locale) {
}

这篇关于可以在运行@Validated之前修改@ModelAttribute的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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