数据响应方式
1.页面跳转
1.1返回字符串形式
1.2通过ModalAndView对象
2.回写数据
1.返回字符串
2.返回json格式字符串
pom.xml中:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.7</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.7</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.7</version>
</dependency>
报错的话,put into output root
3.返回对象或集合
springMVC自动将User转成json格式
spring-mvc.xml(繁琐)
<!--配置处理器映射器-->
<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>
简单方法:使用mvc的注解驱动代替上述配置
spring-mvc.xml(简单,推荐)
<!--mvc的注解驱动-->
<mvc:annotation-driven/>