源代码
https://download.csdn.net/download/qzc70919700/19931374
一、<mvc:annotation-driven/>
<mvc:annotation-driven/>可以帮助自动创建注解处理器映射器RequestMappingHandlermapping,注解处理器适配器RequestMappingHandlerAdapter。提供对json格式的支持。
在springmvc.xml文件中配置
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<context:component-scan base-package="demo.mvc"></context:component-scan>
<!--
<mvc:annotation-driven/>可以帮助自动创建
注解处理器映射器RequestMappingHandlermapping,
注解处理器适配器RequestMappingHandlerAdapter;
并且提供对json格式的支持。
-->
<mvc:annotation-driven/>
<!-- 配置sprigmvc视图解析器:解析逻辑试图 后台返回逻辑试图:index 视图解析器解析出真正物理视图:前缀+逻辑试图+后缀====/WEB-INF/jsps/index.jsp -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/jsps/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
二、测试
2.1、编码,尝试请求响应JSON格式的实体
package demo.mvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import demo.mvc.entity.User;
@Controller
@RequestMapping("/test")
public class TestController {
@RequestMapping("/t1.do")
public @ResponseBody User test1() {
User user = new User("张三", "男", 18);
return user;
}
}
测试: