1.要求1
在所有课程信息的后面加入已选该课程学生信息和未选该课程学生信息的按钮
1.1 修改jsp界面
cour_list.jsp代码如下
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8" import ="java.util.*,course.*"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>cour_list</title>
</head>
<body>
<form action="${request.contextPath}/StuCour/CourListServlet" method="post">
id:<input type = "text" name="id" <c:if test="${cour.id ne -1}">value="${cour.id}"</c:if>> <br>
name:<input type="text" name="name" value="${cour.name}"><br>
teacher:<input type = "text" name="teacher" value="${cour.teacher}"><br>
<input type="submit" value="search">
<input type="reset">
<br>
</form>
<table border="1">
<tr>
<th>课程号</th>
<th>课程名</th>
<th>教师</th>
<th>操作1</th>
<th>操作2</th>
<th>操作3</th>
<th>操作4</th>
</tr>
<c:forEach items="${requestScope.cours}" var = "cour">
<tr>
<td>${cour.id}</td>
<td>${cour.name}</td>
<td>${cour.teacher}</td>
<td><a href="${request.contextPath}/StuCour/CourDelServlet?id=${cour.id}">删除</a></td>
<td><a href="${request.contextPath}/StuCour/CourUpdServlet?id=${cour.id}">修改</a></td>
<td><a href="${request.contextPath}/StuCour/CourStuYesServlet?id=${cour.id}">已选学生</a></td>
<td><a href="${request.contextPath}/StuCour/CourStuNoServlet?id=${cour.id}">未选学生</a></td>
</tr>
</c:forEach>
</table>
<a href="${request.contextPath}/StuCour/CourAddServlet">insert</a>
</body>
</html>
实现案例:
2.要求2
进入已选该课程学生信息界面时,要求上方仍有条件查询,每个学生右侧有退选功能
2.1修改jsp界面
cour_stu_yes.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>已选该课程学生信息</title>
</head>
<body>
<form action="${ request.contextPath }/StuCour/CourStuYesServlet" method="post">
课程:${requestScope.cour.name} <br>
<input type="hidden" name="courId" value="${requestScope.cour.id}">
学号:<input type="text" name="stuId" <c:if test="${ requestScope.stu.id ne -1}"> value="${ requestScope.stu.id }"</c:if> /><br />
姓名:<input type="text" name="stuName" value="${ requestScope.stu.name }"><br />
性别:<select name="sex">
<option value="" selected></option>
<option value="男" <c:if test="${ requestScope.stu.sex eq '男'}">selected</c:if> >男</option>
<option value="女" <c:if test="${ requestScope.stu.sex eq '女'}">selected </c:if> >女</option>
</select>
年龄:<input type="text" name="age" <c:if test="${ requestScope.stu.age ne -1}"> value="${ requestScope.stu.age }"</c:if>"><br />
专业:<select name="majorId">
<option value=""> </option>
<c:forEach items="${ requestScope.majors }" var="major">
<option value="${ major.id }" <c:if test="${ major.id eq requestScope.stu.major.id}">selected</c:if> >${ major.name }</option>
</c:forEach>
</select><br />
<input type="submit" value="搜索"/><input type="reset" />
</form>
<form action="${request.contextPath}/StuCour/CourStuDelServlet" method="get">
<input type="hidden" name="courId" value="${requestScope.cour.id}">
<table border = "1">
<tr><th>学号</th> <th>姓名</th> <th>性别</th><th>年龄</th><th>专业</th><th>退课</th></tr>
<c:forEach items="${requestScope.stus}" var="stu">
<tr>
<td>${ stu.id }</td>
<td>${ stu.name }</td>
<td>${ stu.sex }</td>
<td>${ stu.age }</td>
<td>${ stu.major.name }</td>
<td><input type="checkbox" name="stuIds" value="${stu.id}"></td>
</tr>
</c:forEach>
</table>
<input type="submit" value="确认退课">
<input type="reset" value="刷新界面">
<a href="${request.contextPath}/StuCour/CourListServlet">返回主菜单</a>
</for m>
</body>
</html>
实现样例:
2.2实现CourStuYesServlet文件
doGet:
- 实现页面跳转
- 传给页面参数
- 注意点:在传参时要注意将cour信息也传过去,不然在学生界面时无cour信息,跳转到退课功能时,无法传入cour信息,导致退课功能失败
doPost:
- 实现搜索功能
- 实现页面跳转
package stucour.controller;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import course.model.Course;
import course.service.CourService;
import course.service.imp.CourServiceImp;
import major.model.Major;
import major.service.MajorService;
import major.service.imp.MajorServiceImp;
import stucour.service.StuCourService;
import stucour.service.imp.StuCourServiceImp;
import student.model.Student;
/**
* Servlet implementation class CourStuYesServlet
*/
@WebServlet("/CourStuYesServlet")
public class CourStuYesServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//读取课程id,找到该课程信息
String courId = request.getParameter("id");
Course cour = new Course();
CourService courService = new CourServiceImp();
cour = courService.listById(Integer.parseInt(courId));
//查询已选课学生信息
StuCourService stuCourService = new StuCourServiceImp();
Student stu = new Student();
List <Student> stus = stuCourService.ListStuByCour(cour, stu);
//将查到学生信息数组和课程信息传回界面
request.setAttribute("stus", stus);
request.setAttribute("cour", cour);
//条件查询需要全部专业信息,所以还得将全部专业信息传过去
MajorService majorService = new MajorServiceImp();
List <Major> majors = majorService.listAll();
request.setAttribute("majors", majors);
request.getRequestDispatcher("stucour/cour_stu_yes.jsp").forward(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//取出该课程课程号,得到该课程信息
String courId = request.getParameter("courId");
Course cour = new Course();
CourService courService = new CourServiceImp();
cour = courService.listById(Integer.parseInt(courId));
//将输入查询学生信息构成一个新的学生stu
StuCourService stuCourService = new StuCourServiceImp();
Student stu = new Student();
String id = request.getParameter("stuId").trim();
String name = request.getParameter("stuName").trim();
String sex = request.getParameter("sex").trim();
String age = request.getParameter("age").trim();
String majorId = request.getParameter("majorId").trim();
Major major = new Major();
MajorService majorService = new MajorServiceImp();
if(id != null && !id.equals(""))stu.setId( Integer.parseInt(id) );
if(name != null && !name.equals(""))stu.setName( name );
if(sex != null && !sex.equals(""))stu.setSex( sex );
if(age != null && !age.equals(""))stu.setAge( Integer.parseInt(age) );
if(majorId != null && !majorId.equals("")) {
major.setId(Integer.parseInt(majorId));
stu.setMajor(major);
}
//利用学生信息和课程信息查询选择该课程的学生,传回需要的信息
List <Student> stus = stuCourService.ListStuByCour(cour, stu);
request.setAttribute("stu", stu);
request.setAttribute("stus", stus);
request.setAttribute("cour", cour);
List <Major> majors = majorService.listAll();
request.setAttribute("majors", majors);
request.getRequestDispatcher("stucour/cour_stu_yes.jsp").forward(request, response);
}
}
3.3实现CourStuAddServlet文件
- 实现学生和课程的选课关系的保存
package stucour.controller; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import course.model.Course; import course.service.CourService; import course.service.imp.CourServiceImp; import stucour.service.StuCourService; import stucour.service.imp.StuCourServiceImp; import student.model.Student; import student.service.StuService; import student.service.imp.StuServiceImp; /** * Servlet implementation class CourStuDelServlet */ @WebServlet("/CourStuAddServlet") public class CourStuAddServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub String courId = request.getParameter("courId"); Course cour = new Course(); CourService courService = new CourServiceImp(); cour = courService.listById(Integer.parseInt(courId)); String [] stuIds = request.getParameterValues("stuIds"); Student stu = new Student(); StuService stuService = new StuServiceImp(); StuCourService stuCourService = new StuCourServiceImp(); for(String stuId:stuIds) { stu = stuService.listById(Integer.parseInt(stuId)); stuCourService.save(stu, cour); } response.sendRedirect("CourListServlet"); } }
3.要求3
进入未选该课程学生信息界面时,上方也有条件查询,每个学生右侧有选课功能
3.1修改jsp界面
cour_stu_no.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>未选该课程学生信息</title>
</head>
<body>
课程:${requestScope.cour.name} <br>
<form action="${ request.contextPath }/StuCour/CourStuNoServlet" method="post">
<input type="hidden" name="courId" value="${requestScope.cour.id}">
学号:<input type="text" name="stuId" <c:if test="${ requestScope.stu.id ne -1}"> value="${ requestScope.stu.id }"</c:if> /><br />
姓名:<input type="text" name="stuName" value="${ requestScope.stu.name }"><br />
性别:<select name="sex">
<option value="" selected></option>
<option value="男" <c:if test="${ requestScope.stu.sex eq '男'}">selected</c:if> >男</option>
<option value="女" <c:if test="${ requestScope.stu.sex eq '女'}">selected </c:if> >女</option>
</select>
年龄:<input type="text" name="age" <c:if test="${ requestScope.stu.age ne -1}"> value="${ requestScope.stu.age }"</c:if>"><br />
专业:<select name="majorId">
<option value=""> </option>
<c:forEach items="${ requestScope.majors }" var="major">
<option value="${ major.id }" <c:if test="${ major.id eq requestScope.stu.major.id}">selected</c:if> >${ major.name }</option>
</c:forEach>
</select><br />
<input type="submit" value="搜索"/><input type="reset" />
</form>
<form action="${request.contextPath}/StuCour/CourStuAddServlet" method="get">
<input type="hidden" name="courId" value="${requestScope.cour.id}">
<table border = "1">
<tr><th>学号</th> <th>姓名</th> <th>性别</th><th>年龄</th><th>专业</th><th>选课</th></tr>
<c:forEach items="${requestScope.stus}" var="stu">
<tr>
<td>${ stu.id }</td>
<td>${ stu.name }</td>
<td>${ stu.sex }</td>
<td>${ stu.age }</td>
<td>${ stu.major.name }</td>
<td><input type="checkbox" name="stuIds" value="${stu.id}"></td>
</tr>
</c:forEach>
</table>
<input type="submit" value="确认选课">
<input type="reset" value="刷新界面">
<a href="${request.contextPath}/StuCour/CourListServlet">返回主菜单</a>
</form>
</body>
</html>
实现案例
3.2实现CourStuNoServlet文件
doGet:
- 实现页面跳转
- 传给页面参数
doPost:
- 实现搜索功能
- 实现页面跳转
总体功能和要求2类似,代码几乎一致
package stucour.controller;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import course.model.Course;
import course.service.CourService;
import course.service.imp.CourServiceImp;
import major.model.Major;
import major.service.MajorService;
import major.service.imp.MajorServiceImp;
import stucour.service.StuCourService;
import stucour.service.imp.StuCourServiceImp;
import student.model.Student;
import student.service.StuService;
import student.service.imp.StuServiceImp;
/**
* Servlet implementation class CourStuYesServlet
*/
@WebServlet("/CourStuNoServlet")
public class CourStuNoServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String courId = request.getParameter("id");
Course cour = new Course();
CourService courService = new CourServiceImp();
cour = courService.listById(Integer.parseInt(courId));
StuCourService stuCourService = new StuCourServiceImp();
Student stu = new Student();
List <Student> stus = stuCourService.ListStuByCour2(cour, stu);
request.setAttribute("stus", stus);
request.setAttribute("cour", cour);
MajorService majorService = new MajorServiceImp();
List <Major> majors = majorService.listAll();
request.setAttribute("majors", majors);
request.getRequestDispatcher("stucour/cour_stu_no.jsp").forward(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//取出课程id,利用课程id查找课程cour
String courId = request.getParameter("courId");
Course cour = new Course();
CourService courService = new CourServiceImp();
cour = courService.listById(Integer.parseInt(courId));
StuCourService stuCourService = new StuCourServiceImp();
Student stu = new Student();
String id = request.getParameter("stuId").trim();
String name = request.getParameter("stuName").trim();
String sex = request.getParameter("sex").trim();
String age = request.getParameter("age").trim();
String majorId = request.getParameter("majorId").trim();
Major major = new Major();
MajorService majorService = new MajorServiceImp();
if(id != null && !id.equals(""))stu.setId( Integer.parseInt(id) );
if(name != null && !name.equals(""))stu.setName( name );
if(sex != null && !sex.equals(""))stu.setSex( sex );
if(age != null && !age.equals(""))stu.setAge( Integer.parseInt(age) );
if(majorId != null && !majorId.equals("")) {
major.setId(Integer.parseInt(majorId));
stu.setMajor(major);
}
List <Student> stus = stuCourService.ListStuByCour2(cour, stu);
request.setAttribute("stu", stu);
request.setAttribute("stus", stus);
request.setAttribute("cour", cour);
List <Major> majors = majorService.listAll();
request.setAttribute("majors", majors);
request.getRequestDispatcher("stucour/cour_stu_no.jsp").forward(request, response);
}
}
3.3实现CourStuDelServlet文件
- 实现学生和课程的选课关系的删除
package stucour.controller;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import course.model.Course;
import course.service.CourService;
import course.service.imp.CourServiceImp;
import stucour.service.StuCourService;
import stucour.service.imp.StuCourServiceImp;
import student.model.Student;
import student.service.StuService;
import student.service.imp.StuServiceImp;
/**
* Servlet implementation class CourStuDelServlet
*/
@WebServlet("/CourStuDelServlet")
public class CourStuDelServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//得到课程号
String courId = request.getParameter("courId");
Course cour = new Course();
CourService courService = new CourServiceImp();
cour = courService.listById(Integer.parseInt(courId));
//得到选择退课的学生id组
String [] stuIds = request.getParameterValues("stuIds");
Student stu = new Student();
StuService stuService = new StuServiceImp();
StuCourService stuCourService = new StuCourServiceImp();
//利用for循环遍历退课学生组
for(String stuId:stuIds)
{
//利用del函数进行删除stu与cour的选课关系
stu = stuService.listById(Integer.parseInt(stuId));
stuCourService.del(stu, cour);
}
response.sendRedirect("CourListServlet");
}
}
4.Dao功能修改
4.1ListStuByCour(Course cour, Student stu) 方法
在昨天的代码中,ListStuByCour方法对输入学号id查询功能存在bug,无论查询什么id,都相当于没有查询空id,这显然是不对的,所以在ListStuByCour方法中加入if判断id传入的值,如果stu.getId()为默认值,则表示没传参,如果不为默认值,则表示按id查询。
修改代码后,在实现过程发现,按id查询功能正常,但是想将输入的id删去,查询也正常,但对于回显功能仍存在问题,回显值不为默认值隐藏,而是为可查询的学生id最大值,经讨论查阅发现Java 函数的参数引用会导致参数的更新,有点类似C++中的指针,所以需要在结束时在做修改值为默认值的操作。
修改代码如下:
public List<Student> ListStuByCour(Course cour, Student stu) {
// TODO Auto-generated method stub
Connection con = null;
ArrayList <Student> stus = new ArrayList <Student> ();
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection(DataSource.url ,DataSource.username ,DataSource.password);
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("select * from stucour where course_id="+cour.getId());
StuService stuService = new StuServiceImp();
if(stu.getId()==-1)
{
while(rs.next())
{
int studentId = rs.getInt("student_id");
stu.setId(studentId);
List<Student> stu1s = stuService.list(stu);
for(Student stu1:stu1s)
{
stus.add(stu1);
}
stu.setId(-1);
}
}else {
while(rs.next())
{
int studentId = rs.getInt("student_id");
if(stu.getId()==studentId)
{
List<Student> stu1s = stuService.list(stu);
for(Student stu1:stu1s)
{
stus.add(stu1);
}
}
}
}
}catch(Exception e)
{
e.printStackTrace();
}
try {
con.close();
}catch (Exception e1) {
e1.printStackTrace();
}
return stus;
}
4.2ListStuByCour2(Course cour, Student stu) 方法
在未选课学生界面,经过实践发现,可以利用上述已有的Dao方法,通过全部学生stus - 已选课学生stu1s 来表示未选课学生,以此进行全部未选课学生信息的显示,但是在条件搜索界面,没有想出一个比较好的方法来实现。
反而如果在Dao层中新加一个方法,利用sql来实现对未选课学生的查询很简单,而且使得Servlet中已选课学生和未选课学生的代码只要将ListStuByCour方法改为ListStuByCour2和最后的跳转页面地址修改即可,其他代码完全一致。 所以这里选择新建ListStuByCour2方法,表示对未选课学生信息的查询。
具体代码如下:
@Override
public List<Student> ListStuByCour2(Course cour, Student stu) {
// TODO Auto-generated method stub
Connection con = null;
List <Student> stus = new ArrayList <Student> ();
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection(DataSource.url ,DataSource.username ,DataSource.password);
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("select * from student where id not in (select student_id from stucour where course_id="+cour.getId()+")");
StuService stuService = new StuServiceImp();
if(stu.getId()==-1)
{
List<Student> del = new ArrayList();
while(rs.next())
{
int studentId = rs.getInt("id");
stu.setId(studentId);
List<Student> stu1s = stuService.list(stu);
for(Student stu1:stu1s)
{
stus.add(stu1);
}
stu.setId(-1);
}
}else {
while(rs.next())
{
int studentId = rs.getInt("id");
if(studentId==stu.getId())
{
Student stu1 = new Student();
stu1 = stuService.listById(studentId);
stus.add(stu1);
}
}
}
}catch(Exception e)
{
e.printStackTrace();
}
try {
con.close();
}catch (Exception e1) {
e1.printStackTrace();
}
return stus;
}
5.代码改进
5.1在不添加额外Dao方法的情况下实现未选课学生信息查询
5.1.1改进思想
在未选课学生信息的搜索时,可以利用搜索学生信息和未选课学生信息的比对,通过判断搜索学生信息是否为默认值,为默认值则不做比较,否则判断与未选课学生信息是否相等,相等则进入新数组,最后返回该数组即可。
这样做就要求每次都能够接收和传送所有未选该课程的学生,所以要多写几次数据传递和hidden按钮
5.1.2cour_stu_no.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>未选该课程学生信息</title>
</head>
<body>
课程:${requestScope.cour.name} <br>
<form action="${ request.contextPath }/StuCour/CourStuNoServlet" method="post">
<input type="hidden" name="courId" value="${requestScope.cour.id}">
学号:<input type="text" name="stuId" <c:if test="${ requestScope.stu.id ne -1}"> value="${ requestScope.stu.id }"</c:if> /><br />
姓名:<input type="text" name="stuName" value="${ requestScope.stu.name }"><br />
性别:<select name="sex">
<option value="" selected></option>
<option value="男" <c:if test="${ requestScope.stu.sex eq '男'}">selected</c:if> >男</option>
<option value="女" <c:if test="${ requestScope.stu.sex eq '女'}">selected </c:if> >女</option>
</select>
年龄:<input type="text" name="age" <c:if test="${ requestScope.stu.age ne -1}"> value="${ requestScope.stu.age }"</c:if>"><br />
专业:<select name="majorId">
<option value=""> </option>
<c:forEach items="${ requestScope.majors }" var="major">
<option value="${ major.id }" <c:if test="${ major.id eq requestScope.stu.major.id}">selected</c:if> >${ major.name }</option>
</c:forEach>
</select><br />
<c:forEach items="${requestScope.stuNos}" var="stu">
<td><input type="hidden" name="stuIds" value="${stu.id}"></td>
</c:forEach>
<input type="submit" value="搜索"/><input type="reset" />
</form>
<form action="${request.contextPath}/StuCour/CourStuAddServlet" method="get">
<input type="hidden" name="courId" value="${requestScope.cour.id}">
<table border = "1">
<tr><th>学号</th> <th>姓名</th> <th>性别</th><th>年龄</th><th>专业</th><th>选课</th></tr>
<c:forEach items="${requestScope.stus}" var="stu">
<tr>
<td>${ stu.id }</td>
<td>${ stu.name }</td>
<td>${ stu.sex }</td>
<td>${ stu.age }</td>
<td>${ stu.major.name }</td>
<td><input type="checkbox" name="stuIds" value="${stu.id}"></td>
</tr>
</c:forEach>
</table>
<input type="submit" value="确认选课">
<input type="reset" value="刷新界面">
<a href="${request.contextPath}/StuCour/CourListServlet">返回主菜单</a>
</form>
</body>
</html>
5.1.3CourStuNoServlet
package stucour.controller;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import course.model.Course;
import course.service.CourService;
import course.service.imp.CourServiceImp;
import major.model.Major;
import major.service.MajorService;
import major.service.imp.MajorServiceImp;
import stucour.service.StuCourService;
import stucour.service.imp.StuCourServiceImp;
import student.model.Student;
import student.service.StuService;
import student.service.imp.StuServiceImp;
/**
* Servlet implementation class CourStuYesServlet
*/
@WebServlet("/CourStuNoServlet")
public class CourStuNoServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
///取出传入的课程的id,并得到该课程的全部信息,传入未选课学生界面,否则后续的添加选课功能无cour信息
String courId = request.getParameter("id");
Course cour = new Course();
CourService courService = new CourServiceImp();
cour = courService.listById(Integer.parseInt(courId));
request.setAttribute("cour", cour);
///根据空学生信息和课程信息,查找出全部已选课学生stu1s
Student stu = new Student();
StuCourService stuCourService = new StuCourServiceImp();
List<Student> stu1s = stuCourService.ListStuByCour(cour, stu);
System.out.println(stu1s.get(0).getName());
查出所有学生信息stus
StuService stuService = new StuServiceImp();
List<Student> stus = stuService.listAll();
System.out.println(stus.get(0).getName());
///未选课学生stus = stus - stu1s
List<Student> del = new ArrayList();
for(Student stuall :stus)
{
for(Student stu1:stu1s)
{
if(stu1.getId()==stuall.getId())
{
del.add(stuall);
}
}
}
stus.removeAll(del);
将未选课学生信息传入
List<Student> stuNos = stus;
request.setAttribute("stus", stus);
request.setAttribute("stuNos", stuNos);
///将全部专业信息传入,实现专业搜索
Major major = new Major();
MajorService majorService = new MajorServiceImp();
List <Major> majors = majorService.listAll();
request.setAttribute("majors", majors);
request.getRequestDispatcher("stucour/cour_stu_no.jsp").forward(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//取出该课程课程号,得到该课程信息,传入界面才可以实现之后的选课功能
String courId = request.getParameter("courId");
Course cour = new Course();
CourService courService = new CourServiceImp();
cour = courService.listById(Integer.parseInt(courId));
request.setAttribute("cour", cour);
//接收子条件查询学生信息,并新建该学生对象,传入界面实现回显功能
StuCourService stuCourService = new StuCourServiceImp();
Student stu = new Student();
String id = request.getParameter("stuId").trim();
String name = request.getParameter("stuName").trim();
String sex = request.getParameter("sex").trim();
String age = request.getParameter("age").trim();
String majorId = request.getParameter("majorId").trim();
Major major = new Major();
MajorService majorService = new MajorServiceImp();
if(id != null && !id.equals(""))stu.setId( Integer.parseInt(id) );
if(name != null && !name.equals(""))stu.setName( name );
if(sex != null && !sex.equals(""))stu.setSex( sex );
if(age != null && !age.equals(""))stu.setAge( Integer.parseInt(age) );
if(majorId != null && !majorId.equals("")) {
major.setId(Integer.parseInt(majorId));
stu.setMajor(major);
}
request.setAttribute("stu",stu);
///接收全部未选课学生信息
String [] stuIds = request.getParameterValues("stuIds");
List <Student> stuNos = new ArrayList();
StuService stuService = new StuServiceImp();
for(String stuId:stuIds)
{
stuNos.add(stuService.listById(Integer.parseInt(stuId)));
}
request.setAttribute("stuNos", stuNos);
///利用循环遍历查询学生stu和未选课学生是否相等,相等则保留进stus,并传入界面
List <Student> stus = new ArrayList();
for(Student stuNo:stuNos)
{
if((stu.getId()==-1||stu.getId()==stuNo.getId())&&(stu.getAge()==-1||stu.getAge()==stuNo.getAge())&&(stu.getName()==null||stu.getName()==stuNo.getName())&&(stu.getSex()==null||stu.getSex()==stuNo.getSex())&&(stu.getMajor()==null||stu.getMajor()==stuNo.getMajor()))
{
stus.add(stuNo);
}
}
request.setAttribute("stus", stus);
///将全部专业信息传入,实现专业搜索
List <Major> majors = majorService.listAll();
request.setAttribute("majors", majors);
request.getRequestDispatcher("stucour/cour_stu_no.jsp").forward(request, response);
}
}