采用Ajax的方式,来演示学生信息的添加、查看、删除简单功能.

使用技术:

1.Spring 4.3.3.RELEASE
2.Maven 3
3.JDK 1.7
4.Eclipse 4.4
5.Boostrap 3

一,项目结构

二,Maven 配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.devnp</groupId>
<artifactId>SpringMVC4AjaxExample</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>SpringMVC4AjaxExaple Maven Webapp</name>
<url>http://maven.apache.org</url>

<properties>
<project.name>SpringMVC4AjaxExample</project.name>
<jdk.version>1.7</jdk.version>
<spring.version>4.3.3.RELEASE</spring.version>
<jstl.version>1.2</jstl.version>
<jackson.version>2.7.5</jackson.version>
<servletapi.version>2.5</servletapi.version>
<sl4j.version>1.7.9</sl4j.version>
</properties>

<dependencies>
<!-- spring mvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>

<!-- json -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>

<!-- sl4j -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${sl4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${sl4j.version}</version>
</dependency>

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>${servletapi.version}</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>${jstl.version}</version>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>${jdk.version}</source>
<target>${jdk.version}</target>
<encoding>UTF-8</encoding>
<excludes>
<exclude>**/test/*.java</exclude>
</excludes>
</configuration>
</plugin>
<!-- embedded Jetty server, for testing -->
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.2.11.v20150529</version>
<configuration>
<scanIntervalSeconds>10</scanIntervalSeconds>
<webApp>
<contextPath>/${project.name}</contextPath>
</webApp>
<httpConnector>
<port>9999</port>
</httpConnector>
</configuration>
</plugin>
<!-- configure Eclipse workspace -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.9</version>
<configuration>
<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
<wtpversion>2.0</wtpversion>
<wtpContextName>${project.name}</wtpContextName>
</configuration>
</plugin>

</plugins>
<finalName>${project.name}</finalName>
</build>
</project>


三,控制器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92

package com.devnp.controller;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import com.devnp.model.JsonResult;
import com.devnp.model.Student;
import com.devnp.util.BuildResult;

@Controller
@EnableWebMvc
public class StudentController {

private List<Student> students = null ;

@RequestMapping(value="/")
public String index(){

return "index" ;
}

@RequestMapping(value="/findAllStudents", method=RequestMethod.POST)
@ResponseBody
public JsonResult<List<Student>> findAll(){

if(students == null){
students = new ArrayList<Student>();

Calendar calendar = Calendar.getInstance() ;

calendar.add(Calendar.YEAR, -18);

students.add(new Student("Lucy", "Famle", calendar.getTime(), 1));
students.add(new Student("Tom", "Male", calendar.getTime(), 2));
}

BuildResult<List<Student>> result = new BuildResult<List<Student>>();

return result.resultSuccess(students);
}

@RequestMapping(value="/addStudent", method=RequestMethod.POST)
@ResponseBody
public JsonResult<Student> addStudent(@RequestBody Student student){

BuildResult<Student> result = new BuildResult<Student>();

students.add(student);

return result.resultSuccess("Add Success!");
}

@RequestMapping(value="/delStudent/{id}", method=RequestMethod.GET)
@ResponseBody
public JsonResult<Student> delStudent(@PathVariable(value="id") Integer id){

for (int i = 0; i < students.size(); i++) {
if(students.get(i).getId().equals(id))
students.remove(i);
}

BuildResult<Student> result = new BuildResult<Student>();

return result.resultSuccess("Delete Success!");
}

@RequestMapping(value="/findStudent/{id}", method=RequestMethod.POST)
@ResponseBody
public JsonResult<Student> findStudent(@PathVariable(value="id") Integer id){

Student student = null ;
for (int i = 0; i < students.size(); i++) {
if(students.get(i).getId().equals(id))
student = students.get(i);
}

BuildResult<Student> result = new BuildResult<Student>();

return result.resultSuccess(student);
}
}

四,页面

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<spring:url value="/resources/css/bootstrap.min.css" var="bootstrapCss" />
<link href="${bootstrapCss}" rel="stylesheet" />
<spring:url value="/resources/css/bootstrap-datetimepicker.min.css" var="datetimepickerCss" />
<link href="${datetimepickerCss}" rel="stylesheet" />
<spring:url value="/resources/js/jquery-1.12.4.min.js" var="jqueryJs" />
<script src="${jqueryJs}"></script>
<spring:url value="/resources/js/bootstrap.min.js" var="jqueryJs" />
<script src="${jqueryJs}"></script>
<spring:url value="/resources/js/bootstrap-datetimepicker.min.js" var="datetimepickerJs" />
<script src="${datetimepickerJs}"></script>

<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Hello, World.</title>
</head>
<body>
<div class="container">
<table class="table table-striped">
<caption>学生信息</caption>
<thead>
<tr>
<th>ID</th>
<th>姓名</th>
<th>生日</th>
<th>性别</th>
<th>操作</th>
</tr>
</thead>
<tbody id="students">

</tbody>
</table>

<button type="button" class="btn btn-success" data-title="add" data-toggle="modal" data-target="#add">添加学生</button>
</div>

<div id="my-modal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="view" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true"><span class="glyphicon glyphicon-remove" aria-hidden="true"></span></button>
<h4 class="modal-title custom_align" id="Heading">学生信息</h4>
</div>
<div class="modal-body">
<div class="row">
<div class="col-md-6">姓名</div>
<div class="col-md-6" id="sName"></div>
</div>
<div class="row">
<div class="col-md-6">生日</div>
<div class="col-md-6" id="sBirthDay"></div>
</div>
<div class="row">
<div class="col-md-6">性别</div>
<div class="col-md-6" id="sSex"></div>
</div>
</div>
</div>
</div>
</div>

<div id="my-modal2" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="view" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true"><span class="glyphicon glyphicon-remove" aria-hidden="true"></span></button>
<h4 class="modal-title custom_align" id="Heading">学生信息</h4>
</div>
<div class="modal-body">
删除成功!
</div>
</div>
</div>
</div>

<div class="modal fade" id="add" tabindex="-1" role="dialog" aria-labelledby="add" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true"><span class="glyphicon glyphicon-remove" aria-hidden="true"></span></button>
<h4 class="modal-title custom_align" id="Heading">学生信息</h4>
</div>
<div class="modal-body">
<div class="form-group">
<input class="form-control " type="text" placeholder="姓名" id="addSName">
</div>
<div class="form-group">

<div class="input-append date form_datetime">
<input size="16" type="text" value="" readonly id="addSSex">
<span class="add-on"><i class="icon-th"></i></span>
</div>
</div>
<div class="form-group">
<div class="radio">
<label>
<input type="radio" name="sSex" id="optionsRadios1" value="Famle">Famle
</label>
</div>
<div class="radio">
<label>
<input type="radio" name=sSex id="optionsRadios2" value="Male">Male
</label>
</div>
</div>
</div>
<div class="modal-footer ">
<button type="button" class="btn btn-warning btn-lg" style="width: 100%;" id="addStudents">添加</button>
</div>
</div>
</div>
</div>

<script>
jQuery(document).ready(function($) {

var data = {}
$.ajax({
type : "POST",
contentType : "application/json",
url : "http://localhost:7001/SpringMVC4AjaxExample/findAllStudents",
data : JSON.stringify(data),
dataType : 'json',
timeout : 100000,
success : function(data) {
$.each(data.result, function(index, element) {
$("#students").append(
"<tr><td>" + element.id + "</td>" +
"<td>" + element.sName + "</td>" +
"<td>" + element.sBirthDay + "</td>" +
"<td>" + element.sSex + "</td>" +
"<td>" + "<a href='javascript:viewStudents(" + element.id + ")' >查看</a>" + "&amp;nbsp"+ "<a href='javascript:deleteStudents(" + element.id + ")'>删除</a>" + "<td></tr>");
});

console.log("SUCCESS: ", data);
},
error : function(e) {
console.log("ERROR: ", e);
},
done : function(e) {
console.log("DONE");
}
});

$(".form_datetime").datetimepicker({
format: "yyyy-mm-dd"
});

$("#addStudents").click(function(){
var sName = $("#addSName").val();
var sBrith = $("#addSSex").val();
var sSex = $('input[name=sSex]:checked').val();

var data = {"id":3,"sName":sName,"sSex":sSex,"sBirthDay":sBrith}

alert(sSex);
$.ajax({
type : "POST",
contentType : "application/json",
url : "http://localhost:7001/SpringMVC4AjaxExample/addStudent",
data : JSON.stringify(data),
dataType : 'json',
timeout : 100000,
success : function(data) {
var statusCode = data.statusCode ;

if(statusCode == 200){
location.reload();
$('#add').modal('hide');
}

console.log("SUCCESS: ", data);
},
error : function(e) {
console.log("ERROR: ", e);
},
done : function(e) {
console.log("DONE");
}
});

});
});

function viewStudents(id){
var data = {}
$.ajax({
type : "POST",
contentType : "application/json",
url : "http://localhost:7001/SpringMVC4AjaxExample/findStudent/"+id,
data : JSON.stringify(data),
dataType : 'json',
timeout : 100000,
success : function(data) {
var result = data.result ;

$("#sName").empty();
$("#sBirthDay").empty();
$("#sSex").empty();

$("#sName").append(result.sName);
$("#sBirthDay").append(result.sBirthDay);
$("#sSex").append(result.sSex);

console.log("SUCCESS: ", data);
},
error : function(e) {
console.log("ERROR: ", e);
},
done : function(e) {
console.log("DONE");
}
});

$('#my-modal').modal('show');
}

function deleteStudents(id){
var data = {}
$.ajax({
type : "GET",
contentType : "application/json",
url : "http://localhost:7001/SpringMVC4AjaxExample/delStudent/"+id,
data : JSON.stringify(data),
dataType : 'json',
timeout : 100000,
success : function(data) {
var statusCode = data.statusCode ;

if(statusCode == 200){
//$('#my-modal2').modal('show');
location.reload();
}

console.log("SUCCESS: ", data);
},
error : function(e) {
console.log("ERROR: ", e);
},
done : function(e) {
console.log("DONE");
}
});
}
</script>
</body>
</html>

五,测试

1.访问 http://localhost:7001/SpringMVC4AjaxExample/

2.点击查看按钮

3.点击删除按钮

4.点击添加按钮

六,代码下载

SpringMVC4AjaxExample.zip

注:在页面js还很粗糙,有待修好