SpringMVC 数据响应方式
- 页面跳转
- 返回字符穿
- 通过 ModelAndView 对象返回
- 回写数据
- 直接返回字符串
- 返回对象或集合
页面跳转
返回字符串
直接返回字符串:此种方式会将返回的字符串与视图解析器的前后缀拼接后跳转。
返回带有前缀的字符串
转发:forward:/WEB-INF/views/index.jsp
重定向:redirect:/index.jsp
返回 ModelAndView 对象
@RequestMapping(value = "/quick2")
private ModelAndView save2(){
ModelAndView modelAndView = new ModelAndView();
/*
* Model 模型 作用:封装数据
* View 视图 作用:展示数据
* */
//设置视图名称
modelAndView.setViewName("success");
//设置模型数据
modelAndView.addObject("username","aaa");
return modelAndView;
}
@RequestMapping(value = "/quick3")
//这里参数容器会自动注入
private ModelAndView save3(ModelAndView modelAndView){
modelAndView = new ModelAndView();
/*
* Model 模型 作用:封装数据
* View 视图 作用:展示数据
* */
//设置视图名称
modelAndView.setViewName("success");
//设置模型数据
modelAndView.addObject("username","bbb");
return modelAndView;
}
@RequestMapping(value = "/quick4")
private String save4(Model model){
model.addAttribute("username","黑马");
return "success";
}
@RequestMapping(value = "/quick5")
private String save5(HttpServletRequest request){
request.setAttribute("username","酷酷");
return "success";
}
回写数据
直接返回字符串
Web基础阶段,客户蟎访问服务器端,如果想直接回写字符串作为响应体返回的话,只需要使用
response getWriter().print( “helloworld” )即可,那么在Controller中想直接回写字符串该怎样呢?
-
通过 SpringMVC 框架注入 response 对象,使用 response.getWriter().print()
@RequestMapping(value = "/quick6") private void save6(HttpServletResponse httpServletResponse) throws IOException { httpServletResponse.getWriter().print("ddddddddd"); }
-
将需要回写的字符串直接返回,但此时需要通过
@ResponseBody
注解告知SpringMVC框架, 方法,返回的字符串不是跳转是直接在http响应体中返回。@RequestMapping(value = "/quick7") @ResponseBody//告诉 SpringMVC 不进行视图跳转,直接进行数据相应 private String save7( ) throws IOException { return "hello test" }
-
返回 JSON 数据格式
@RequestMapping(value = "/quick9") @ResponseBody private String save9( ) throws IOException { Account account = new Account(); account.setAccount(123456); account.setAge(18.0); //使用 json 转换工具,转换成 json 字符串 ObjectMapper objectMapper = new ObjectMapper(); String json = objectMapper.writeValueAsString(account); return json; }
返回对象或集合
-
返回对象或集合
要在 spring-mvc 配置适配器,spring MVC 会自动转为 json 字符串
<!--配置处理器映射器--> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"> <property name="messageConverters"> <list> <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> </bean> </list> </property> </bean>
代码使用得加 @ResponseBody ,就可以直接返回对象或者集合
@RequestMapping(value = "/quick10") @ResponseBody private Account save10( ) throws IOException { Account account = new Account(); account.setAccount(123456); account.setAge(18.0); return account; }
-
更简便的操作
在上面方法上添加 @ResponseBody 和配置 spring-mvc.xml 就可以返回json格式的字符串,但是这样配置比较麻烦,配置的代码比较多,
因此,我们可以使用mvc的注解驱动代替上述配置,在 spring-mvc.xml 配置一下如下代码(这里得有 spring 的 mvc 的命名空间)
<!--配置 mvc 注解驱动--> <mvc:annotation-driven></mvc:annotation-driven>
在 SpringMVC 的各个组件中,处理器映射器、处理器适配器、视圈解析器称为 SpringMVC 的三大组件。使用
<mvc:annotation-driven >
自动加载 RequestMappingHandlerMapping (处理映射器)和 RequestMappingHandlerAdapter (处理适配器),可用在 spring-mvc.xml 配置文件中使用<mvc:annotation-driven>
替代注解处理器和适配器的配置。 同时使用 <mvc:annotation driven> 默认底层就会集成jackson进行对象或集合的 json 格式字符串的转换。注意
上面介绍转换为 json 返回对象的几种方法,包括 ObjectMapper 、使用 mvc 注解驱动、在配置文件自己配置处理器映射器,都要记得导入下面三个依赖
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.9.0</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.0</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.9.0</version> </dependency>