我遇到了问题,需要你的帮助。
我试图从 Controller 检索一个整数值到jsp。
在我的 jsp 中,我有一个 ajax 调用:
$("#hdnCustomerSize").load(contextPath+"/customer/size", function() {
// some codes
});
在我的 Controller 中:
@RequestMapping(method = RequestMethod.GET, value="/size")
public void getCustomerSize(Model model) {
model.addAttribute("customerSize", customerService.getCustomers().size());
}
我的问题是我遇到了异常:
javax.servlet.ServletException: Could not resolve view with name 'customer/size' in servlet with name 'tombuyandsell'.
我知道我收到此异常是因为我故意没有将其映射到 views.properties
中。原因是我只想获取整数值 size
而不是整个 jsp 页面。请帮忙。
最佳答案
使用@ResponseBody
注释并将int作为字符串返回。 @ResponseBody
将导致返回类型写入响应 HTTP 主体。
@RequestMapping(method = RequestMethod.GET, value="/size")
@ResponseBody
public String getGroupChatSize(Model model) {
return Integer.toString(customerService.getCustomers().size());
}
关于java - spring mvc - 如何在不创建 View 的情况下从 Controller 检索值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13409098/