request域
传统方式向request域中写入数据:直接通过request对象写入。
向model中写入数据,默认就是写入request域
session域
传统方式向session域中写入数据
通过model+@SessionAttributes将数据写入session
ServletContext域
只能通过传统方式
@ModelAttribute
被修饰的方法将会在当前类的任意handler方法执行之前执行,该方法的返回值会自动存入model供后续使用。
使用在方法参数前
会从model中获取属性值复制到被修饰的方法参数上。
?
返回字符串数据
通过response返回
response.getWriter().write("abcde");
直接获取PrintWriter返回
@RequestMapping("/test.action")
public void test(PrintWriter writer)throws Exception {
writer.write("abcde");
}
通过ResponseBody返回
@ResponseBody
@RequestMapping(value="/test.action",produces="text/html;charset=utf-8")
public String test() {
return "abcd中国";
}
返回json数据
手动拼接json
通过配置@ResponseBody利用内置的jackson将对象处理为json返回
@ResponseBody
@RequestMapping(value="/test.action",produces="application/json;charset=utf-8")
public User test() throws Exception {
User user = new User(99,"张三",Arrays.asList("bj","sh","gz"));
return user;
}
?
支持的方法参数类型
支持的返回值类型
原文:https://www.cnblogs.com/juzhuxiaozhu/p/13238261.html