2012-01-04 24 views

回答

0

如果控制器類用於不同的請求,其中一些有這個列表框而有些不是,(例如,控制器處理實體的顯示,創建和更新功能,其中只有創建和更新頁面具有該列表框)然後用@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) { 
... 
} 
} 
相關問題