– Records of t_manager
INSERT INTO t_manager
VALUES (1, ‘管理员’, ‘admin’, ‘admin’);
– Table structure for t_paper
DROP TABLE IF EXISTS t_paper
;
CREATE TABLE t_paper
(
id
int(11) NOT NULL AUTO_INCREMENT,
joinDate
datetime(0) NULL DEFAULT NULL,
paperName
varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
PRIMARY KEY (id
) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 6 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
– Records of t_paper
INSERT INTO t_paper
VALUES (1, ‘2014-01-01 00:00:00’, ‘Java试卷一’);
INSERT INTO t_paper
VALUES (2, ‘2014-02-01 00:00:00’, ‘语文试卷二’);
INSERT INTO t_paper
VALUES (3, ‘2014-01-01 00:00:00’, ‘数学试卷一’);
– Table structure for t_question
DROP TABLE IF EXISTS t_question
;
CREATE TABLE t_question
(
id
int(11) NOT NULL AUTO_INCREMENT,
answer
varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
joinTime
datetime(0) NULL DEFAULT NULL,
optionA
varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
optionB
varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
optionC
varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
optionD
varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
subject
varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
type
varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
paperId
int(11) NULL DEFAULT NULL,
PRIMARY KEY (id
) USING BTREE,
INDEX FK_ebouwob97chiilpjmc6gtgwkw
(paperId
) USING BTREE,
CONSTRAINT FK_ebouwob97chiilpjmc6gtgwkw
FOREIGN KEY (paperId
) REFERENCES t_paper
(id
) ON DELETE RESTRICT ON UPDATE RESTRICT
) ENGINE = InnoDB AUTO_INCREMENT = 21 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
– Records of t_question
INSERT INTO t_question
VALUES (1, ‘D’, ‘2014-01-01 00:00:00’, ‘A. a1’, ‘B. $1’, ‘C. _1’, ‘D .11’, ‘下列不可作为java语言修饰符的是?’, ‘1’, 1);
INSERT INTO t_question
VALUES (2, ‘A’, ‘2014-01-01 00:00:00’, ‘A. a1.java’, ‘B. a.class’, ‘C. a1’, ‘D. 都可以’, ‘有一段java应用程序,它的主类名是a1,那么保存 它的源文件名可以是?’, ‘1’, 1);
INSERT INTO t_question
VALUES (3, ‘A,B’, ‘2014-01-01 00:00:00’, ‘A. String []a’, ‘B. String a[]’, ‘C. char a[][]’, ‘D. String a[10]’, ‘下面正确声明一个一维数组的是?’, ‘2’, 1);
INSERT INTO t_question
VALUES (4, ‘A,D’, ‘2014-01-01 00:00:00’, ‘A. 在java中只允许单继承。’, ‘B. 在java中一个类只能实现一个接口。’, ‘C. 在java中一个类不能同时继承一个类和实现一个接口。’, ‘D. java的单一继承使代码更可靠。’, ‘下面关于继承的叙述哪些是正确的?’, ‘2’, 1);
INSERT INTO t_question
VALUES (5, ‘C’, ‘2014-01-01 00:00:00’, ‘A. 一个子类可以有多个父类,一个父类也可以有多个子类’, ‘B. 一个子类可以有多个父类,但一个父类只可以有一个子类’, ‘C. 一个子类可以有一个父类,但一个父类可以有多个子类’, ‘D. 上述说法都不对’, ‘在Java中?’, ‘1’, 1);
INSERT INTO t_question
VALUES (6, ‘A,D’, ‘2014-01-01 00:00:00’, ‘A. 包的声明必须是源文件的第一句代码。’, ‘B. 包的声明必须紧跟在import语句的后面。’, ‘C. 只有公共类才能放在包中。’, ‘D. 可以将多个源文件中的类放在同一个包中。’, ‘可以将多个源文件中的类放在同一个包中?’, ‘2’, 1);
INSERT INTO t_question
VALUES (7, ‘C’, ‘2014-01-01 00:00:00’, ‘A. Java是跨平台的编程语言’, ‘B. Java支持分布式计算’, ‘C. Java是面向过程的编程语言’, ‘D. Java是面向对象的编程语言’, ‘下列关于Java语言的特点,描述错误的是?’, ‘1’, 1);
INSERT INTO t_question
VALUES (21, ‘D’, ‘2021-06-29 16:00:00’, ‘1’, ‘2’, ‘3’, ‘4’, ‘Java基础类有几个?’, ‘1’, 1);
– Table structure for t_student
DROP TABLE IF EXISTS t_student
;
CREATE TABLE t_student
(
id
varchar(40) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
cardNo
varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
name
varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
password
varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
prefession
varchar(40) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
sex
varchar(5) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
PRIMARY KEY (id
) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
– Records of t_student
INSERT INTO t_student
VALUES (‘JS1001’, ‘1001’, ‘张三’, ‘123456’, ‘计算机科学’, ‘男’);
INSERT INTO t_student
VALUES (‘JS1002’, ‘1002’, ‘李四’, ‘123456’, ‘软件工程’, ‘男’);
INSERT INTO t_student
VALUES (‘JS1003’, ‘1003’, ‘王五’, ‘123456’, ‘网络攻防’, ‘女’);
SET FOREIGN_KEY_CHECKS = 1;
5.工程截图
二、系统展示
======
1.学生-登录页面
2.学生-主页面
3.学生-在线考试
4.学生-成绩查询
5.学生-修改密码
6.管理员-登录页面
7.管理员-主页面
8.管理员-考生信息管理
9.管理员-考生成绩查询
10.管理员-试卷管理
11.管理员-题目管理
三、部分代码
======
ExamAction
package com.java.yzl.action;
//考试Action类
import com.java.yzl.actionInter.ExamActionInter;
import com.java.yzl.bean.Exam;
import com.java.yzl.bean.PageBean;
import com.java.yzl.bean.Question;
import com.java.yzl.dao.ExamDao;
import com.java.yzl.dao.QuestionDao;
import com.java.yzl.util.PageUtil;
import com.java.yzl.util.PropertiesUtil;
import com.java.yzl.util.StringUtil;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.interceptor.ServletRequestAware;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;
import java.util.Map.Entry;
public class ExamAction extends ActionSupport implements ServletRequestAware, ExamActionInter {
private static final long serialVersionUID = 1L;
private ExamDao examDao = new ExamDao();
private QuestionDao questionDao = new QuestionDao();
private HttpServletRequest request;
private String mainPage;
private Exam exam;
private Exam s_exam;
private List examList;
private String page;
private int total;
private String pageCode;
public String getMainPage() {
return mainPage;
}
public void setMainPage(String mainPage) {
this.mainPage = mainPage;
}
public Exam getExam() {
return exam;
}
public void setExam(Exam exam) {
this.exam = exam;
}
public List getExamList() {
return examList;
}
public void setExamList(List examList) {
this.examList = examList;
}
public Exam getS_exam() {
return s_exam;
}
public void setS_exam(Exam s_exam) {
this.s_exam = s_exam;
}
public String getPage() {
return page;
}
public void setPage(String page) {
this.page = page;
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public String getPageCode() {
return pageCode;
}
public void setPageCode(String pageCode) {
this.pageCode = pageCode;
}
/**
计算/添加考试成绩
@return
@throws Exception
*/
public String add() throws Exception {
Map
keyMap = request.getParameterMap();
Iterator
int totalScore = 0;
int singleScore = 0;
int moreScore = 0;
while (it2.hasNext()) {
Entry
String keyStr = entry.getKey();
String values[] = entry.getValue();
String key;
String value = “”;
if (keyStr.equals(“exam.student.id”) || keyStr.equals(“exam.paper.id”)) {
continue;
}
if (keyStr.split(“-”)[1].equals(“r”)) { // 单选
key = keyStr.split(“-”)[2];
value = values[0];
singleScore += this.calScore(key, value, “1”);
} else { // 多选
key = keyStr.split(“-”)[2];
for (String s : values) {
value += s + “,”;
}
value = value.substring(0, value.length() - 1);
moreScore += this.calScore(key, value, “2”);
}
}
totalScore = singleScore + moreScore;
exam.setSingleScore(singleScore);
exam.setMoreScore(moreScore);
exam.setScore(totalScore);
exam.setExamDate(new Date());
examDao.saveExam(exam);
mainPage = “exam/examResult.jsp”;
return SUCCESS;
}
/**
计算每道题目的得分
@param questionId
@param userAnswer
@return
*/
private int calScore(String questionId, String userAnswer, String type) throws Exception {
Question question = questionDao.getQuestion(questionId);
if (userAnswer.equals(question.getAnswer())) {
if (“1”.equals(type)) {
return 20;
} else {
return 30;
}
} else {
return 0;
}
}
/**
获取考试成绩
@return
@throws Exception
*/
public String getExams() throws Exception {
examList = examDao.getExams(s_exam, null);
mainPage = “exam/myExam.jsp”;
return SUCCESS;
}
/**
获取所有考试成绩
@return
@throws Exception
*/
public String examList() throws Exception {
HttpSession session = request.getSession();
if (StringUtil.isEmpty(page)) {
page = “1”;
}
if (s_exam != null) {
session.setAttribute(“s_exam”, s_exam);
} else {
Object o = session.getAttribute(“s_exam”);
if (o != null) {
s_exam = (Exam) o;
} else {
s_exam = new Exam();
}
}
PageBean pageBean = new PageBean(Integer.parseInt(page), Integer.parseInt(PropertiesUtil.getValue(“pageSize”)));
examList = examDao.getExams(s_exam, pageBean);
total = examDao.examCount(s_exam);
pageCode = PageUtil.genPagation(request.getContextPath() + “/exam!examList”, total, Integer.parseInt(page), Integer.parseInt(PropertiesUtil.getValue(“pageSize”)));
mainPage = “exam/examList.jsp”;
return SUCCESS;
}
public void setServletRequest(HttpServletRequest request) {
this.request = request;
}
}
ManagerAction
package com.java.yzl.action;
import com.java.yzl.bean.Manager;
import com.java.yzl.dao.ManagerDao;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.interceptor.ServletRequestAware;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
public class ManagerAction extends ActionSupport implements ServletRequestAware {
/**
*/
private static final long serialVersionUID = 1L;
private HttpServletRequest request;
private ManagerDao managerDao = new ManagerDao();
private Manager manager;
private String error;
public Manager getManager() {
return manager;
}
public void setManager(Manager manager) {
this.manager = manager;
}
public String getError() {
return error;
}
public void setError(String error) {
this.error = error;
}
/**
登录验证
@return
@throws Exception
*/
public String login() throws Exception {
HttpSession session = request.getSession();
Manager currentUser = managerDao.login(manager);
if (currentUser == null) {
error = “用户名或者密码错误!”;
return ERROR;
} else {
session.setAttribute(“currentUser”, currentUser);
return SUCCESS;
}
}
/**
注销用户
@throws Exception
*/
public String logout() throws Exception {
request.getSession().invalidate();
return “logout”;
}
public void setServletRequest(HttpServletRequest request) {
this.request = request;
}
}
PaperAction
package com.java.yzl.action;
import com.java.yzl.bean.Paper;
import com.java.yzl.bean.Question;
import com.java.yzl.dao.PaperDao;
import com.java.yzl.dao.QuestionDao;
import com.java.yzl.util.ResponseUtil;
import com.java.yzl.util.StringUtil;
import com.opensymphony.xwork2.ActionSupport;
import net.sf.json.JSONObject;
import org.apache.struts2.ServletActionContext;
import java.util.*;
/**
试卷Action类
@author Administrator
*/
public class PaperAction extends ActionSupport {
/**
*/
private static final long serialVersionUID = 1L;
private PaperDao paperDao = new PaperDao();
private QuestionDao questionDao = new QuestionDao();
private String mainPage;
private String paperId;
private List paperList = new ArrayList();
private List squestionList = new ArrayList();
private List mquestionList = new ArrayList();
private String title; // 标题
private Paper paper;
public List getPaperList() {
return paperList;
}
public void setPaperList(List paperList) {
this.paperList = paperList;
}
public List getSquestionList() {
return squestionList;
}
public void setSquestionList(List squestionList) {
this.squestionList = squestionList;
}
public List getMquestionList() {
return mquestionList;
}
public void setMquestionList(List mquestionList) {
this.mquestionList = mquestionList;
}
public Paper getPaper() {
return paper;
}
public void setPaper(Paper paper) {
this.paper = paper;
}
public String getPaperId() {
return paperId;
}
public void setPaperId(String paperId) {
this.paperId = paperId;
}
public String getMainPage() {
return mainPage;
}
public void setMainPage(String mainPage) {
this.mainPage = mainPage;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
/**
获取所有试卷
@return
@throws Exception
*/
public String list() throws Exception {
paperList = paperDao.getPapers();
mainPage = “exam/selectPaper.jsp”;
return SUCCESS;
}
/**
获取所有试卷(管理)
@return
@throws Exception
*/
public String paperList() throws Exception {
paperList = paperDao.getPapers();
mainPage = “paper/paperList.jsp”;
return SUCCESS;
}
/**
通过id获取试卷实体
@return
@throws Exception
*/
public String getPaperById() throws Exception {
paper = paperDao.getPaper(paperId);
mainPage = “paper/paperSave.jsp”;
return SUCCESS;
}
/**
保存预操作
@return
@throws Exception
*/
public String preSave() throws Exception {
if (StringUtil.isNotEmpty(paperId)) {
paper = paperDao.getPaper(paperId);
title = “修改试卷”;
} else {
title = “添加试卷”;
}
mainPage = “paper/paperSave.jsp”;
return SUCCESS;
}
/**
保存试卷
@return
@throws Exception
*/
public String savePaper() throws Exception {
if (StringUtil.isNotEmpty(paperId)) {
paper.setId(Integer.parseInt(paperId));
} else {
paper.setJoinDate(new Date());
}
paperDao.savePaper(paper);
return “save”;
}
/**
删除试卷
@return
@throws Exception
*/
public String deletePaper() throws Exception {
paper = paperDao.getPaper(paperId);
JSONObject resultJson = new JSONObject();
if (questionDao.existQuestionByPaperId(paperId)) {
resultJson.put(“error”, “试卷下面有题目,不能删除”);
} else {
paperDao.paperDelete(paper);
resultJson.put(“success”, true);
}
ResponseUtil.write(resultJson, ServletActionContext.getResponse());
return null;
}
/**
获取指定试卷
@return
@throws Exception
*/
public String getDetailPaper() throws Exception {
paper = paperDao.getPaper(paperId);
Set questionList = paper.getQuestions();
Iterator it = questionList.iterator();
while (it.hasNext()) {
Question q = it.next();
if (“1”.equals(q.getType())) {
squestionList.add(q);
} else {
mquestionList.add(q);
}
}
squestionList = this.getRandomQuestion(squestionList, 3);
mquestionList = this.getRandomQuestion(mquestionList, 2);
mainPage = “exam/paper.jsp”;
return SUCCESS;
}
/**
获取随机试题
@param questionList
@param num
@return
*/
private List getRandomQuestion(List questionList, int num) {
List resultList = new ArrayList();
Random random = new Random();
if (num > 0) {
for (int i = 1; i <= num; i++) {
int n = random.nextInt(questionList.size());
Question q = questionList.get(n);
if (resultList.contains(q)) {
i–;
} else {
resultList.add(questionList.get(n));
}
}
}
return resultList;
}
}
QuestionAction
package com.java.yzl.action;
import com.java.yzl.bean.PageBean;
import com.java.yzl.bean.Paper;
import com.java.yzl.bean.Question;
import com.java.yzl.dao.PaperDao;
import com.java.yzl.dao.QuestionDao;
import com.java.yzl.util.PageUtil;
import com.java.yzl.util.PropertiesUtil;
import com.java.yzl.util.ResponseUtil;
import com.java.yzl.util.StringUtil;
import com.opensymphony.xwork2.ActionSupport;
import net.sf.json.JSONObject;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
public class QuestionAction extends ActionSupport implements ServletRequestAware {
/**
*/
private static final long serialVersionUID = 1L;
private HttpServletRequest request;
private QuestionDao questionDao = new QuestionDao();
private PaperDao paperDao = new PaperDao();
private List questionList;
private List paperList;
private String mainPage;
private String questionId;
private Question question;
private String title;
private String page;
private int total;
private String pageCode;
private Question s_question;
public List getPaperList() {
return paperList;
}
public void setPaperList(List paperList) {
this.paperList = paperList;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public List getQuestionList() {
return questionList;
}
public void setQuestionList(List questionList) {
this.questionList = questionList;
}
public String getMainPage() {
return mainPage;
}
public void setMainPage(String mainPage) {
this.mainPage = mainPage;
}
public String getPage() {
return page;
}
public void setPage(String page) {
this.page = page;
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public String getPageCode() {
return pageCode;
}
public void setPageCode(String pageCode) {
this.pageCode = pageCode;
}
public Question getS_question() {
return s_question;
}
public void setS_question(Question s_question) {
this.s_question = s_question;
}
public String getQuestionId() {
return questionId;
}
public void setQuestionId(String questionId) {
this.questionId = questionId;
}
public Question getQuestion() {
return question;
}
public void setQuestion(Question question) {
this.question = question;
}
/**
查询试题信息
@return
@throws Exception
*/
public String list() throws Exception {
HttpSession session = request.getSession();
if (StringUtil.isEmpty(page)) {
page = “1”;
}
if (s_question != null) {
session.setAttribute(“s_question”, s_question);
} else {
Object o = session.getAttribute(“s_question”);
if (o != null) {
s_question = (Question) o;
} else {
s_question = new Question();
}
}
PageBean pageBean = new PageBean(Integer.parseInt(page), Integer.parseInt(PropertiesUtil.getValue(“pageSize”)));
questionList = questionDao.getQuestions(s_question, pageBean);
total = questionDao.questionCount(s_question);
pageCode = PageUtil.genPagation(request.getContextPath() + “/question!list”, total, Integer.parseInt(page), Integer.parseInt(PropertiesUtil.getValue(“pageSize”)));
mainPage = “question/questionList.jsp”;
return SUCCESS;
}
/**
通过id获取试题
@return
@throws Exception
*/
public String getQuestionById() throws Exception {
question = questionDao.getQuestion(questionId);
mainPage = “question/questionShow.jsp”;
return SUCCESS;
}
/**
预编辑操作
@return
@throws Exception
*/
public String preSave() throws Exception {
paperList = paperDao.getPapers();
if (StringUtil.isNotEmpty(questionId)) {
question = questionDao.getQuestion(questionId);
title = “修改试题信息”;
} else {
title = “添加试题信息”;
}
mainPage = “question/questionSave.jsp”;
return SUCCESS;
}
/**
删除试题
@return
@throws Exception
*/
public String delete() throws Exception {
question = questionDao.getQuestion(questionId);
questionDao.deleteQuestion(question);
JSONObject resultJson = new JSONObject();
resultJson.put(“success”, true);
ResponseUtil.write(resultJson, ServletActionContext.getResponse());
return null;
}
/**
保存试题
@return
@throws Exception
*/
public String saveQuestion() throws Exception {
if (StringUtil.isNotEmpty(questionId)) {
question.setId(Integer.parseInt(questionId));
}
questionDao.saveQuestion(question);
return “save”;
}
public void setServletRequest(HttpServletRequest request) {
this.request = request;
}
}
StudentAction
package com.java.yzl.action;
import com.java.yzl.bean.PageBean;
import com.java.yzl.bean.Student;
import com.java.yzl.dao.StudentDao;
import com.java.yzl.util.*;
import com.opensymphony.xwork2.ActionSupport;
import net.sf.json.JSONObject;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
/**
学生Action类
@author Administrator
*/
public class StudentAction extends ActionSupport implements ServletRequestAware {
/**
*/
private static final long serialVersionUID = 1L;
private StudentDao studentDao = new StudentDao();
private HttpServletRequest request;
private String mainPage;
private Student student;
private String error;
private String page;
private int total;
private String pageCode;
private List studentList;
private Student s_student;
private String id; // 学生编号
private String title; // 标题
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
public String getError() {
return error;
}
public void setError(String error) {
this.error = error;
}
public String getMainPage() {
return mainPage;
}
public void setMainPage(String mainPage) {
this.mainPage = mainPage;
}
public String getPageCode() {
return pageCode;
}
public void setPageCode(String pageCode) {
this.pageCode = pageCode;
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public String getPage() {
return page;
}
public void setPage(String page) {
this.page = page;
}
public Student getS_student() {
return s_student;
}
public void setS_student(Student s_student) {
this.s_student = s_student;
}
public List getStudentList() {
return studentList;
}
public void setStudentList(List studentList) {
this.studentList = studentList;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
/**
登录验证
@return
@throws Exception
*/
public String login() throws Exception {
HttpSession session = request.getSession();
Student currentUser = studentDao.login(student);
if (currentUser == null) {
error = “准考证号或者密码错误!”;
return ERROR;
} else {
session.setAttribute(“currentUser”, currentUser);
return SUCCESS;
}
}
/**
修改密码预操作
@return
@throws Exception
*/
public String preUpdatePassword() throws Exception {
mainPage = “student/updatePassword.jsp”;
return SUCCESS;
}
/**
修改密码
@return
@throws Exception
*/
public String updatePassword() throws Exception {
Student s = studentDao.getStudentById(student.getId());
s.setPassword(student.getPassword());
studentDao.saveStudent(s);
mainPage = “student/updateSuccess.jsp”;
return SUCCESS;
}
/**
查询学生信息
@return
@throws Exception
*/
public String list() throws Exception {
HttpSession session = request.getSession();
if (StringUtil.isEmpty(page)) {
page = “1”;
}
if (s_student != null) {
session.setAttribute(“s_student”, s_student);
} else {
Object o = session.getAttribute(“s_student”);
if (o != null) {
s_student = (Student) o;
} else {
s_student = new Student();
}
}
PageBean pageBean = new PageBean(Integer.parseInt(page), Integer.parseInt(PropertiesUtil.getValue(“pageSize”)));
studentList = studentDao.getStudents(s_student, pageBean);
total = studentDao.studentCount(s_student);
pageCode = PageUtil.genPagation(request.getContextPath() + “/student!list”, total, Integer.parseInt(page), Integer.parseInt(PropertiesUtil.getValue(“pageSize”)));
mainPage = “student/studentList.jsp”;
return SUCCESS;
}
/**
获取学生
@return
@throws Exception
*/
public String getStudentById() throws Exception {
student = studentDao.getStudent(id);
mainPage = “student/studentSave.jsp”;
return SUCCESS;
}
/**
保存学生
@return
@throws Exception
*/
public String saveStudent() throws Exception {
if (StringUtil.isEmpty(student.getId())) {
student.setId(“JS” + DateUtil.getCurrentDateStr());
}
studentDao.saveStudent(student);
return “save”;
}
/**
删除学生
@return
@throws Exception
*/
public String deleteStudent() throws Exception {
student = studentDao.getStudent(id);
studentDao.studentDelete(student);
JSONObject resultJson = new JSONObject();
resultJson.put(“success”, true);
ResponseUtil.write(resultJson, ServletActionContext.getResponse());
return null;
}
/**
预添加操作
@return
@throws Exception
*/
public String preSave() throws Exception {
if (StringUtil.isNotEmpty(id)) {
student = studentDao.getStudent(id);
title = “修改学生信息”;
} else {
title = “添加学生信息”;
}
mainPage = “student/studentSave.jsp”;
return SUCCESS;
}
/**
注销用户
@throws Exception
*/
public String logout() throws Exception {
request.getSession().invalidate();
return “logout”;
}
public void setServletRequest(HttpServletRequest request) {
this.request = request;
}
}
ExamDao
package com.java.yzl.dao;
import com.java.yzl.bean.Exam;
import com.java.yzl.bean.PageBean;
import com.java.yzl.daoInter.ExamDaoInter;
import com.java.yzl.util.HibernateUtil;
import com.java.yzl.util.StringUtil;
import org.hibernate.Query;
import org.hibernate.Session;
import java.math.BigInteger;
import java.util.List;
public class ExamDao implements ExamDaoInter {
// 保存考试信息
public void saveExam(Exam exam) throws Exception {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
session.merge(exam);
session.getTransaction().commit();
}
//获取考试信息
public List getExams(Exam s_exam, PageBean pageBean) throws Exception {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
StringBuffer hql = new StringBuffer(“from Exam exam”);
if (s_exam.getStudent() != null && StringUtil.isNotEmpty(s_exam.getStudent().getId())) {
hql.append(" and exam.student.id like ‘%" + s_exam.getStudent().getId() + "%’");
}
if (s_exam.getStudent() != null && StringUtil.isNotEmpty(s_exam.getStudent().getName())) {
hql.append(" and exam.student.name like ‘%" + s_exam.getStudent().getName() + "%’");
}
Query query = session.createQuery(hql.toString().replaceFirst(“and”, “where”));
if (pageBean != null) {
query.setFirstResult(pageBean.getStart());
query.setMaxResults(pageBean.getPageSize());
}
@SuppressWarnings(“unchecked”)
List examList = (List) query.list();
session.getTransaction().commit();
return examList;
}
//查询考试信息记录数
public int examCount(Exam s_exam) throws Exception {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
StringBuffer sql = new StringBuffer("select count(*) from t_exam t1 ,t_student t2 where t1.studentId=t2.id ");
if (s_exam.getStudent() != null && StringUtil.isNotEmpty(s_exam.getStudent().getId())) {
sql.append(" and t2.id like ‘%" + s_exam.getStudent().getId() + "%’");
}
if (s_exam.getStudent() != null && StringUtil.isNotEmpty(s_exam.getStudent().getName())) {
sql.append(" and t2.name like ‘%" + s_exam.getStudent().getName() + "%’");
}
Query query = session.createSQLQuery(sql.toString());
int count = ((BigInteger) query.uniqueResult()).intValue();
session.getTransaction().commit();
return count;
}
}
ManagerDao
package com.java.yzl.dao;
import com.java.yzl.bean.Manager;
import com.java.yzl.daoInter.ManagerDaoInter;
import com.java.yzl.util.HibernateUtil;
import org.hibernate.Query;
import org.hibernate.Session;
public class ManagerDao implements ManagerDaoInter {
//管理员登录验证
public Manager login(Manager manager) throws Exception {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Query query = session.createQuery("from Manager as m where m.userName=:userName and m.password=:password ");
query.setString(“userName”, manager.getUserName());
query.setString(“password”, manager.getPassword());
Manager resultManager = (Manager) query.uniqueResult();
session.getTransaction().commit();
return resultManager;
}
}
PaperDao
package com.java.yzl.dao;
import com.java.yzl.bean.Paper;
import com.java.yzl.daoInter.PaperDaoInter;
import com.java.yzl.util.HibernateUtil;
import org.hibernate.Query;
import org.hibernate.Session;
import java.util.List;
public class PaperDao implements PaperDaoInter {
//获取所有试卷
public List getPapers() throws Exception {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Query query = session.createQuery(“from Paper”);
@SuppressWarnings(“unchecked”)
List paperList = (List) query.list();
session.getTransaction().commit();
return paperList;
}
//获取指定试卷
public Paper getPaper(String paperId) throws Exception {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Paper paper = (Paper) session.get(Paper.class, Integer.parseInt(paperId));
session.getTransaction().commit();
return paper;
}
//保存试卷实体
public void savePaper(Paper paper) throws Exception {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
session.merge(paper);
session.getTransaction().commit();
}
//删除试卷
public void paperDelete(Paper paper) throws Exception {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
session.delete(paper);
session.getTransaction().commit();
}
}
QuestionDao
package com.java.yzl.dao;
//问题DAO类
import com.java.yzl.bean.PageBean;
import com.java.yzl.bean.Question;
import com.java.yzl.bean.Student;
import com.java.yzl.daoInter.QuestionDaoInter;
import com.java.yzl.util.HibernateUtil;
import com.java.yzl.util.StringUtil;
import org.hibernate.Query;
import org.hibernate.Session;
import java.math.BigInteger;
import java.util.List;
public class QuestionDao implements QuestionDaoInter {
//通过问题id获取问题实体
public Question getQuestion(String questionId) throws Exception {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Question question = (Question) session.get(Question.class, Integer.parseInt(questionId));
session.getTransaction().commit();
return question;
}
//判断执行的试卷下有无题目
public boolean existQuestionByPaperId(String paperId) throws Exception {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Query query = session.createQuery(“from Question as q where q.paper.id=:paperId”);
query.setString(“paperId”, paperId);
@SuppressWarnings(“unchecked”)
List studentList = (List) query.list();
session.getTransaction().commit();
if (studentList.size() > 0) {
return true;
} else {
return false;
}
}
//获取所有题目
public List getQuestions(Question s_question, PageBean pageBean) throws Exception {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
StringBuffer hql = new StringBuffer(“from Question”);
if (StringUtil.isNotEmpty(s_question.getSubject())) {
hql.append(" and subject like ‘%" + s_question.getSubject() + "%’");
}
Query query = session.createQuery(hql.toString().replaceFirst(“and”, “where”));
if (pageBean != null) {
query.setFirstResult(pageBean.getStart());
query.setMaxResults(pageBean.getPageSize());
}
@SuppressWarnings(“unchecked”)
List questionList = (List) query.list();
session.getTransaction().commit();
return questionList;
}
//查询试题记录数
public int questionCount(Question s_question) throws Exception {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
StringBuffer sql = new StringBuffer(“select count(*) from t_question”);
if (StringUtil.isNotEmpty(s_question.getSubject())) {
sql.append(" and subject like ‘%" + s_question.getSubject() + "%’");
}
Query query = session.createSQLQuery(sql.toString().replaceFirst(“and”, “where”));
int count = ((BigInteger) query.uniqueResult()).intValue();
session.getTransaction().commit();
return count;
}
//保存试题实体
public void saveQuestion(Question question) throws Exception {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
session.merge(question);
session.getTransaction().commit();
}
//删除试题
public void deleteQuestion(Question question) throws Exception {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
session.delete(question);
session.getTransaction().commit();
}
}
StudentDao
package com.java.yzl.dao;
import com.java.yzl.bean.PageBean;
import com.java.yzl.bean.Student;
import com.java.yzl.daoInter.StudentDaoInter;
import com.java.yzl.util.HibernateUtil;
import com.java.yzl.util.StringUtil;
import org.hibernate.Query;
import org.hibernate.Session;
import java.math.BigInteger;
import java.util.List;
public class StudentDao implements StudentDaoInter {
//学生登录
public Student login(Student student) throws Exception {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Query query = session.createQuery("from Student as s where s.id=:id and s.password=:password ");
query.setString(“id”, student.getId());
query.setString(“password”, student.getPassword());
Student resultStu = (Student) query.uniqueResult();
session.getTransaction().commit();
return resultStu;
}
//通过id获取学生实体
public Student getStudentById(String id) throws Exception {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Student student = (Student) session.get(Student.class, id);
session.getTransaction().commit();
return student;
}
//保存学生实体
public void saveStudent(Student student) throws Exception {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
session.merge(student);
session.getTransaction().commit();
}
//获取所有学生
public List getStudents(Student s_student, PageBean pageBean) throws Exception {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
StringBuffer hql = new StringBuffer(“from Student”);
if (StringUtil.isNotEmpty(s_student.getId())) {
hql.append(" and id like ‘%" + s_student.getId() + "%’");
}
if (StringUtil.isNotEmpty(s_student.getName())) {
hql.append(" and name like ‘%" + s_student.getName() + "%’");
}
Query query = session.createQuery(hql.toString().replaceFirst(“and”, “where”));
if (pageBean != null) {
query.setFirstResult(pageBean.getStart());
query.setMaxResults(pageBean.getPageSize());
}
@SuppressWarnings(“unchecked”)
List studentList = (List) query.list();
session.getTransaction().commit();
return studentList;
}
//查询学生记录数
public int studentCount(Student s_student) throws Exception {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
StringBuffer sql = new StringBuffer(“select count(*) from t_student”);
if (StringUtil.isNotEmpty(s_student.getId())) {
sql.append(" and id like ‘%" + s_student.getId() + "%’");
}
if (StringUtil.isNotEmpty(s_student.getName())) {
sql.append(" and name like ‘%" + s_student.getName() + "%’");
}
Query query = session.createSQLQuery(sql.toString().replaceFirst(“and”, “where”));
int count = ((BigInteger) query.uniqueResult()).intValue();
session.getTransaction().commit();
return count;
}
//保存学生
public void studentSave(Student student) throws Exception {