我ajax的jsp页面上有一个通过URL调用spring控制器的页面/check。
ajax
jsp
/check
$.ajax({ type : "GET", url : "${pageContext.request.contextPath}/check", data : { "id" : ${articleCount} }, success: function(data){ //response from controller } });
现在,控制器看起来像
@RequestMapping("/check") public String check(@RequestParam Integer id, HttpServletRequest request, HttpServletResponse response, Model model) { boolean a = getSomeResult(); if (a == true) { model.addAttribute("alreadySaved", true); return view; } else model.addAttribute("alreadySaved", false); return view; }
我发送的数据使用模式,并试图访问它success: function(data)的"${alreadySaved}",但它显示为空白。
success: function(data)
"${alreadySaved}"
有什么方法可以true/false在查看页面上接收该数据?
true/false
您必须为Spring Ajax调用示例添加@ResponseBody批注
@RequestMapping("/check") @ResponseBody public String check(@RequestParam Integer id, HttpServletRequest request, HttpServletResponse response, Model model) { boolean a = getSomeResult(); if (a == true) { model.addAttribute("alreadySaved", true); return view; } else { model.addAttribute("alreadySaved", false); return view; } }