在我的 Controller 中我有这些 ff 方法
@RequestMapping("/countryList.html")
@ModelAttribute("countries")
public Collection<Country> getCountries() {
return worldService.getAllCountries();
}
@RequestMapping("/countryList.html")
public String getName() {
return viewers_name;
}
我试图做的是在 countryList.html 中,它将返回国家和当前查看它的用户的名称,但是在访问 countryList.html 时它返回给我一个异常
Ambiguous handler methods mapped for HTTP path '/countryList.html': {public java.lang.String levelup.world.web.CountryController.getName(), public java.util.Collection levelup.world.web.CountryController.getCountries()}.
我该如何解决这个问题?
最佳答案
@RequestMapping("/countryList.html") 对于方法应该是唯一的。您如何将此请求映射到两种方法。
根据您的评论:-
@RequestMapping(value = "/countryList.html")
public Collection<Country> getCountries(ModelMap model) {
model.addAttribute("countries", countryObject);
return viewName;
}
或者在config中定义jsonView返回ajax调用的json对象
@RequestMapping(value = "/countryList.html")
public Collection<Country> getCountries(ModelMap model) {
model.addAttribute("countries", countryObject);
return jsonView;
}
关于java - Spring MVC 在一个请求中返回多个对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15067179/