文章目录
1 SpringMVC的数据响应
1.1 SpringMVC的数据响应方式
SpringMVC的数据响应方式有两种:页面跳转和回写数据
页面跳转
1,返回字符串
2,通过ModelAndView对象返回
回写数据
1,返回字符串
2,返回对象或集合
1.2 页面跳转
1.2.1 返回字符串
// 页面跳转-返回字符串
@RequestMapping(value = "/start")
public String save() {
// 直接返回视图
return "success";
}
此种方式会将返回的字符串与视图解析器的前后缀拼接后跳转
上述的示例就是页面跳转的返回字符串的形式,返回了/jsp/success.jsp页面,还可以在字符串前增加forward:(请求转发)或redirect:(请求重定向)
1.2.2 通过ModelAndView对象返回
ModelAndView是一个对象,Model是模型用于封装数据,View是视图用于展示数据
1,创建并设置一个ModelAndView对象并返回:
// 页面跳转-返回ModelAndView对象(1)
@RequestMapping("/start2")
public ModelAndView save2() {
ModelAndView modelAndView = new ModelAndView();
// 设置视图名称
modelAndView.setViewName("success");
// 设置模型数据 将数据以键值对的形式存储 可以在jsp页面中取出并展示
modelAndView.addObject("userName", "coisini");
return modelAndView;
}
2,使用ModelAndView参数并返回:
// 页面跳转-返回ModelAndView对象(2)
@RequestMapping("/start3")
public ModelAndView save3(ModelAndView modelAndView) {
modelAndView.setViewName("success");
modelAndView.addObject("userName", "when");
return modelAndView;
}
这种方式使用了参数中的modelAndView对象,但回顾整个过程,并没有直接传递这个ModelAndView参数的行为,实际上是SpringMVC检测到了这里的ModelAndView参数,自动注入了一个ModelAndView对象
可以在方法的形参中使用常见的参数,并在方法内直接使用,这些参数会由SpringMVC自动注入
1.3 回写数据
1.3.1 返回字符串
在Web基础阶段,如果想直接回传字符串作为响应体返回的话,只需要使用response.getWriter().print(“xxx”)即可,在SpringMVC的Controller中想要回传字符串可以:
1,通过SpringMVC框架注入的response对象,使用response.getWriter().print("xxx")
2,直接返回字符串,但需要通过@ResponseBody注解告知SpringMVC框架
该方法返回的字符串不是跳转页面而是作为response响应
1,通过response对象返回字符串:
// 回传数据-返回字符串-通过response对象
@RequestMapping("/start4")
public void save4(HttpServletResponse response) throws IOException {
response.getWriter().println("Hi SpringMVC");
}
2,通过@ResponseBody注解直接返回字符串:
// 回传数据-返回字符串-直接返回
@RequestMapping("/start5")
@ResponseBody
public String save5() {
return "Hi SpringMVC";
}
当需要回传json格式的字符串时,也可以通过@ResponseBody注解直接返回:
// 回传数据-返回字符串-返回json格式字符串
@RequestMapping("/start7")
@ResponseBody
public String save7() throws JsonProcessingException {
User user = new User();
user.setUserName("coisini");
user.setAge(21);
// 使用json转换工具将对象转换成json格式的字符串再返回
ObjectMapper objectMapper = new ObjectMapper();
String json = objectMapper.writeValueAsString(user);
return json;
}
需要用到jackson相关jar包:
<!-- json转换工具jackson -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.11.0</version>
</dependency>
<!-- jackson数据绑定工具 -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.11.0</version>
</dependency>
<!-- jackson注解相关工具 -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.11.0</version>
</dependency>
1.3.2 返回对象或集合
当需要把对象或集合返回时,需要将其转换成json格式的字符串,通过配置处理器映射器,可以在代码中直接返回对象,SpringMVC会自动根据配置将其转换为json字符串
spring-mvc.xml中处理器映射器的配置:
<!-- 配置处理器映射器 能将返回的对象通过jackson转换成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>
// 回传数据-返回对象-需要预先配置处理器映射器
// 这样就避免了自己先转换成json字符串再返回的繁琐过程
@RequestMapping("/start8")
@ResponseBody
public User save8() {
User user = new User();
user.setUserName("coisini");
user.setAge(21);
return user;
}
自己配置处理器映射器较为麻烦,为此可以使用mvc的注解驱动代替:
在spring-mvc.xml中新增mvc命名空间:
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
</beans>
在spring-mvc.xml中增加mvc的注解驱动:
<!-- mvc注解驱动 将默认使用jackson进行对象或集合的json字符串格式转换 -->
<mvc:annotation-driven></mvc:annotation-driven>
使用mvc:annotation-driver会将底层集成jackson进行对象或集合的json格式字符串转换,使用该配置就不需要手动配置处理器适配器