SpringMVC实战--构建学生管理系统(09)
首页部分课程管理页面(编辑/删除功能)制作 (在上篇博客基础上继续)
(源代码见仓库:https://gitee.com/jianghao233/course)
编辑功能:
修改代码:courseManger.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/static/css/common.css"/>
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/static/css/admin-common.css"/>
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/static/css/admin-course.css"/>
<script src="${pageContext.request.contextPath}/static/js/jquery.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
<div id="class-title">
课程列表
</div>
<div id="add-div">
<form action="${pageContext.request.contextPath}/course/showAdd" method="post">
<input type="submit" value="添加" />
</form>
</div>
<table cellspacing="0">
<tr>
<th>课程ID</th>
<th>课程名称</th>
<th>课程学时</th>
<th>课程学分</th>
<th>操作</th>
</tr>
<c:forEach items="${list}" var="c">
<tr>
<td>${c.courseid }</td>
<td>${c.coursename }</td>
<td>${c.hour }</td>
<td>${c.score }</td>
<td>
<a href="${pageContext.request.contextPath}/course/showModify?courseid=${c.courseid}">编辑</a> //修改代码
<a href="">删除</a>
</td>
</tr>
</c:forEach>
</table>
<div id="fenye">
<div class="page">
<
</div>
<div class="page">
1
</div>
<div class="page">
>
</div>
<div class="page">
4
</div>
<div class="page">
20
</div>
</div>
</body>
</html>
修改代码:CourseService.java
package com.neuedu.service;
import java.util.List;
import com.neuedu.po.TbCourse;
public interface CourseService {
void save(TbCourse tbCourse);
TbCourse getCourseById(Integer courseid);
List<TbCourse> getList();
void update(TbCourse tbCourse); //新增代码
}
修改代码:配置文件 CourseServiceImpl.java
package com.neuedu.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.neuedu.mapper.TbCourseMapper;
import com.neuedu.po.TbCourse;
import com.neuedu.service.CourseService;
@Service
public class CourseServiceImpl implements CourseService {
@Autowired
private TbCourseMapper courseMapper;
@Override
public void save(TbCourse tbCourse) {
courseMapper.insertSelective(tbCourse);
}
@Override
public List<TbCourse> getList() {
return courseMapper.getList();
}
@Override
public TbCourse getCourseById(Integer courseid) { //新增代码
return courseMapper.selectByPrimaryKey(courseid); //新增代码
}
@Override
public void update(TbCourse tbCourse) {
courseMapper.updateByPrimaryKeySelective(tbCourse); //新增代码
}
}
修改代码:CourseController.java
package com.neuedu.controller;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.neuedu.po.TbCourse;
import com.neuedu.service.CourseService;
import com.sun.javafx.css.CssError.InlineStyleParsingError;
@Controller
@RequestMapping("/course")
public class CourseController {
@Autowired
private CourseService courseService;
@RequestMapping({"/","/list"})
public String list(Model model) {
//查询课程列表
List<TbCourse> list = courseService.getList();
model.addAttribute("list",list);
return "admin/courseManager";
}
//显示添加页面
@RequestMapping("/showAdd")
public String showAdd() {
return "admin/courseAdd";
}
//保存课程信息
@RequestMapping("/save")
public String save(TbCourse tbCourse){
//调用service保存课程信息
courseService.save(tbCourse);
return "redirect:/course/";
}
//显示编辑页面
@RequestMapping("/showModify") //新增代码
public String showModify(Integer courseid,Map<String,Object> map) {
TbCourse tbCourse = courseService.getCourseById(courseid);
map.put("c", tbCourse);
return "admin/courseModify";
} //新增代码
@RequestMapping("/modify") //新增代码
public String modify(TbCourse tbCourse) {
courseService.update(tbCourse);
return "redirect:/course/";
} //新增代码
}
输出:点击课程管理--进入课程管理页面--点击编辑--进入编辑页面---输入更改信息---点击提交--数据已保存到数据库且显示
删除功能:
修改代码:courseManger.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/static/css/common.css"/>
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/static/css/admin-common.css"/>
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/static/css/admin-course.css"/>
<script src="${pageContext.request.contextPath}/static/js/jquery.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
<div id="class-title">
课程列表
</div>
<div id="add-div">
<form action="${pageContext.request.contextPath}/course/showAdd" method="post">
<input type="submit" value="添加" />
</form>
</div>
<table cellspacing="0">
<tr>
<th>课程ID</th>
<th>课程名称</th>
<th>课程学时</th>
<th>课程学分</th>
<th>操作</th>
</tr>
<c:forEach items="${list}" var="c">
<tr>
<td>${c.courseid }</td>
<td>${c.coursename }</td>
<td>${c.hour }</td>
<td>${c.score }</td>
<td>
<a href="${pageContext.request.contextPath}/course/showModify?courseid=${c.courseid}">编辑</a>
<a href="${pageContext.request.contextPath}/course/delete?courseid=${c.courseid}">删除</a> //修改代码
</td>
</tr>
</c:forEach>
</table>
<div id="fenye">
<div class="page">
<
</div>
<div class="page">
1
</div>
<div class="page">
>
</div>
<div class="page">
4
</div>
<div class="page">
20
</div>
</div>
</body>
</html>
修改代码:CourseService.java
package com.neuedu.service;
import java.util.List;
import com.neuedu.po.TbCourse;
public interface CourseService {
void save(TbCourse tbCourse);
TbCourse getCourseById(Integer courseid);
List<TbCourse> getList();
void update(TbCourse tbCourse);
void delete(Integer courseid); //新增代码
}
修改代码:配置文件 CourseServiceImpl.java
package com.neuedu.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.neuedu.mapper.TbCourseMapper;
import com.neuedu.po.TbCourse;
import com.neuedu.service.CourseService;
@Service
public class CourseServiceImpl implements CourseService {
@Autowired
private TbCourseMapper courseMapper;
@Override
public void save(TbCourse tbCourse) {
courseMapper.insertSelective(tbCourse);
}
@Override
public List<TbCourse> getList() {
return courseMapper.getList();
}
@Override
public TbCourse getCourseById(Integer courseid) {
return courseMapper.selectByPrimaryKey(courseid);
}
@Override
public void update(TbCourse tbCourse) {
courseMapper.updateByPrimaryKeySelective(tbCourse);
}
@Override //新增代码
public void delete(Integer courseid) {
courseMapper.deleteByPrimaryKey(courseid);
} //新增代码
}
修改代码:CourseController.java
package com.neuedu.controller;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.neuedu.po.TbCourse;
import com.neuedu.service.CourseService;
import com.sun.javafx.css.CssError.InlineStyleParsingError;
@Controller
@RequestMapping("/course")
public class CourseController {
@Autowired
private CourseService courseService;
@RequestMapping({"/","/list"})
public String list(Model model) {
//查询课程列表
List<TbCourse> list = courseService.getList();
model.addAttribute("list",list);
return "admin/courseManager";
}
//显示添加页面
@RequestMapping("/showAdd")
public String showAdd() {
return "admin/courseAdd";
}
//保存课程信息
@RequestMapping("/save")
public String save(TbCourse tbCourse){
//调用service保存课程信息
courseService.save(tbCourse);
return "redirect:/course/";
}
//显示编辑页面
@RequestMapping("/showModify")
public String showModify(Integer courseid,Map<String,Object> map) {
TbCourse tbCourse = courseService.getCourseById(courseid);
map.put("c", tbCourse);
return "admin/courseModify";
}
@RequestMapping("/modify")
public String modify(TbCourse tbCourse) {
courseService.update(tbCourse);
return "redirect:/course/";
}
@RequestMapping("/delete")
public String delete(Integer courseid) {
courseService.delete(courseid);
return "redirect:/course/";
}
}
输出:点击 网络攻防 的删除按钮---网络攻防成功被删除