一、新建一个web项目
二、导入相关jar包
包括Spring所有包+
commons-logging-1.1.1.jar
jquery-3.1.1.min.js
jackson-annotations-2.8.0.jar
jackson-core-2.8.1.jar
jackson-databind-2.8.1.jar
三、编写web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>SpringServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>hello.do</welcome-file>
</welcome-file-list>
</web-app>
四、在src目录下新建一个SpringServlet-servlet.xml的配置文件(
命名方式为web.xml中定义的
DispatcherServlet名-servlet.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.zscs.controller"/>
<!-- 配置渲染器 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<!-- 结果是视图的前缀 -->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!-- 结果是视图的后缀 -->
<property name="suffix" value=".jsp"/>
</bean>
<!-- 配置文件上传类 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="utf-8"/>
<property name="maxUploadSize" value="10485760000"/>
<property name="maxInMemorySize" value="40960"/>
</bean>
<!-- 配置StringHttpMessageConverter -->
<bean id="StringHttpMessageConverter"
class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/plain;charset=UTF-8</value>
</list>
</property>
</bean>
<!-- 配置MappingJackson2HttpMessageConverter -->
<bean id="MappingJackson2HttpMessageConverter"
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
</bean>
<!-- 用于对象转成json -->
<bean id="AnnotationMethodHandlerAdapter"
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="StringHttpMessageConverter"/>
<ref bean="MappingJackson2HttpMessageConverter"/>
</list>
</property>
</bean>
</beans>
五、在WEB-INFO
目录下新建一个文件夹文件夹名为jsp与上述渲染器配置中结果视图的前缀保持一致,并且在jsp目录下新建一个jsp文件。
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript" src="js/jquery-3.1.1.min.js"></script>
<script type="text/javascript">
$(function(){
$("#btn").click(function(){
$.post("json.do",function(data){
var html="";
for(var i=0;i<data.length;i++){
html+="<tr><td>"+data[i].id+"</td><td>"+data[i].name+"</td><td>"+data[i].sex+"</td></tr>";
}
$("#tt").html(html);
});
});
});
</script>
</head>
<body>
<input type="button" id="btn" value="获取数据">
<table >
<tr><th>编号</th><th>姓名</th><th>性别</th></tr>
<tbody id="tt"></tbody>
</table>
</body>
</html>
六、编写实体类User.java
package com.zscs.entity;
public class User {
private int id;
private String name;
private String sex;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", sex=" + sex + "]";
}
public User(int id, String name, String sex) {
super();
this.id = id;
this.name = name;
this.sex = sex;
}
public User() {
super();
}
}
六、编写上传控制器类JsonController
package com.zscs.controller;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.zscs.entity.User;
@Controller
public class JsonController {
@RequestMapping("/json")
@ResponseBody
public List<User> json(){
List<User> list=new ArrayList<User>();
User user1=new User(0, "张三", "男");
User user2=new User(1, "李四", "男");
User user3=new User(2, "王五", "男");
list.add(user1);
list.add(user2);
list.add(user3);
return list;
}
}