1
使用ModelAttribute註解,我們可以提供很多東西,比如listbox。更好的方式來餵食一個列表框
爲控制器中的每個lisbox提供不同的方法或使用表單併爲每個列表框提供一個對象列表是否更好?
使用ModelAttribute註解,我們可以提供很多東西,比如listbox。更好的方式來餵食一個列表框
爲控制器中的每個lisbox提供不同的方法或使用表單併爲每個列表框提供一個對象列表是否更好?
如果控制器類用於不同的請求,其中一些有這個列表框而有些不是,(例如,控制器處理實體的顯示,創建和更新功能,其中只有創建和更新頁面具有該列表框)然後用@ModelAttribute
帶註釋的方法來模擬模型意味着,即使不需要值,也會執行該方法。 - 我謙遜的意見,這將是不好的。
我希望我能理解你的問題,如果不是的話,請爲你想比較的兩個choises中的每一個添加一個例子。而不是
@RequestMapping("/users")
@Controller
TheWayIPreferController() {
@RequestMapping(params = "form", method = RequestMethod.GET)
public ModelAndView createForm() {
ModelMap uiModel = new ModelMap();
uiModel.addAttribute("userCreateCommand", new UserCreateCommand());
uiModel.addAttribute("securityRoles", this.securityRoleDao.readAll()));
uiModel.addAttribute("salutations", this.salutationDao.readAll()));
uiModel.addAttribute("locales", this.localeDao.readAll());
return new ModelAndView("users/create", uiModel);
}
@RequestMapping(method = RequestMethod.POST)
public ModelAndView create(final @Valid UserCreateCommand userCreateCommand, final BindingResult bindingResult) {
...
}
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public ModelAndView show(@PathVariable("id") final User user) {
...
}
}
:
@RequestMapping("/users")
@Controller
TheWayIDiscourageController(){
@ModelAttribute("securityRoles")
public List<SecurityRoles> getSecurityRoles(){
return this.securityRoleDao.readAll();
}
@ModelAttribute("salutations")
public List<SecurityRoles> getSalutations(){
return this.salutationDao.readAll());
}
@ModelAttribute("locales")
public List<SecurityRoles> getLocals(){
return this.localeDao.readAll();
}
@RequestMapping(params = "form", method = RequestMethod.GET)
public ModelAndView createForm() {
return new ModelAndView("users/create", "userCreateCommand", new UserCreateCommand());
}
@RequestMapping(method = RequestMethod.POST)
public ModelAndView create(final @Valid UserCreateCommand userCreateCommand, final BindingResult bindingResult) {
...
}
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public ModelAndView show(@PathVariable("id") final User user) {
...
}
}