一、SpringMVC的数据响应方式
1、 页面跳转
①返回字符串
②通过ModelAndView对象返回
2、回写数据
①直接返回字符串
②返回对象或集合
二、页面跳转
1、返回字符串
② 返回带有前缀的字符串,不再拼接上面的前后缀,用于指定页面跳转的途径:
转发:forward:/WEB-INF/views/index.jsp
重定向:redirect:/index.jsp
2、通过ModelAndView对象返回
①在方法中创建ModelAndView
@RequestMapping(value="/quick2")
public ModelAndView save2(){
/*
Model:模型 作用封装数据
View:视图 作用展示数据
*/
ModelAndView modelAndView = new ModelAndView();
//设置模型数据
modelAndView.addObject("username","itcast");
//设置视图名称
modelAndView.setViewName("success");
return modelAndView;
}
②设置方法参数为ModelAndView类型
@RequestMapping(value="/quick3")
public ModelAndView save3(ModelAndView modelAndView){
modelAndView.addObject("username","itheima");
modelAndView.setViewName("success");
return modelAndView;
}
③设置方法参数类型为Model,返回值为String
@RequestMapping(value="/quick4")
public String save4(Model model){
model.addAttribute("username","博学谷");
return "success";
}
3、向request域存储数据
① 通过SpringMVC框架注入的request对象setAttribute()方法设置
@RequestMapping("/quick5")
public String quickMethod(HttpServletRequest request){
request.setAttribute("name","zhangsan");
return "index";
}
@RequestMapping("/quick6")
public ModelAndView quickMethod3(){
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("forward:/WEB-INF/views/index.jsp");
modelAndView.addObject("username","lisi");
return modelAndView;
}
三、回写数据
1、直接返回字符串
Web基础阶段,客户端访问服务器端,如果想直接回写字符串作为响应体返回的话,只需要使用
response.getWriter().print(“hello world”) 即可,那么在Controller中想直接回写字符串该怎样呢?
① 通过SpringMVC框架注入的response对象,使用response.getWriter().print(“hello world”) 回写数据,此时不需要视图跳转,业务方法返回值为void。
@RequestMapping(value="/quick6")
public void save6(HttpServletResponse response) throws IOException {
response.getWriter().print("hello itcast");
}
② 将需要回写的字符串直接作为返回值,但此时需要通过@ResponseBody注解告知SpringMVC框架,方法返回的字符串不是跳转,而是直接在http响应体中返回。
@RequestMapping(value="/quick7")
@ResponseBody //告知SpringMVC框架 不进行视图跳转 直接进行数据响应
public String save7() throws IOException {
return "hello itheima";
}
③返回json格式字符串
@RequestMapping(value="/quick8")
@ResponseBody
public String save8() throws IOException {
return "{\"username\":\"zhangsan\",\"age\":18}";
}
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.13.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.13.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.2.1</version>
</dependency>
通过jackson将对象转换为json格式字符串:
@RequestMapping(value="/quick9")
@ResponseBody
public String save9() throws IOException {
User user = new User();
user.setUsername("lisi");
user.setAge(30);
//使用json的转换工具将对象转换成json格式字符串在返回
ObjectMapper objectMapper = new ObjectMapper();
String json = objectMapper.writeValueAsString(user);
return json;
}
2. 返回对象或集合
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
</list>
</property>
</bean>
方法直接返回对象:
@RequestMapping(value="/quick10")
@ResponseBody
//期望SpringMVC自动将User转换成json格式的字符串
public User save10() throws IOException {
User user = new User();
user.setUsername("lisi2");
user.setAge(32);
return user;
}
但是配置消息转换器比较麻烦,配置的代码比较多, 因此,我们可以使用mvc的注解驱动代替上述配置:
<!--mvc的注解驱动,不要忘了声明mvc标签-->
<mvc:annotation-driven/>