Json @restcontroller和@responsebody在Spring MVC+;百里香
在配置为使用Thymeleaf 2.1.4的SpringMVC4.1.5应用程序中(在使用JSP之前,它工作正常),我无法返回JSON响应 无论我的请求映射是否在@RestController中,或者是否用@responsebody注释,它都会返回一个完整的HTML页面 这是控制器 在@controller类中,我有以下映射Json @restcontroller和@responsebody在Spring MVC+;百里香,json,spring,spring-mvc,thymeleaf,spring-restcontroller,Json,Spring,Spring Mvc,Thymeleaf,Spring Restcontroller,在配置为使用Thymeleaf 2.1.4的SpringMVC4.1.5应用程序中(在使用JSP之前,它工作正常),我无法返回JSON响应 无论我的请求映射是否在@RestController中,或者是否用@responsebody注释,它都会返回一个完整的HTML页面 这是控制器 在@controller类中,我有以下映射 @RequestMapping(value = { "/", "/land", "/login" }, method = RequestMethod.GET) public
@RequestMapping(value = { "/", "/land", "/login" }, method = RequestMethod.GET)
public String getLogin(Model model, HttpSession session) {
session.setAttribute("login", "none");
System.out.println(programId);
model.addAttribute("signUpForm", new SignUpForm());
return "login";
}
在@RestController类中,下面是相同URL的post方法
@RequestMapping(value = {"/login" }, method = RequestMethod.POST )
public @ResponseBody HashMap<String, Object> login2(@RequestBody SignUpForm signUpForm, HttpServletRequest request,
HttpServletResponse httpServletResponse, HashMap<String, Object> mo, HttpSession session ) {
User user = userDao.findUserByName(signUpForm.getUserName());
if (user != null && encoder.matches(signUpForm.getPassword(), user.getPassword())&& user.getProgram_id()==3) {/* && user.getProgram_id()==3*/
session.setMaxInactiveInterval(1200);
System.out.println(session.getMaxInactiveInterval()+":"+session.getLastAccessedTime()+":"+session.getCreationTime()+":"+session.getServletContext().getContextPath());
session.setAttribute("login", "success");
mo.put("redirect", "/home");
String ipAddress = request.getHeader("X-FORWARDED-FOR");
if (ipAddress == null) {
ipAddress = request.getRemoteAddr();
}
session.setAttribute("urip", ipAddress);
return mo;
} else {
mo.put("error", "Login failed. Please check your credentials");
return mo;
}
}
嗯。这是我想出来的使它工作,但我仍然不确定的原因
public @ResponseBody HashMap<String, Object> login2(@RequestBody SignUpForm signUpForm, HttpServletRequest request,
HttpServletResponse httpServletResponse, HashMap<String, Object> mo, HttpSession session ){
////
}
public@ResponseBody HashMap login2(@RequestBody-SignUpForm-SignUpForm,HttpServletRequest-request,
HttpServletResponse HttpServletResponse,HashMap mo,HttpSession会话){
////
}
在上面的方法签名中,我“注入”了一个hashmap。spring框架默认值或某个未知配置决定注入一个“绑定结果感知Hashmap”。但为了修复它,我不得不做一个
HashMap<String, Object> mo=new HashMap<String, Object>;
HashMap mo=新的HashMap;
在方法体内部,删除注入的hashmap。
如果有人能够理解这种行为,请解释。我觉得在我对spring框架的认识中遗漏了一些基本的东西
public @ResponseBody HashMap<String, Object> login2(@RequestBody SignUpForm signUpForm, HttpServletRequest request,
HttpServletResponse httpServletResponse, HashMap<String, Object> mo, HttpSession session ){
////
}
HashMap<String, Object> mo=new HashMap<String, Object>;