在/web/WEB-INF/jsp下新建course文件夹,在course里新建courseList.jsp
将下面的文件cv到新建的courseList.jsp里
代码如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta charset="UTF-8">
<title>课程列表</title>
<link rel="stylesheet" type="text/css" href="../easyui/themes/default/easyui.css">
<link rel="stylesheet" type="text/css" href="../easyui/themes/icon.css">
<link rel="stylesheet" type="text/css" href="../easyui/css/demo.css">
<script type="text/javascript" src="../easyui/jquery.min.js"></script>
<script type="text/javascript" src="../easyui/jquery.easyui.min.js"></script>
<script type="text/javascript" src="../easyui/js/validateExtends.js"></script>
<script type="text/javascript">
$(function() {
//datagrid初始化
$('#dataList').datagrid({
title:'课程列表',
iconCls:'icon-more',//图标
border: true,
collapsible: false,//是否可折叠的
fit: true,//自动大小
method: "post",
url:"CourseServlet?method=CourseList&t="+new Date().getTime(),
idField:'id',
singleSelect: true,//是否单选
pagination: false,//分页控件
rownumbers: true,//行号
sortName:'id',
sortOrder:'DESC',
remoteSort: false,
columns: [[
{field:'chk',checkbox: true,width:50},
{field:'id',title:'ID',width:50, sortable: true},
{field:'name',title:'课程名称',width:200},
]],
toolbar: "#toolbar"
});
//设置工具类按钮
$("#add").click(function(){
$("#addDialog").dialog("open");
});
//删除
$("#delete").click(function(){
var selectRow = $("#dataList").datagrid("getSelected");
if(selectRow == null){
$.messager.alert("消息提醒", "请选择数据进行删除!", "warning");
} else{
var courseid = selectRow.id;
$.messager.confirm("消息提醒", "将删除与课程相关的所有数据,确认继续?", function(r){
if(r){
$.ajax({
type: "post",
url: "CourseServlet?method=DeleteCourse",
data: {courseid: courseid},
success: function(msg){
if(msg == "success"){
$.messager.alert("消息提醒","删除成功!","info");
//刷新表格
$("#dataList").datagrid("reload");
} else{
$.messager.alert("消息提醒","删除失败!","warning");
return;
}
}
});
}
});
}
});
//设置添加窗口
$("#addDialog").dialog({
title: "添加课程",
width: 450,
height: 250,
iconCls: "icon-add",
modal: true,
collapsible: false,
minimizable: false,
maximizable: false,
draggable: true,
closed: true,
buttons: [
{
text:'添加',
plain: true,
iconCls:'icon-book-add',
handler:function(){
var validate = $("#addForm").form("validate");
if(!validate){
$.messager.alert("消息提醒","请检查你输入的数据!","warning");
return;
} else{
$.ajax({
type: "post",
url: "CourseServlet?method=AddCourse",
data: $("#addForm").serialize(),
success: function(msg){
if(msg == "success"){
$.messager.alert("消息提醒","添加成功!","info");
//关闭窗口
$("#addDialog").dialog("close");
//清空原表格数据
$("#add_name").textbox('setValue', "");
//刷新
$('#dataList').datagrid("reload");
} else{
$.messager.alert("消息提醒","添加失败!","warning");
return;
}
}
});
}
}
},
{
text:'重置',
plain: true,
iconCls:'icon-book-reset',
handler:function(){
$("#add_name").textbox('setValue', "");
}
},
]
});
});
</script>
</head>
<body>
<!-- 数据列表 -->
<table id="dataList" cellspacing="0" cellpadding="0">
</table>
<!-- 工具栏 -->
<div id="toolbar">
<div style="float: left;"><a id="add" href="javascript:;" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true">添加</a></div>
<div style="float: left;" class="datagrid-btn-separator"></div>
<div><a id="delete" href="javascript:;" class="easyui-linkbutton" data-options="iconCls:'icon-some-delete',plain:true">删除</a></div>
</div>
<!-- 添加数据窗口 -->
<div id="addDialog" style="padding: 10px">
<form id="addForm" method="post">
<table cellpadding="8" >
<tr>
<td>课程名称:</td>
<td><input id="add_name" style="width: 200px; height: 30px;" class="easyui-textbox" type="text" name="name" data-options="required:true, validType:'repeat_course', missingMessage:'不能为空'" /></td>
</tr>
</table>
</form>
</div>
</body>
</html>
在/src/demo/servlet里新建course文件夹,并在course里新建CourseServlet
代码如下
修改admin.jsp里
修改courseList.jsp
修改admin.jsp
修改CourseServlet
先在/src/demo/model新建Course
代码如下
package demo.model;
public class Course {
private Integer cid;
private String courseName;
public Integer getCid() {
return cid;
}
public void setCid(Integer cid) {
this.cid = cid;
}
public String getCourseName() {
return courseName;
}
public void setCourseName(String courseName) {
this.courseName = courseName;
}
}
在/src/demo/dao里新建CourseDao
代码如下
package demo.dao;
import demo.model.Course;
import demo.utils.DBUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import java.sql.SQLException;
import java.util.List;
public class CourseDao {
QueryRunner queryRunner = new QueryRunner(DBUtils.getDs());
public List<Course> getAllCourses() throws SQLException {
return queryRunner.query("select * from course",new BeanListHandler<>(Course.class));
}
}
在/src/demo/service新建CourseService
代码如下
package demo.service;
import demo.dao.CourseDao;
import demo.model.Course;
import java.sql.SQLException;
import java.util.List;
public class CourseService {
CourseDao courseDao = new CourseDao();
public List<Course>grtAllCourses(){
try {
return courseDao.getAllCourses();
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
}
在CourseServlet写如下代码
运行用Postman测试一下