branch_li
LFF 3 months ago
parent d1ae0ce251
commit 2bbfdb4db2

@ -8,6 +8,7 @@ import cn.ppdxzz.service.DormService;
import cn.ppdxzz.service.StudentService;
import com.github.pagehelper.PageInfo;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@ -20,9 +21,11 @@ import java.io.InputStream;
import java.io.PrintWriter;
import java.util.List;
@RequestMapping("/dorm")
@Controller
public class DormController {
public class DormController
{
// 定义宿舍服务、学生服务和管理员服务的私有成员变量
private DormService dormService;
@ -31,19 +34,22 @@ public class DormController {
// 使用@Autowired注解自动注入DormService实例
@Autowired
public void setStudentService(StudentService studentService) {
public void setStudentService(StudentService studentService)
{
this.studentService = studentService;
}
// 使用@Autowired注解自动注入StudentService实例
@Autowired
public void setDormService(DormService dormService) {
public void setDormService(DormService dormService)
{
this.dormService = dormService;
}
// 使用@Autowired注解自动注入AdminService实例
@Autowired
public void setAdminService(AdminService adminService) {
public void setAdminService(AdminService adminService)
{
this.adminService = adminService;
}
@ -58,7 +64,10 @@ public class DormController {
*/
@RequestMapping("/findAll")
public ModelAndView findAll(@RequestParam(name = "page", required = true, defaultValue = "1")int page, @RequestParam(name = "size", required = true, defaultValue = "5") int size, HttpServletRequest request, HttpServletResponse response) throws Exception {
public ModelAndView findAll(@RequestParam(name = "page", required = true, defaultValue = "1"
)int page, @RequestParam(name = "size", required = true, defaultValue = "5") int size,
HttpServletRequest request, HttpServletResponse response) throws Exception
{
// 设置请求和响应的字符编码为UTF-8
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
@ -71,10 +80,13 @@ public class DormController {
// 获取搜索关键字
String keyword = request.getParameter("keyword");
if (keyword == null || "".trim().equals(keyword)) {
if (keyword == null || "".trim().equals(keyword))
{
// 根据分页参数查询所有宿舍信息
dorms = dormService.findAll(page,size);
}else {
}
else
{
// 根据关键字进行搜索
dorms = dormService.search(page,size,keyword);
}
@ -97,7 +109,8 @@ public class DormController {
* @throws Exception
*/
@RequestMapping("/toAdd")
public String addDorm() throws Exception {
public String addDorm() throws Exception
{
// 返回宿舍添加页面的视图名称
return "dorm-add";
}
@ -108,7 +121,8 @@ public class DormController {
* @throws Exception
*/
@RequestMapping("/add")
public void add(Dorm dorm,HttpServletResponse response) throws Exception {
public void add(Dorm dorm,HttpServletResponse response) throws Exception
{
response.setCharacterEncoding("utf-8");
// 获取PrintWriter对象用于向客户端输出内容
@ -116,14 +130,16 @@ public class DormController {
// 检查宿舍对象及其必要属性是否为空
if (dorm == null || dorm.getDorm_id() == null || dorm.getDorm_intro() == null || dorm.getDorm_rps() == null
|| dorm.getDorm_leader() == null || dorm.getTeacher() == null) {
|| dorm.getDorm_leader() == null || dorm.getTeacher() == null)
{
writer.write("false");
return;
}
// 根据宿舍ID查询是否存在相同ID的宿舍
Dorm isNull = dormService.findByDormId(dorm.getDorm_id());
if (isNull != null) {
if (isNull != null)
{
writer.write("false");
return;
}
@ -140,7 +156,8 @@ public class DormController {
* @throws Exception
*/
@RequestMapping("/isExist")
public void isExist(HttpServletRequest request,HttpServletResponse response) throws Exception {
public void isExist(HttpServletRequest request,HttpServletResponse response) throws Exception
{
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
PrintWriter writer = response.getWriter();
@ -148,7 +165,8 @@ public class DormController {
// 获取请求中的宿舍ID参数
String dorm_id = request.getParameter("dorm_id");
Dorm isNull = dormService.findByDormId(dorm_id);
if (isNull != null) {
if (isNull != null)
{
// 如果存在相同ID的宿舍则返回"true"
writer.write("true");
return;
@ -162,11 +180,13 @@ public class DormController {
* @throws Exception
*/
@RequestMapping("/toUpdate")
public ModelAndView toUpdate(HttpServletRequest request) throws Exception {
public ModelAndView toUpdate(HttpServletRequest request) throws Exception
{
request.setCharacterEncoding("utf-8");
ModelAndView mv = new ModelAndView();
String id = request.getParameter("id");
if (id == null) {
if (id == null)
{
return mv;
}
Dorm dorm = dormService.findById(id);
@ -187,11 +207,14 @@ public class DormController {
* @throws Exception
*/
@RequestMapping("/update")
public void update(Dorm dorm,HttpServletResponse response) throws Exception {
public void update(Dorm dorm,HttpServletResponse response) throws Exception
{
response.setCharacterEncoding("utf-8");
PrintWriter writer = response.getWriter();
if (dorm == null ||dorm.getId() == null || dorm.getDorm_id() == null || dorm.getDorm_intro() == null || dorm.getDorm_rps() == null
|| dorm.getDorm_leader() == null || dorm.getTeacher() == null) {
if (dorm == null ||dorm.getId() == null || dorm.getDorm_id() == null
|| dorm.getDorm_intro() == null || dorm.getDorm_rps() == null
|| dorm.getDorm_leader() == null || dorm.getTeacher() == null)
{
writer.write("false");
return;
}
@ -207,7 +230,8 @@ public class DormController {
* @throws Exception
*/
@RequestMapping("/export")
public void export(HttpServletResponse response) throws Exception {
public void export(HttpServletResponse response) throws Exception
{
// 获取宿舍信息的输入流
InputStream is = dormService.getInputStream();
@ -232,20 +256,26 @@ public class DormController {
* @throws Exception
*/
@RequestMapping("/look")
public ModelAndView look(HttpServletRequest request) throws Exception {
public ModelAndView look(HttpServletRequest request) throws Exception
{
ModelAndView mv = new ModelAndView();
Dorm dorm = null;
String id = request.getParameter("id");
String uid = request.getParameter("uid");
// 根据不同的参数情况查询宿舍信息
if (id == null && uid != null) {
if (id == null && uid != null)
{
// 根据学生ID查询学生信息再根据学生的宿舍ID查询宿舍信息
Student stu = studentService.findBySno(uid);
dorm = dormService.findByDormId(stu.getDorm_id());
}else if (id != null) {
}
else if (id != null)
{
dorm = dormService.findById(id);
}else {
}
else
{
return mv;
}
mv.addObject("dorm",dorm);
@ -265,7 +295,8 @@ public class DormController {
@RequestMapping("/byDorm_leader")
//根据宿舍ID或学生ID查询宿舍学生信息
public ModelAndView find(HttpServletRequest request) throws Exception {
public ModelAndView find(HttpServletRequest request) throws Exception
{
request.setCharacterEncoding("utf-8");
// 创建ModelAndView对象用于返回视图和数据
@ -274,7 +305,8 @@ public class DormController {
// 从请求中获取参数uid
String uid = request.getParameter("uid");
String dorm_id = request.getParameter("dorm_id");
if (dorm_id != null) {
if (dorm_id != null)
{
// 根据dorm_id查询学生信息
List<Student> studentsInfo = studentService.findByDormId(dorm_id, 1);
@ -299,7 +331,8 @@ public class DormController {
//根据教师ID查询该教师管理的宿舍信息
@RequestMapping("/byTeacher")
public ModelAndView find1(HttpServletRequest request) throws Exception {
public ModelAndView find1(HttpServletRequest request) throws Exception
{
ModelAndView mv = new ModelAndView();
String uid = request.getParameter("uid");
@ -309,7 +342,7 @@ public class DormController {
// 根据管理员姓名查询宿舍信息
List<Dorm> dorms = dormService.findByTeacher(admin.getName());
/ 宿ModelAndView
// 将宿舍信息添加到ModelAndView对象中
mv.addObject("dorms",dorms);
mv.setViewName("dormsTeacherInfo");
return mv;
@ -325,7 +358,10 @@ public class DormController {
*/
@RequestMapping("/findStudent")
//根据教师姓名和关键词分页查询学生集合
public ModelAndView findStudents(@RequestParam(name = "page", required = true, defaultValue = "1")int page, @RequestParam(name = "size", required = true, defaultValue = "5") int size,HttpServletRequest request) throws Exception {
public ModelAndView findStudents(@RequestParam(name = "page", required = true, defaultValue = "1")int page,
@RequestParam(name = "size", required = true, defaultValue = "5") int size,
HttpServletRequest request) throws Exception
{
request.setCharacterEncoding("utf-8");
ModelAndView mv = new ModelAndView();
@ -339,11 +375,14 @@ public class DormController {
// 打印关键词到控制台
System.out.println(keyword);
if (keyword == null || "".trim().equals(keyword) || keyword.length() == 0) {
if (keyword == null || "".trim().equals(keyword) || keyword.length() == 0)
{
// 根据教师姓名分页查询学生信息
students = studentService.findByTeacher(page,size,teacher);
}
if (keyword != null){
if (keyword != null)
{
students = studentService.searchStudent(page,size,teacher,keyword);
}
PageInfo pageInfo = new PageInfo(students);

@ -1,16 +1,16 @@
<!-- 生成一个宿舍信息管理页面-->
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
Created by IntelliJ IDEA.
User: user
Date: 2020/2/19
Time: 21:16
To change this template use File | Settings | File Templates.
--%>
<!-- 引入JSTL标签库并定义前缀c-->
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!-- 设置页面内容类型为HTML字符编码为UTF-8使用的编程语言为Java-->
<html>
<!-- 定义HTML文档的根元素-->
<head>
<!-- 包含文档的头部信息-->
<title>Title</title>
<!-- 设置网页标题为“Title”-->
<link rel="stylesheet" href="${pageContext.request.contextPath}/css/bootstrap.css">
<!-- 引入Bootstrap样式表路径是相对于当前请求上下文的CSS文件夹下的bootstrap.css-->
<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-3.1.1.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/bootstrap.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/layer/layer.js"></script>
@ -18,12 +18,18 @@
<body>
<br />
<form>
<!-- 表格用于显示和编辑宿舍信息 -->
<table class="table" style="width: 100%;text-align: center;">
<tbody>
<!-- 宿舍号输入框 -->
<tr>
<td><label for="dorm_id">宿舍号</label></td>
<td>
<label for="dorm_id">宿舍号</label>
</td>
<td>
<!-- 隐藏的输入框用于存储宿舍ID -->
<input type="hidden" id="id" name="id" value="${dorm.id}">
<!-- 根据管理员权限判断是否可编辑宿舍号 -->
<c:if test="${sessionScope.adminInfo.power > 3}">
<input type="text" name="dorm_id" placeholder="例:西七B209" value="${dorm.dorm_id}" class="form-control" id="dorm_id" required>
</c:if>
@ -32,17 +38,22 @@
</c:if>
</td>
</tr>
<!-- 宿舍简介输入框 -->
<tr>
<td><label for="dorm_intro">宿舍简介</label></td>
<td>
<label for="dorm_intro">宿舍简介</label>
</td>
<td colspan="3">
<input class="form-control" id="dorm_intro" value="${dorm.dorm_intro}" name="dorm_intro" cols="2" maxlength="80" placeholder="请输入宿舍简介" required="required">
</td>
</tr>
<!-- 宿舍奖惩选择框 -->
<tr>
<td>
<label for="dorm_rps">宿舍奖惩</label>
</td>
<td colspan="3">
<!-- 根据当前宿舍的奖惩类型,动态生成下拉菜单选项 -->
<c:if test="${dorm.dorm_rps == '年度最佳宿舍'}">
<select class="form-control" name="dorm_rps" id="dorm_rps">
<option value="无" selected>无</option>
@ -122,6 +133,7 @@
</c:if>
</td>
</tr>
<!-- 宿舍长输入框 -->
<tr>
<td>
<label for="dorm_leader">宿舍长</label>
@ -130,9 +142,13 @@
<input type="text" name="dorm_leader" value="${dorm.dorm_leader}" class="form-control" id="dorm_leader" required>
</td>
</tr>
<!-- 育人导师选择框 -->
<tr>
<td><label for="teacher">育人导师</label></td>
<td>
<label for="teacher">育人导师</label>
</td>
<td colspan="3">
<!-- 根据当前宿舍的育人导师,动态生成下拉菜单选项 -->
<c:if test="${dorm.teacher == '小李'}">
<select class="form-control" name="teacher" id="teacher">
<option value="小李" selected>小李</option>
@ -190,7 +206,9 @@
</table>
</form>
<script>
// 当点击id为"update-dorm"的元素时,执行以下函数
$("#update-dorm").click(function () {
// 获取并去除输入框中id、dorm_id、dorm_intro、dorm_rps、dorm_leader和teacher的值的前后空格
var id = $("#id").val().trim();
var dorm_id = $("#dorm_id").val().trim();
var dorm_intro = $("#dorm_intro").val().trim();
@ -198,14 +216,19 @@
var dorm_leader = $("#dorm_leader").val().trim();
var teacher = $("#teacher").val().trim();
// 检查是否有任何一个字段为空如果有则弹出提示信息并返回false阻止后续代码执行
if (id.length == 0 || dorm_id.length == 0 || dorm_intro.length == 0 || dorm_rps.length == 0 || dorm_leader == 0 || teacher.length == 0) {
layer.msg('字段不能为空');
return false;
}
// 检查当前用户的权限是否小于1如果是则弹出提示信息并返回false阻止后续代码执行
if (${sessionScope.adminInfo.power < 1}) {
layer.msg('权限不足');
return false;
}
// 使用AJAX发送POST请求到服务器进行数据更新操作
$.ajax({
url: "${pageContext.request.contextPath}/dorm/update",//要请求的服务器url
//这是一个对象表示请求的参数两个参数method=ajax&val=xxx服务器可以通过request.getParameter()来获取
@ -218,12 +241,13 @@
dorm_leader:dorm_leader,
teacher: teacher,
},
type: "POST", //请求方式为POST
dataType: "json",
success:function(result){ //这个方法会在服务器执行成功时被调用 参数data就是服务器返回的值(现在是json类型)
type: "POST", // 请求方式为POST
dataType: "json",// 预期服务器返回的数据类型为JSON
success:function(result){ // 请求成功时的回调函数参数result是服务器返回的数据
//alert(result);
if(result){
layer.msg('修改成功!');
// 根据用户权限不同,跳转到不同的页面
if (${sessionScope.adminInfo.power == 1}) {
setTimeout(function () {window.location.href='${pageContext.request.contextPath}/dorm/look?uid=${sessionScope.adminInfo.uid}';},2000);
}
@ -235,6 +259,7 @@
setTimeout(function () {window.location.href='${pageContext.request.contextPath}/dorm/findAll';},2000);
}
}else {
// 如果服务器返回的结果为假(即修改失败)
layer.msg('修改失败,请联系管理员');
if (${sessionScope.adminInfo.power == 1}) {
setTimeout(function () {window.location.href='${pageContext.request.contextPath}/dorm/look?uid=${sessionScope.adminInfo.uid}';},2000);

Loading…
Cancel
Save