原理:@ResponseBody注解表示你的返回值将存在ResponseBody中返回前端,将return返回值作为请求返回值,return的数据不会成为返回跳转路径,将java对象转为json格式的数据,前端接收后会显示将数据到页面,如果不加的话,返回值将会作为url的一部分,页面会跳转到url,也就是跳转到你的返回路径(百度)
用法:一般使用在控制层(controller)的方法上,其作用是将方法的返回值以特定的格式写入到response的body区域,进而将数据返回给客户端。当方法上没有@ResponseBody,底层会将方法的返回值封装为ModelAndView对象。
使用场景:当返回的数据不是html标签的页面,而是其他某种格式的数据时(json,xml等)使用,常用在ajax异步请求中,可以通过ajax的“success” :function(data){} data直接获取到。
这段代码的return返回值是做为json格式在前台页面显示,而不是return返回值做为请求的url:
@RequestMapping(value="/login",method=RequestMethod.POST)
@ResponseBody
public MapString ,String> loginAct(User user,String information){
Map<String ,String> res = new HashMap<String,String>();
if(user == null){
res.put("type","error");
res.put("msg","请填写用户信息!");
}
if(cpacha == null){
res.put("type","error");
res.put("meg","请填写验证码!");
}
if(StringUtil.isEmpty(user.getUserName())){
res.put("type","error");
res.put("msg","请填写用户名!");
}
if(StringUtil.isEmpty(user.getPassword())){
res.put("type","error");
res.put("msg","请填写用户密码!");
}
res.put("type","success");
res.put("msg","登录成功!");
return res;
}
感谢提醒,已改正
contriller ;D