SpringMVC向前台传输 JSON数据

所需Jar包jackson-core、jackson-annotations和jackson-databind

在MVC的配置文件中加入<mvc:annotation-driven></mvc:annotation-driven>

一、 pom.xml

  1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  2     <modelVersion>4.0.0</modelVersion>
  3     <groupId>cn.mcs</groupId>
  4     <artifactId>springmvc02</artifactId>
  5     <version>0.0.1-SNAPSHOT</version>
  6     <packaging>war</packaging>
  7 
  8     <properties>
  9         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 10     </properties>
 11 
 12     <dependencyManagement>
 13         <dependencies>
 14             <dependency>
 15                 <groupId>io.spring.platform</groupId>
 16                 <artifactId>platform-bom</artifactId>
 17                 <version>2.0.0.RELEASE</version>
 18                 <type>pom</type>
 19                 <scope>import</scope>
 20             </dependency>
 21         </dependencies>
 22     </dependencyManagement>
 23 
 24     <build>
 25         <finalName>activiti</finalName>
 26         <plugins>
 27             <plugin>
 28                 <groupId>org.apache.maven.plugins</groupId>
 29                 <artifactId>maven-surefire-plugin</artifactId>
 30                 <version>2.19</version>
 31                 <configuration>
 32                     <!-- Maven 跳过运行 Test 代码的配置 -->
 33                     <skipTests>true</skipTests>
 34                 </configuration>
 35             </plugin>
 36         </plugins>
 37     </build>
 38 
 39     <dependencies>
 40         <!-- Junit 4.12 -->
 41         <dependency>
 42             <groupId>junit</groupId>
 43             <artifactId>junit</artifactId>
 44             <scope>test</scope>
 45         </dependency>
 46         <!-- javax.servlet -->
 47         <dependency>
 48             <groupId>javax.servlet</groupId>
 49             <artifactId>javax.servlet-api</artifactId>
 50             <scope>provided</scope>
 51         </dependency>
 52         <dependency>
 53             <groupId>javax.servlet.jsp</groupId>
 54             <artifactId>javax.servlet.jsp-api</artifactId>
 55             <scope>provided</scope>
 56         </dependency>
 57         <dependency>
 58             <groupId>javax.servlet</groupId>
 59             <artifactId>jstl</artifactId>
 60         </dependency>
 61         <dependency>
 62             <groupId>javax.servlet.jsp.jstl</groupId>
 63             <artifactId>javax.servlet.jsp.jstl-api</artifactId>
 64         </dependency>
 65         <!-- EL -->
 66         <dependency>
 67             <groupId>javax.el</groupId>
 68             <artifactId>javax.el-api</artifactId>
 69         </dependency>
 70         <dependency>
 71             <groupId>org.glassfish</groupId>
 72             <artifactId>javax.el</artifactId>
 73             <scope>test</scope>
 74         </dependency>
 75         <!-- Log4j 1.2.17 -->
 76         <dependency>
 77             <groupId>log4j</groupId>
 78             <artifactId>log4j</artifactId>
 79         </dependency>
 80 
 81         <!-- Spring 4.2.3.RELEASE -->
 82         <dependency>
 83             <groupId>org.springframework</groupId>
 84             <artifactId>spring-core</artifactId>
 85         </dependency>
 86         <dependency>
 87             <groupId>org.springframework</groupId>
 88             <artifactId>spring-context</artifactId>
 89         </dependency>
 90         <dependency>
 91             <groupId>org.springframework</groupId>
 92             <artifactId>spring-context-support</artifactId>
 93         </dependency>
 94         <dependency>
 95             <groupId>org.springframework</groupId>
 96             <artifactId>spring-orm</artifactId>
 97         </dependency>
 98         <dependency>
 99             <groupId>org.springframework</groupId>
100             <artifactId>spring-webmvc</artifactId>
101         </dependency>
102         <dependency>
103             <groupId>org.springframework</groupId>
104             <artifactId>spring-tx</artifactId>
105         </dependency>
106         <dependency>
107             <groupId>org.springframework</groupId>
108             <artifactId>spring-jdbc</artifactId>
109         </dependency>
110         <dependency>
111             <groupId>org.springframework</groupId>
112             <artifactId>spring-aspects</artifactId>
113         </dependency>
114         <dependency>
115             <groupId>org.springframework</groupId>
116             <artifactId>spring-messaging</artifactId>
117         </dependency>
118         <dependency>
119             <groupId>org.springframework</groupId>
120             <artifactId>spring-test</artifactId>
121         </dependency>
122         
123         <!-- Jackson -->
124         <dependency>
125             <groupId>com.fasterxml.jackson.core</groupId>
126             <artifactId>jackson-core</artifactId>
127         </dependency>
128         <dependency>
129             <groupId>com.fasterxml.jackson.core</groupId>
130             <artifactId>jackson-annotations</artifactId>
131         </dependency>
132         <dependency>
133             <groupId>com.fasterxml.jackson.core</groupId>
134             <artifactId>jackson-databind</artifactId>
135         </dependency>
136 
137         <!-- Commons -->
138         <dependency>
139             <groupId>commons-io</groupId>
140             <artifactId>commons-io</artifactId>
141         </dependency>
142         <dependency>
143             <groupId>commons-fileupload</groupId>
144             <artifactId>commons-fileupload</artifactId>
145         </dependency>
146         <dependency>
147             <groupId>commons-logging</groupId>
148             <artifactId>commons-logging</artifactId>
149         </dependency>
150 
151         <!-- Mysql -->
152         <dependency>
153             <groupId>mysql</groupId>
154             <artifactId>mysql-connector-java</artifactId>
155         </dependency>
156 
157         <!-- c3p0 -->
158         <dependency>
159             <groupId>com.mchange</groupId>
160             <artifactId>c3p0</artifactId>
161         </dependency>
162 
163         <!-- hibernate-validator -->
164         <dependency>
165             <groupId>org.hibernate</groupId>
166             <artifactId>hibernate-validator</artifactId>
167         </dependency>
168 
169 
170     </dependencies>
171 
172 
173 </project>
View Code

二、web.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
 3 <!-- 项目名称 -->
 4     <display-name>SSM</display-name>
 5 
 6     <!-- 指定spring相关文件的位置 -->
 7     <context-param>
 8         <param-name>contextConfigLocation</param-name>
 9         <param-value>classpath:spring-core.xml</param-value>
10     </context-param>
11 
12     <!-- 配置字符集过滤器 -->
13     <!-- 必须配置在所有过滤器的前面 -->
14     <filter>
15         <filter-name>encodingFilter</filter-name>
16         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
17         <init-param>
18             <param-name>encoding</param-name>
19             <param-value>UTF-8</param-value>
20         </init-param>
21     </filter>
22     <!-- 配置项目的编码mapping -->
23     <filter-mapping>
24         <filter-name>encodingFilter</filter-name>
25         <url-pattern>/*</url-pattern>
26     </filter-mapping>
27 
28     <!-- 开启spring功能 -->
29     <listener>
30         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
31     </listener>
32     <!-- 防止内存溢出监听器 -->
33     <listener>
34         <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
35     </listener>
36 
37     <!-- 配置spring mvc -->
38     <servlet>
39         <servlet-name>springMvc</servlet-name>
40         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
41         <init-param>
42             <param-name>contextConfigLocation</param-name>
43             <param-value>classpath:spring-mvc.xml</param-value>
44         </init-param>
45         <load-on-startup>1</load-on-startup>
46     </servlet>
47     <!-- 配置spring mvc mapping -->
48     <servlet-mapping>
49         <servlet-name>springMvc</servlet-name>
50         <url-pattern>*.action</url-pattern>
51     </servlet-mapping>
52 
53     <!-- 配置session超时时间,单位分钟 -->
54     <session-config>
55         <session-timeout>15</session-timeout>
56     </session-config>
57 
58     <!-- 设置欢迎页面 -->
59     <welcome-file-list>
60         <welcome-file>/index.jsp</welcome-file>
61     </welcome-file-list>
62 
63     <!-- 找不到页错误转向 -->
64     <error-page>
65         <error-code>404</error-code>
66         <location>/error/404.jsp</location>
67     </error-page>
68     <!-- 系统内部错误转向 -->
69     <error-page>
70         <error-code>500</error-code>
71         <location>/error/500.jsp</location>
72     </error-page>
73 </web-app>
View Code

三、Spring与SpringMVC的配置文件

1、Spring配置文件

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
 3         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
 4         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
 5         http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.8.xsd
 6         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">
 7     
 8     <!-- 配置自动扫描的包 -->
 9     <context:component-scan base-package="cn.mcs">
10         <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
11         <context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
12     </context:component-scan>    
13     
14 </beans>
View Code

2、SpringMVC的配置文件

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xmlns:context="http://www.springframework.org/schema/context"
 5     xmlns:mvc="http://www.springframework.org/schema/mvc"
 6     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
 7         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
 8         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd">
 9 
10     <!-- 配置自动扫描的包,只扫描以下两种注解的文件 -->
11     <context:component-scan base-package="cn.mcs" use-default-filters="false">
12         <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
13         <context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
14     </context:component-scan>
15     
16     <!-- 配置视图解析器 -->
17     <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
18         <property name="prefix" value="/WEB-INF/views/" />
19         <property name="suffix" value=".jsp" />
20     </bean>
21     
22     <!-- 静态资源处理 -->
23     <mvc:default-servlet-handler/>
24     
25     <mvc:annotation-driven></mvc:annotation-driven>
26     
27 
28 </beans>
View Code

四、Controller代码

 1 package cn.mcs.action;
 2 
 3 import java.util.ArrayList;
 4 import java.util.Date;
 5 import java.util.LinkedHashMap;
 6 import java.util.List;
 7 import java.util.Map;
 8 
 9 import org.springframework.stereotype.Controller;
10 import org.springframework.web.bind.annotation.RequestMapping;
11 import org.springframework.web.bind.annotation.ResponseBody;
12 
13 import cn.mcs.entity.Employee;
14 
15 @RequestMapping("/employee")
16 @Controller
17 public class EmployeeAction {
18     
19     @ResponseBody
20     @RequestMapping("/getEmployee")
21     public Employee getEmployee() {
22         Employee employee = new Employee(1, "张三", "1", 5600d, new Date());
23         
24         return employee;
25     }
26     
27     @ResponseBody
28     @RequestMapping("/getEmployeeList")
29     public List<Employee> getEmployeeList() {
30         List<Employee> employees = new ArrayList<Employee>();
31         employees.add(new Employee(1, "张三", "1", 5600d, new Date()));
32         employees.add(new Employee(2, "李四", "2", 4600d, new Date()));
33         employees.add(new Employee(3, "王二麻", "1", 6600d, new Date()));
34         
35         return employees;
36     }
37     
38     @ResponseBody
39     @RequestMapping("/getEmployeeMap")
40     public Map<String, Object> getEmployeeMap() {
41         List<Employee> employees = new ArrayList<Employee>();
42         employees.add(new Employee(1, "张三", "1", 5600d, new Date()));
43         employees.add(new Employee(2, "李四", "2", 4600d, new Date()));
44         employees.add(new Employee(3, "王二麻", "1", 6600d, new Date()));
45         
46         Map<String, Object> map = new LinkedHashMap<String,Object>();
47         map.put("total", employees.size());
48         map.put("rows", employees);
49         
50         return map;
51     }
52     
53     @RequestMapping("/toEmployeeListPage")
54     public String toEmployeeListPage() {
55         return "employee/employeeList";
56     }
57     
58     
59     
60 }
View Code

五、Web页面代码

 1 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
 2 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
 3 <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
 4 
 5 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 6 <html>
 7 <head>
 8 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 9 <title>Insert title here</title>
10 
11 <c:set var="path" value="${pageContext.request.contextPath}" />
12 <link rel="stylesheet" type="text/css" href="${path}/jslib/easyui/jquery-easyui-1.4.4/themes/default/easyui.css">
13 <link rel="stylesheet" type="text/css" href="${path}/jslib/easyui/jquery-easyui-1.4.4/themes/icon.css">
14 
15 <script type="text/javascript" src="${path}/jslib/easyui/jquery-easyui-1.4.4/jquery.min.js"></script>
16 <script type="text/javascript" src="${path}/jslib/easyui/jquery-easyui-1.4.4/jquery.easyui.min.js"></script>
17 <script type="text/javascript" src="${path}/jslib/easyui/jquery-easyui-1.4.4/locale/easyui-lang-zh_CN.js"></script>
18 
19 </head>
20 <body class="easyui-layout"  >
21      <div data-options="region:'center',fit:true,border:false" >  
22          <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-add'">新增</a>  
23         <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-edit'">修改</a> 
24         <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-remove'">删除</a> 
25         <table id="dg"></table>    
26     </div>       
27     
28     <script type="text/javascript">
29         $("#dg").datagrid({
30             url: "${pageContext.request.contextPath }/employee/getEmployeeMap.action",
31             pagination: true,
32             singleSelect: true,
33             rownumbers: true,
34             columns:[[    
35                       {field:'id',title:'ID',width:100,align:'center'},    
36                       {field:'name',title:'职员名称',width:100,align:'center'},    
37                       {field:'sex',title:'性别',width:100,
38                           formatter: function(value){
39                               if (value == "1") {
40                                   return "";
41                               } else {
42                                   return "";
43                               }
44                           }},  
45                       {field:'salary',title:'薪水',width:100,
46                               formatter: function(value){
47                                   if (value == null) {
48                                       return ""+0;
49                                   } else {
50                                       return ""+value;
51                                   }
52                               }},   
53                       {field:'hireDate',title:'入职日期',width:100,
54                           formatter: function (value, row, index) {
55                               var date = new Date(value);
56                               var datetime = date.getFullYear()+""+(date.getMonth()+1)+""+date.getDate()+"";
57                               return datetime;                             
58                       }}
59                   ]],
60         });
61     </script>
62 </body>
63 </html>
View Code

 

posted @ 2015-12-09 08:37  小个子猫  阅读(9116)  评论(0编辑  收藏  举报