<!--springMVC的视图解析器, -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
springMVC的数据响应的方式
分两种
一种是页面跳转
一种是回写数据
页面跳转包括直接返回字符串, 通过ModelAndView对象返回
回写数据包括直接返回字符串, 返回对象或者集合.
一. 页面跳转的直接返回字符串
此种方式会将返回的字符串与视图解析器的前后缀拼接后跳转
例如
小结: forward是转发可以不写, redirect重定向就一定要写
二. 页面跳转的返回ModelAndView对象
@RequestMapping("/save2")
public ModelAndView save2(){
ModelAndView modelAndView = new ModelAndView();
//设置视图名称
modelAndView.setViewName("success");
//设置模型数据 , 其实就是放入request域
modelAndView.addObject("username","itdong");
return modelAndView;
}
或者这样写
@RequestMapping("/save2")
public ModelAndView save2(ModelAndView modelAndView){
//设置视图名称
modelAndView.setViewName("success");
//设置模型数据 , 其实就是放入request域
modelAndView.addObject("username","itdong");
return modelAndView;
}
@RequestMapping("/save2")
public ModelAndView save2(Model model){
//设置模型数据 , 其实就是放入request域
modelAndView.addObject("username","itdong");
return "success";
}
三. 回写数据的直接返回字符串
- 通过SpringMVC框架注入response对象, 使用response.getWriter().print(“XXX”) 回写数据, 注意此时不用视图跳转, 业务方法返回值是void
- 将需要回写的字符串直接返回, 但此时需要有一个注解ResponseBody让框架识别出来, return的不是视图名, 也就是不是页面跳转, 是直接在响应体中返回
- 使用json返回
<!--导入json相关的jar包, 有三个-->
<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>
四. 回写数据的返回对象或者集合
- 期望能自己转成json格式
<!--配置处理器的映射器, 为了能回写数据是集合或者对象时, 能够自动转json-->
<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>
2. 这样配置代码有点多, 不方便, 因此, 我们可以使用MVC的注解驱动来代替上诉的配置
使用mvc:annotation-driven/自动加载处理器映射器, 处理器适配器, 同时底层默认就会集成jackson进行json数据格式的转化
注意: 别忘了引入mvc命名空间
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--Controller的组件扫描-->
<context:component-scan base-package="controller"/>
<!--springMVC的视图解析器, -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!--<!–配置处理器的映射器, 为了能回写数据是集合或者对象时, 能够自动转json–>-->
<!--<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>-->
<!--mvc的注解驱动, 为了代替以上处理器映射器的配置-->
<mvc:annotation-driven/>
</beans>