@RequestMapping("/list")
public String getList(ModelMap map) {
List<Student> students = userService.getTotal();
map.addAttribute("students", students);
// System.out.println(userService.getTotal());
return "/index.jsp";
}
@RequestMapping("/addStudent")
public String addStudent(HttpServletRequest request,
HttpServletResponse response) {
Student student = new Student();
int studentID = Integer.parseInt(request.getParameter("student_id"));
String name = request.getParameter("name");
int age = Integer.parseInt(request.getParameter("age"));
String sex = request.getParameter("sex");
Date birthday = null;
// String 类型按照 yyyy-MM-dd 的格式转换为 java.util.Date 类
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
try {
birthday = simpleDateFormat.parse(request.getParameter("birthday"));
} catch (ParseException e) {
e.printStackTrace();
}
System.out.println(userService.getTotal());
student.setStudent_id(studentID);
student.setName(name);
student.setAge(age);
student.setSex(sex);
student.setBirthday(birthday);
userService.addStudent(student);
return "redirect:index.jsp";
}
@RequestMapping("/deleteStudent")
public String deleteStudent(int id) {
userService.deleteStudent(id);
return "redirect:index.jsp";
}
@RequestMapping("/editStudent")
public ModelAndView editStudent(int id) {
ModelAndView mav = new ModelAndView("editStudent");
Student student = userService.getStudent(id);
mav.addObject("student", student);
return mav;
}
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script src="jquery-2.1.1.min.js"></script>
<script src="bootstrap.min.js"></script>
<title>学生管理页面 - 首页</title>
<script>
$(function() {
$("ul.pagination li.disabled a").click(function() {
return false;
});
});
function del() {
var msg = "您真的确定要删除吗?\n\n请确认!";
if (confirm(msg) == true) {
return true;
} else {
return false;
}
}
</script>
</head>
<body>
<div class="listDIV">
<table border="1" width="50%" height="50%" style="text-align: center;">
<tr>
<th>学号</th>
<th>姓名</th>
<th>年龄</th>
<th>性别</th>
<th>出生日期</th>
<th>操作</th>
</tr>
<c:forEach items="${students}" var="s" varStatus="status">
<tr>
<td>${s.student_id}</td>
<td>${s.name}</td>
<td>${s.age}</td>
<td>${s.sex}</td>
<td>${s.birthday}</td>
<td><a href="/editStudent?id=${s.id}"><span
class="glyphicon glyphicon-edit"></span> 修改</a> <a
href="/deleteStudent?id=${s.id}"
onclick="javascript:return del();"><span
class="glyphicon glyphicon-trash"></span> 删除</a></td>
</tr>
</c:forEach>
</table>
</div>
<a href="addStudent.jsp">添加学生</a>
</body>
</html>
<div class="addDIV">
<div class="panel panel-success">
<div class="panel-heading">
<h3 class="panel-title">添加学生</h3>
</div>
<div class="panel-body">
<form method="post" action="/addStudent" role="form">
<table class="addTable">
<tr>
<td>学号:</td>
<td><input type="text" name="student_id" id="student_id"
placeholder="请在这里输入学号"></td>
</tr>
<tr>
<td>姓名:</td>
<td><input type="text" name="name" id="name"
placeholder="请在这里输入名字"></td>
</tr>
<tr>
<td>年龄:</td>
<td><input type="text" name="age" id="age"
placeholder="请在这里输入年龄"></td>
</tr>
<tr>
<td>性别:</td>
<td><input type="radio" class="radio radio-inline"
name="sex" value="男"> 男 <input type="radio"
class="radio radio-inline" name="sex" value="女"> 女</td>
</tr>
<tr>
<td>出生日期:</td>
<td><input type="date" name="birthday" id="birthday"
placeholder="请在这里输入出生日期"></td>
</tr>
<tr class="submitTR">
<td colspan="2" align="center">
<button type="submit" class="btn btn-success">提 交</button>
</td>
</tr>
</table>
</form>
</div>
</div>
</div>
<div class="editDIV">
<div class="panel panel-success">
<div class="panel-heading">
<h3 class="panel-title">编辑学生</h3>
</div>
<div class="panel-body">
<form method="post" action="/updateStudent" role="form">
<table class="editTable">
<tr>
<td>学号:</td>
<td><input type="text" name="student_id" id="student_id" value="${student.student_id}"
placeholder="请在这里输入学号"></td>
</tr>
<tr>
<td>姓名:</td>
<td><input type="text" name="name" id="name" value="${student.name}" placeholder="请在这里输入名字">
</td>
</tr>
<tr>
<td>年龄:</td>
<td><input type="text" name="age" id="age" value="${student.age}" placeholder="请在这里输入年龄"></td>
</tr>
<tr>
<td>性别:</td>
<td><input type="radio" class="radio radio-inline" name="sex" value="男"> 男
<input type="radio" class="radio radio-inline" name="sex" value="女"> 女
</td>
</tr>
<tr>
<td>出生日期:</td>
<td><input type="date" name="birthday" id="birthday" value="${student.birthday}"
placeholder="请在这里输入出生日期"></td>
</tr>
<tr class="submitTR">
<td colspan="2" align="center">
<input type="hidden" name="id" value="${student.id}">
<button type="submit" class="btn btn-success">提 交</button>
</td>
</tr>
</table>
</form>
</div>
</div>
</div>