SpringMVC实战--构建学生管理系统(15)
首页部分成绩录入页面(编辑/删除/批量录入功能)制作
(源代码见仓库:https://gitee.com/jianghao233/course)
新建 scoreInManager.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>
</head>
<body>
<div id="class-title">
学生成绩表
</div>
<form action="${pageContext.request.contextPath}/scorein/batModify" method="post">
<table cellspacing="0">
<tr>
<th>班级</th>
<th>学生学号</th>
<th>学生名称</th>
<th>学年</th>
<th>学期</th>
<th>课程</th>
<th>学分</th>
<th>成绩</th>
<th>操作</th>
</tr>
<c:forEach items="${list}" var="c" varStatus="s">
<tr>
<input type="hidden" name="selections[${s.index }].selid" value="${c.selid }"/>
<td>${c.classname }</td>
<td>${c.stunum }</td>
<td>${c.stuname }</td>
<td>${c.year }</td>
<td>${c.term }</td>
<td>${c.coursename }</td>
<td>${c.score }</td>
<td><input type="text" id="grade${s.index }" name="selections[${s.index }].grade" value="${c.grade }"/></td>
<td>
<a href="javascript:void(0)" onclick="modify('${c.selid}','${s.index }')">编辑</a>
<a href="${pageContext.request.contextPath}/scorein/deleteScore?selid=${c.selid }">删除</a>
</td>
</tr>
</c:forEach>
</table>
<input type="submit" value="批量录入"/>
</form>
</body>
</html>
<script type="text/javascript" src="${pageContext.request.contextPath }/static/js/jquery.js"></script>
<script type="text/javascript">
function modify(selid,index){
var grade = $('#grade'+index).val();
var url = '${pageContext.request.contextPath}/scorein/modify?selid=' + selid + '&grade=' + grade;
location.href = url;
}
</script>
新建 ScoreInController.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.Selection;
import com.neuedu.service.ScoreInService;
import com.neuedu.vo.ScoreInVO;
@Controller
@RequestMapping("/scorein")
public class ScoreInController {
@Autowired
private ScoreInService scoreInService;
@RequestMapping({"/","/list"})
public String list(Model model) {
List<ScoreInVO> list = scoreInService.getList();
model.addAttribute("list", list);
return "admin/scoreInManager";
}
@RequestMapping("/batModify")
public String batModify(ScoreInVO scoreInVO) {
scoreInService.batModify(scoreInVO.getSelections());
return "redirect:/scorein/";
}
@RequestMapping("/modify")
public String modify(Selection selection) {
scoreInService.modify(selection);
return "redirect:/scorein/";
}
@RequestMapping("/deleteScore")
public String deleteScore(Integer selid) {
scoreInService.deleteScore(selid);
return "redirect:/scorein/";
}
}
修改 index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/static/css/admin_index.css"/>
</head>
<body>
<div id="header">
</div>
<div id="left">
<ul id="nav">
<li><a href="${pageContext.request.contextPath}/class/list" target="content">班级管理</a></li>
<li><a href="${pageContext.request.contextPath}/student/" target="content">学生管理</a></li>
<li><a href="${pageContext.request.contextPath}/course/" target="content">课程管理</a></li>
<li><a href="${pageContext.request.contextPath}/scorein/" target="content">成绩录入</a></li> //修改代码
</ul>
</div>
<div id="right">
<iframe id="iframe" name="content" src="studentManager.jsp" width="100%" height="700px"></iframe>
</div>
</body>
</html>
新建 ScoreInVO.java
package com.neuedu.vo;
import java.util.List;
import com.neuedu.po.Selection;
public class ScoreInVO extends Selection{
private String classname;
private String stunum;
private String stuname;
private String coursename;
private double score;
List<Selection> selections;
public List<Selection> getSelections() {
return selections;
}
public void setSelections(List<Selection> selections) {
this.selections = selections;
}
public String getClassname() {
return classname;
}
public void setClassname(String classname) {
this.classname = classname;
}
public String getStunum() {
return stunum;
}
public void setStunum(String stunum) {
this.stunum = stunum;
}
public String getStuname() {
return stuname;
}
public void setStuname(String stuname) {
this.stuname = stuname;
}
public String getCoursename() {
return coursename;
}
public void setCoursename(String coursename) {
this.coursename = coursename;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
}
新建 ScoreInMapper.java
package com.neuedu.mapper;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import com.neuedu.vo.ScoreInVO;
public interface ScoreInMapper {
public List<ScoreInVO> getList();
public void deleteScore(@Param("selid")Integer selid);
}
新建 ScoreInMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.neuedu.mapper.ScoreInMapper">
<select id="getList" resultType="com.neuedu.vo.ScoreInVO">
select
sn.selid,
c.classname,
s.stunum,
s.stuname,
sn.`year`,
sn.term,
course.coursename,
course.score,
sn.grade
from selection sn
LEFT JOIN tb_student s on s.stuid = sn.stuid
LEFT JOIN tb_class c on s.classid = c.classid
LEFT JOIN tb_course course on course.courseid = sn.courseid
order by sn.`year`,sn.term,c.classid
</select>
<update id="deleteScore">
update selection set grade = null where selid = #{selid}
</update>
</mapper>
新建 ScoreInService.java
package com.neuedu.service;
import java.util.List;
import com.neuedu.po.Selection;
import com.neuedu.vo.ScoreInVO;
public interface ScoreInService {
public List<ScoreInVO> getList();
public void batModify(List<Selection> selections);
public void modify(Selection selection);
public void deleteScore(Integer selid);
}
新建 ScoreInServiceImpl.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.ScoreInMapper;
import com.neuedu.mapper.SelectionMapper;
import com.neuedu.po.Selection;
import com.neuedu.service.ScoreInService;
import com.neuedu.vo.ScoreInVO;
@Service
public class ScoreInServiceImpl implements ScoreInService {
@Autowired
private ScoreInMapper scoreMapper;
@Autowired
private SelectionMapper selMapper;
@Override
public List<ScoreInVO> getList() {
return scoreMapper.getList();
}
@Override
public void batModify(List<Selection> selections) {
for (Selection s : selections) {
selMapper.updateByPrimaryKeySelective(s);
}
}
@Override
public void modify(Selection selection) {
selMapper.updateByPrimaryKeySelective(selection);
}
@Override
public void deleteScore(Integer selid) {
scoreMapper.deleteScore(selid);
}
}
输出:进入成绩录入页面---输入成绩--点击编辑---点击删除
输入全部成绩--点击 批量录入 ---所有成绩全部添加到数据库