所需Jar包jackson-core、jackson-annotations和jackson-databind
在MVC的配置文件中加入
一、 pom.xml
<properties>
<project.build.sourceEncoding>UTF-</project.build.sourceEncoding>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.spring.platform</groupId>
<artifactId>platform-bom</artifactId>
<version>2.0..RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<finalName>activiti</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19</version>
<configuration>
<!-- Maven 跳过运行 Test 代码的配置 -->
<skipTests>true</skipTests>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<!-- Junit 4.12 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<!-- javax.servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>javax.servlet.jsp.jstl-api</artifactId>
</dependency>
<!-- EL -->
<dependency>
<groupId>javax.el</groupId>
<artifactId>javax.el-api</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.el</artifactId>
<scope>test</scope>
</dependency>
<!-- Log4j 1.2. -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</dependency>
<!-- Spring 4.2..RELEASE -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-messaging</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
</dependency>
<!-- Jackson -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<!-- Commons -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</dependency>
<!-- Mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- c3p0 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
</dependency>
<!-- hibernate-validator -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
</dependency>
</dependencies>
二、web.xml
<!-- 指定spring相关文件的位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-core.xml</param-value>
</context-param>
<!-- 配置字符集过滤器 -->
<!-- 必须配置在所有过滤器的前面 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-</param-value>
</init-param>
</filter>
<!-- 配置项目的编码mapping -->
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/\*</url-pattern>
</filter-mapping>
<!-- 开启spring功能 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 防止内存溢出监听器 -->
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<!-- 配置spring mvc -->
<servlet>
<servlet-name>springMvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- 配置spring mvc mapping -->
<servlet-mapping>
<servlet-name>springMvc</servlet-name>
<url-pattern>\*.action</url-pattern>
</servlet-mapping>
<!-- 配置session超时时间,单位分钟 -->
<session-config>
<session-timeout>15</session-timeout>
</session-config>
<!-- 设置欢迎页面 -->
<welcome-file-list>
<welcome-file>/index.jsp</welcome-file>
</welcome-file-list>
<!-- 找不到页错误转向 -->
<error-page>
<error-code>404</error-code>
<location>/error/404.jsp</location>
</error-page>
<!-- 系统内部错误转向 -->
<error-page>
<error-code>500</error-code>
<location>/error/500.jsp</location>
</error-page>
三、Spring与SpringMVC的配置文件
1、Spring配置文件
<!-- 配置自动扫描的包 -->
<context:component-scan base-package="cn.mcs">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
<context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
</context:component-scan>
2、SpringMVC的配置文件
<!-- 配置自动扫描的包,只扫描以下两种注解的文件 -->
<context:component-scan base-package="cn.mcs" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
<context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
</context:component-scan>
<!-- 配置视图解析器 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- 静态资源处理 -->
<mvc:default-servlet-handler/>
<mvc:annotation-driven></mvc:annotation-driven>
四、Controller代码
package cn.mcs.action;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import cn.mcs.entity.Employee;
@RequestMapping("/employee")
@Controller
public class EmployeeAction {
@ResponseBody
@RequestMapping("/getEmployee")
public Employee getEmployee() {
Employee employee = new Employee(, "张三", "", 5600d, new Date());
return employee;
}
@ResponseBody
@RequestMapping("/getEmployeeList")
public List<Employee> getEmployeeList() {
List<Employee> employees = new ArrayList<Employee>();
employees.add(new Employee(, "张三", "", 5600d, new Date()));
employees.add(new Employee(, "李四", "", 4600d, new Date()));
employees.add(new Employee(, "王二麻", "", 6600d, new Date()));
return employees;
}
@ResponseBody
@RequestMapping("/getEmployeeMap")
public Map<String, Object> getEmployeeMap() {
List<Employee> employees = new ArrayList<Employee>();
employees.add(new Employee(, "张三", "", 5600d, new Date()));
employees.add(new Employee(, "李四", "", 4600d, new Date()));
employees.add(new Employee(, "王二麻", "", 6600d, new Date()));
Map<String, Object> map = new LinkedHashMap<String,Object>();
map.put("total", employees.size());
map.put("rows", employees);
return map;
}
@RequestMapping("/toEmployeeListPage")
public String toEmployeeListPage() {
return "employee/employeeList";
}
}
五、Web页面代码
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<script type="text/javascript">
$("#dg").datagrid({
url: "${pageContext.request.contextPath }/employee/getEmployeeMap.action",
pagination: true,
singleSelect: true,
rownumbers: true,
columns:\[\[
{field:'id',title:'ID',width:,align:'center'},
{field:'name',title:'职员名称',width:,align:'center'},
{field:'sex',title:'性别',width:,
formatter: function(value){
if (value == "") {
return "男";
} else {
return "女";
}
}},
{field:'salary',title:'薪水',width:,
formatter: function(value){
if (value == null) {
return "¥"+;
} else {
return "¥"+value;
}
}},
{field:'hireDate',title:'入职日期',width:,
formatter: function (value, row, index) {
var date = new Date(value);
var datetime = date.getFullYear()+"年"+(date.getMonth()+)+"月"+date.getDate()+"日";
return datetime;
}}
\]\],
});
</script>
手机扫一扫
移动阅读更方便
你可能感兴趣的文章