springMVC的控制器,也就是@Control类中的方法默认返回String类型的一个url地址,可以在操作完成后直接跳到指定页面。然而在大部分开发过程中前后端是分离的,后端研发人员并不清楚前端是什么页面。这时候我们就需要将运行结果以JSON数据返回。前端在想要输出数据的位置获取后端返回的JSON,便完成了一整套WEB开发。
1.准备JSON支持的JAR包(这里使用的fastJson)
fastjson-1.2.31.jar
2.配置spring-mvc.xml(spring配置文件有些是applicationContext.xml)
<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" xmlns:p="http://www.springframework.org/schema/p"
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.spring