在方法上同时使用@RequestMapping和@ModelAttribute

时间:2012-06-07 09:21:35

标签: java ajax jsp spring-mvc

是否可以在Spring MVC控制器中对同一方法使用@RequestMapping@ModelAttribute注释?

例如,我想获得类似的东西:

@ModelAttribute("export_profiles")
@RequestMapping(value = "/profiles_list", method = RequestMethod.GET)
public @ResponseBody
ExportProfile[] getExportProfilesList() {
    edService.getProfiles();
}

我想使用这种方法,因为在我的页面中,我必须将ExportProfile的{​​{1}}列表加载到option中。在同一页面中,我可以创建/删除导出配置文件,因此我必须在我的select中刷新配置文件列表,并且我想使用对同一个metod的ajax调用异步执行此操作。

是否可以使用单个方法设置模型属性并处理异步请求?或者我必须使用两种不同的方法吗?

1 个答案:

答案 0 :(得分:1)

  

我想要做的是将ExportProfile数组设置为模型属性,   在我第一次加载页面时填充选择

你可以像这样设置一个页面的属性......

public String myMethodThatInitiatesPage(Model model) {
  model.addAttribute("exportProfiles", edService.getProfiles())
return "yrViewName";
}