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

标签 java ajax jsp spring-mvc

是否可以同时使用 @RequestMapping@ModelAttribute Spring MVC Controller 中相同方法的注释?

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

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

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

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

最佳答案

What I want to do is to set an ExportProfile array as model attribute, to populate a select the first time I load the page

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

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

关于java - 在方法上同时使用 @RequestMapping 和 @ModelAttribute,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10929030/

相关文章:

java - 在这种情况下使用空的 catch block 是不好的做法吗?

c# - 冒泡排序算法的空间复杂度

java - Whitelabel 错误页面出现意外错误(type=Not Found,status=404)./WEB-INF/views/home.jsp

php - FB JavaScript 中的 SQL 查询

java - 我想在 Spring 项目中使用 JSP 文件的开发版本。是否可以?

java - 需要一个在jsp中读取动态复选框ID的javascript函数

java - 写入包含子文件夹的新文件时自动创建完整路径

java - Spring Batch - 无法解析格式正确的日期

javascript - CORS问题和415不支持媒体回应

ajax - 主干抛出集合[方法]函数错误