@ModelAttribute in a method

后端 未结 1 1526
粉色の甜心
粉色の甜心 2020-12-18 05:15

Imagine a code like this one:

@RequestMapping(value=\"/users\", method=RequestMethod.GET)
public String list(Model model) {
    ...
}

@InitBinder(\"user\")         


        
相关标签:
1条回答
  • 2020-12-18 06:13

    That's not really what @ModelAttribute is for. If you use it as a method parameter, it puts the annotated parameter into the model (that's fine). If you put it on a method, it's called every time to provide reference data that every method in the controller should have access to.

    If you want to take control of building up your User object, you have several options. The two that are most obvious to me are:

    1. Use your InitBinder method to add a new custom editor (a PropertyEditor class) for building User objects,
    2. Use the conversion service in Spring 3 to convert string usernames to User objects.
    0 讨论(0)
提交回复
热议问题