一、响应标准servlet
// 1.响应标准servletAPI
@RequestMapping("doResData01")
public String doResData01(HttpServletRequest request){
request.setAttribute("data1","sys log data");
return "response"; //这是返回一个view name
// 返回的view会通过视图解析器进行解析,获取具体页面
//前端控制器基于师徒解析器返回的页面,进行请求转发
// http://localhost:8080/ai/resp/doResData01
}
二、重定向
@RequestMapping("doResData02")
public String doResData02(HttpServletRequest request){
request.setAttribute("data2","request redirect");
return "redirect:doResData01" ;
// 重定向标识: 【redirect:】
// 访问此方法时,客户端向服务端发起了两次请求
// 当需要重定向到某个url时候,需要添加redirect前缀
//定义重定向的页面内容不会被获取到,直接重定向到另一个页面,只获取另一个页面上的数据
// 访问:http://localhost:8080/ai/resp/doResData02
// 结果访问:http://localhost:8080/ai/resp/doResData01
}
三、ModelAndView对象
@RequestMapping("doResData03")
public ModelAndView doResData03(ModelAndView mav){
// 系统底层会将数据存储到request作用域
// ModelAndView mav = new ModelAndView();
mav.addObject("data1","model and view data1");
//当系统返回mav时,经过视图解析获取view页面,并将请求转发到此页面
//作用:请求转发
mav.setViewName("response");//设置视图页面。response.jsp
return mav;
}
四、Model对象
@RequestMapping("doResData04")
private String doResData04(Model md){ //Model Map
md.addAttribute("data1","model message");
return "response";
}
五、Map对象
@RequestMapping(“doReqMap01”)
private String doReqMap01(Map<String,Object> map){
map.put(“data1”,“This Is Map Message”);
return “response”;
}
六、关于JSON
// JSON :一种数据格式
// JSON格式字符串: “{id:10,name:AAA,...}”
// JSON格式字符串: {Key1:Value1, Key2:Value2,Key3:Value3,...,Keyn:Valuen}
// 用,间隔
//
@RequestMapping("doReqJson01")
@ResponseBody
public Map<String,Object> doReqJson01(){
Map<String,Object> map = new HashMap<String, Object>();
map.put("id",10);
map.put("name","Alice");
return map;
// 直接运行会出现500异常,因为map无法直接输出 需要引入第三方插件jackson-databind
// 添加pom.xml的依赖后
// Spring MVC系统底层可以调用第三方的API将map对象转换为JSON格式的字符串
// http://localhost:8080/ai/resp/doReqJson01
// 输出:{"name":"Alice","id":10}
// jackson(Sping默认支持)
//阿里的fastjson(Spring默认不支持,需要添加依赖后再进行配置)
}
@RequestMapping("doReqJson02")
@ResponseBody
public List<Map<String,Object>> doReqJson02(){
List<Map<String,Object>> list = new ArrayList<Map<String, Object>>();
Map<String,Object> map = new HashMap<String, Object>();
map.put("id",10);
map.put("name","Alice");
list.add(map);
map = new HashMap<String, Object>();
map.put("id",11);
map.put("name","Frank");
list.add(map);
return list;
// http://localhost:8080/ai/resp/doReqJson02
//结果:[{"name":"Alice","id":10},{"name":"Frank","id":11}]
// 若不使用第三方
![在这里插入图片描述](https://img-blog.csdnimg.cn/20190305190126406.png)
}
@RequestMapping("doReqJson03")
@ResponseBody
public SysLog doReqJson03() throws Exception {
SysLog log=new SysLog ();
log.setId(100);
log.setUsername("CCC");
log.setIp("192.168.11.11");
log.setOperation("登陆");
log.setTime(System.currentTimeMillis());
log.setCreatedTime(new Date());
return log;
// 底层将此对象转换为JSON串时会调用对象的get方法获取值
// http://localhost:8080/ai/resp/doReqJson03
// 结果:{"id":100,"username":"CCC","operation":"登陆","method":null,"params":null,"time":1551781968344,"ip":"192.168.11.11","createdTime":1551781968344}
}
// @RequestMapping(value = "doReqJson04",produces = "application/json;charset=utf-8")
@RequestMapping(value = "doReqJson04",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@ResponseBody
public String doReqJson04() throws Exception {
SysLog log=new SysLog ();
log.setId(100);
log.setUsername("CCC");
log.setIp("192.168.11.11");
// 对象-》json字符串 会出现乱码 若不加 【,produces = "application/json;charset=utf-8"】
log.setOperation("登陆");
log.setTime(System.currentTimeMillis());
log.setCreatedTime(new Date());
// 借助第三方API(例如本例子使用的jackon)将对象转为字符串
ObjectMapper om = new ObjectMapper(); //方法来自 Jackson API
String s = om.writeValueAsString(log);
return s;
// 底层将此对象转换为JSON串时会调用对象的get方法获取值
// http://localhost:8080/ai/resp/doReqJson03
// 结果:{"id":100,"username":"CCC","operation":"登陆","method":null,"params":null,"time":1551781968344,"ip":"192.168.11.11","createdTime":1551781968344}
// 添加@RequestMapping(value = "doReqJson04",produces = "application/json;charset=utf-8")后
// 结果:{"id":100,"username":"CCC","operation":"登陆","method":null,"params":null,"time":1551782730183,"ip":"192.168.11.11","createdTime":1551782730184}
// 若 ,produces = "application/json;charset=utf-8" 写错
// 则会将请求信息下载至本地
//
// 为了防止写错 可以将,produces=MediaType.APPLICATION_JSON_UTF8_VALUE
}