初识 Spring(17)---(SpringMVC实战--构建学生管理系统(07))

SpringMVC实战--构建学生管理系统(07)

首页部分学生管理页面(编辑/删除功能)制作(在上篇博客基础上继续)

(源代码见仓库:https://gitee.com/jianghao233/course

编辑功能:

修改代码:studentManger.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"/>
		<script src="${pageContext.request.contextPath}/static/js/jquery.js" type="text/javascript" charset="utf-8"></script>
		<script src="${pageContext.request.contextPath}/static/js/admin-student.js" type="text/javascript" charset="utf-8"></script>
		
	</head>
	<body>
		<div id="class-title">
			学生列表
		</div>
		<div id="add-div">
			<input type="button" id="add-button" value="添加" onclick="add()"/>			
		</div>
		<table cellspacing="0" id="table">
			<tr>
				<th>学号</th>
				<th>姓名</th>
				<th>密码</th>
				<th>班级</th>
				<th>操作</th>
			</tr>
			<c:forEach items="${list }" var="s" varStatus="l">   //修改代码
				<tr>
				<td id="stunum${l.count }">${s.stunum }</td>       //修改代码
				<td id="stuname${l.count }">${s.stuname }</td>     //修改代码
				<td id="password${l.count }">${s.password }</td>    //修改代码
				<td id="classname${l.count }">${s.classname }</td>    //修改代码
				<td>
					<a href="javascript:void(0)" onclick="modify('${s.stuid}','${s.classid }','${l.count}')">编辑</a>
					<a href="">删除</a>
				</td>
			</tr>
			</c:forEach>
			
			
		</table>
		<div id="form-div">
			<form id="form1" action="${pageContext.request.contextPath}/student/save" method="post">
				<input type="hidden" name="stuid" id="stuid">    //修改代码
				学号:<input type="text" name="stunum" id="stunum"/><br />
				姓名:<input type="text" name="stuname" id="stuname"/><br />
				密码:<input type="password" name="password" id="password"/><br />
				班级:<select name="classid" id="classid">
				
						<c:forEach items="${classes }" var="c">
							<option value="${c.classid }">${c.classname }</option>
						</c:forEach>
					</select>
				<input type="submit" value="提交"/>
			</form>
		</div>
		
		<script type="text/javascript">
		$(function(){
			$('#form-div').hide();
		});
		function add(){
			$('#form-div').show();
			$("#table").hide();
			$('#add-div').hide();
			$('#class-title').html("学生信息");
		}

		function modify(stuid,classid,count){              //修改代码
			add();
			
			var stunum = $('#stunum'+count).html();
			var stuname = $('#stuname'+count).html();
			var password = $('#password'+count).html();
			var classname = $('#classname'+count).html();
			
			$('#stuid').val(stuid);
			$('#stunum').val(stunum);
			$('#stuname').val(stuname);
			$('#password').val(password);
			
			var str = "<option value='"+classid+"'>"+classname+"</option>";
			var old = $('#classid').html();
			$('#classid').html(str+old)
			
			$('#form1').attr('action','${pageContext.request.contextPath}/student/modify'); 
		}                                                //修改代码
		</script>
	</body>
</html>

修改代码:StudentService.java

package com.neuedu.service;

import java.util.List;

import com.neuedu.po.TbStudent;
import com.neuedu.vo.StudentVO;

public interface StudentService {

	public List<StudentVO> getList();
	public void save(TbStudent tbStudent);
	
	public void update(TbStudent tbStudent);          //新增代码
}

修改代码:配置文件 StudentServiceImpl.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.TbStudentMapper;
import com.neuedu.po.TbStudent;
import com.neuedu.service.StudentService;
import com.neuedu.vo.StudentVO;

@Service
public class StudentServiceImpl implements StudentService{
	@Autowired
	TbStudentMapper studentMapper;
	
	@Override
	public List<StudentVO> getList() {
		
		return studentMapper.getList();
	}

	@Override
	public void save(TbStudent tbStudent) {
		studentMapper.insertSelective(tbStudent);
		
	}

	@Override                                                    //新增代码
	public void update(TbStudent tbStudent) {
		studentMapper.updateByPrimaryKeySelective(tbStudent);
		
	}                                                            //新增代码

}

修改代码:StudentController.java

package com.neuedu.controller;

import java.util.List;

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 com.neuedu.po.TbClass;
import com.neuedu.po.TbStudent;
import com.neuedu.service.ClassService;
import com.neuedu.service.StudentService;
import com.neuedu.vo.StudentVO;

@Controller
	@RequestMapping("/student")
	public class StudentController {
		@Autowired
		private ClassService classService;
		@Autowired
		private StudentService studentService;
		@RequestMapping({"/","/list"})
		//查询所有学生信息
		public String list(Model model) {
			List<StudentVO> list = studentService.getList();
			List<TbClass> classes = classService.getList();
			
			model.addAttribute("list",list);
			model.addAttribute("classes",classes);
			return "admin/studentManager";
		}
		@RequestMapping("/save")
		public String save(TbStudent tbStudent) {
			studentService.save(tbStudent);
			return "redirect:/student/";
		}
		@RequestMapping("/modify")                        //新增代码
		public String modify(TbStudent tbStudent) {
			studentService.update(tbStudent);
			return "redirect:/student/";
		}                                                 //新增代码
	}
	

输出:点击 li 的编辑按钮---进入编辑界面---修改信息--点击提交--提交信息已更改并保存到数据库中

删除功能:

修改代码:studentManger.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"/>
		<script src="${pageContext.request.contextPath}/static/js/jquery.js" type="text/javascript" charset="utf-8"></script>
		<script src="${pageContext.request.contextPath}/static/js/admin-student.js" type="text/javascript" charset="utf-8"></script>
		
	</head>
	<body>
		<div id="class-title">
			学生列表
		</div>
		<div id="add-div">
			<input type="button" id="add-button" value="添加" onclick="add()"/>			
		</div>
		<table cellspacing="0" id="table">
			<tr>
				<th>学号</th>
				<th>姓名</th>
				<th>密码</th>
				<th>班级</th>
				<th>操作</th>
			</tr>
			
			<c:if test="${empty list }">                    //新增代码
				<tr>
					<td colspan="5">当前没有数据</td>
				</tr>
			</c:if>                                           //新增代码
			
			<c:if test="${!empty list }">                      //修改代码
				<c:forEach items="${list }" var="s" varStatus="l">
				<tr>
				<td id="stunum${l.count }">${s.stunum }</td>
				<td id="stuname${l.count }">${s.stuname }</td>
				<td id="password${l.count }">${s.password }</td>
				<td id="classname${l.count }">${s.classname }</td>
				<td>                                            //修改代码
					<a href="javascript:void(0)" onclick="modify('${s.stuid}','${s.classid }','${l.count}')">编辑</a>
					<a href="${pageContext.request.contextPath}/student/delete? 
 stuid=${s.stuid}">删除</a>                        //修改代码
				</td>
			</tr>
			</c:forEach>
			</c:if>
			
			
			
			
		</table>
		<div id="form-div">
			<form id="form1" action="${pageContext.request.contextPath}/student/save" method="post">
				<input type="hidden" name="stuid" id="stuid">
				学号:<input type="text" name="stunum" id="stunum"/><br />
				姓名:<input type="text" name="stuname" id="stuname"/><br />
				密码:<input type="password" name="password" id="password"/><br />
				班级:<select name="classid" id="classid">
				
						<c:forEach items="${classes }" var="c">
							<option value="${c.classid }">${c.classname }</option>
						</c:forEach>
					</select>
				<input type="submit" value="提交"/>
			</form>
		</div>
		
		<script type="text/javascript">
		$(function(){
			$('#form-div').hide();
		});
		function add(){
			$('#form-div').show();
			$("#table").hide();
			$('#add-div').hide();
			$('#class-title').html("学生信息");
		}

		function modify(stuid,classid,count){
			add();
			
			var stunum = $('#stunum'+count).html();
			var stuname = $('#stuname'+count).html();
			var password = $('#password'+count).html();
			var classname = $('#classname'+count).html();
			
			$('#stuid').val(stuid);
			$('#stunum').val(stunum);
			$('#stuname').val(stuname);
			$('#password').val(password);
			
			var str = "<option value='"+classid+"'>"+classname+"</option>";
			var old = $('#classid').html();
			$('#classid').html(str+old)
			
			$('#form1').attr('action','${pageContext.request.contextPath}/student/modify'); 
		}
		</script>
	</body>
</html>

修改代码:StudentService.java

package com.neuedu.service;

import java.util.List;

import com.neuedu.po.TbStudent;
import com.neuedu.vo.StudentVO;

public interface StudentService {

	public List<StudentVO> getList();
	public void save(TbStudent tbStudent);
	
	public void update(TbStudent tbStudent);
	public void delete(Integer stuid);          //新增代码
}

修改代码:配置文件 StudentServiceImpl.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.TbStudentMapper;
import com.neuedu.po.TbStudent;
import com.neuedu.service.StudentService;
import com.neuedu.vo.StudentVO;

@Service
public class StudentServiceImpl implements StudentService{
	@Autowired
	TbStudentMapper studentMapper;
	
	@Override
	public List<StudentVO> getList() {
		
		return studentMapper.getList();
	}

	@Override
	public void save(TbStudent tbStudent) {
		studentMapper.insertSelective(tbStudent);
		
	}

	@Override
	public void update(TbStudent tbStudent) {
		studentMapper.updateByPrimaryKeySelective(tbStudent);
		
	}

	@Override                                          //新增代码
	public void delete(Integer stuid) {
		studentMapper.deleteByPrimaryKey(stuid);
		
	}                                                  //新增代码

}

修改代码:StudentController.java

package com.neuedu.controller;

import java.util.List;

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 com.neuedu.po.TbClass;
import com.neuedu.po.TbStudent;
import com.neuedu.service.ClassService;
import com.neuedu.service.StudentService;
import com.neuedu.vo.StudentVO;

import oracle.net.aso.s;

@Controller
	@RequestMapping("/student")
	public class StudentController {
		@Autowired
		private ClassService classService;
		@Autowired
		private StudentService studentService;
		@RequestMapping({"/","/list"})
		//查询所有学生信息
		public String list(Model model) {
			List<StudentVO> list = studentService.getList();
			List<TbClass> classes = classService.getList();
			
			model.addAttribute("list",list);
			model.addAttribute("classes",classes);
			return "admin/studentManager";
		}
		@RequestMapping("/save")
		public String save(TbStudent tbStudent) {
			studentService.save(tbStudent);
			return "redirect:/student/";
		}
		@RequestMapping("/modify")
		public String modify(TbStudent tbStudent) {
			studentService.update(tbStudent);
			return "redirect:/student/";
		}
		@RequestMapping("/delete")                 //新增代码
		public String delete(Integer stuid) {
			studentService.delete(stuid);
			return "redirect:/student/";
		}                                            //新增代码
	}
	

输出:点击 李 的删除按钮---李成功被删除---继续删除全部数据---输出“当前没有数据”

猜你喜欢

转载自blog.csdn.net/jianghao233/article/details/81867608