JavaWeb项目开发案例精粹-第3章在线考试系统-005action层

1.

 1 <?xml version="1.0" encoding="UTF-8" ?><!-- XML声明 -->
 2 <!DOCTYPE struts PUBLIC
 3  "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
 4  "http://struts.apache.org/dtds/struts-2.1.dtd"><!-- 指定Struts 2配置文件的DTD信息 -->
 5 <struts><!-- 根节点 -->
 6     <constant name="struts.i18n.encoding" value="gb2312"></constant>
 7     <package name="struts2" extends="struts-default">
 8          <action name="login" class="com.sanqing.action.LoginAction">
 9              <result name="studentSuccess" type="chain">getRandomSubject</result><!--进入考试页面-->
10              <result name="teacherSuccess" type="redirect">/teacher/index.html</result><!--老师登录成功页面-->
11              <result name="input">/login.jsp</result><!--登录失败页面-->
12          </action>
13          <action name="subjectAdd" class="com.sanqing.action.SubjectAddAction">
14              <result name="success" type="redirect">/teacher/subjectAdd.jsp</result><!--重定向到试题添加页面-->
15              <result name="input">/teacher/subjectAdd.jsp</result><!--请求转发到试题添加页面-->
16          </action>
17          <action name="subjectQuery" class="com.sanqing.action.QuerySubjectAction">
18              <result name="success">/teacher/subjectManage.jsp</result><!--跳转到试题列表管理页面-->
19          </action>
20          <action name="subjectParticular" class="com.sanqing.action.SubjectParticularAction">
21              <result name="success">/teacher/subjectShow.jsp</result><!--跳转到试题详细显示页面-->
22          </action>
23          <action name="subjectUpadateBefore" class="com.sanqing.action.SubjectUpdateBefore">
24              <result name="success">/teacher/subjectUpdate.jsp</result><!--跳转到试题更新页面-->
25          </action>
26          <action name="subjectUpadate" class="com.sanqing.action.SubjectUpdateAction">
27              <result name="success">/teacher/subjectUpdate.jsp</result><!--跳转到试题更新页面-->
28          </action>
29          <action name="subjectLikeQuery" class="com.sanqing.action.LikeQuerySubjectAction">
30              <result name="success">/teacher/subjectManage.jsp</result><!--跳转到试题列表管理页面-->
31          </action>
32          <action name="getRandomSubject" class="com.sanqing.action.GetRandomSubject">
33              <result name="success">/student/index.jsp</result><!--跳转到考试页面-->
34          </action>
35          <action name="submitExam" class="com.sanqing.action.SubmitExamAction">
36              <result name="success">/student/examResult.jsp</result><!--跳转到考试页面-->
37          </action>
38          <action name="showSubjectAnswer" class="com.sanqing.action.ShowSubjectAnswer">
39              <result name="success">/student/showAnswer.jsp</result><!--跳转到考试页面-->
40          </action>
41          <action name="queryStudentByName" class="com.sanqing.action.QueryStudentByName">
42              <result name="success">/teacher/studentManage.jsp</result><!--跳转到学生管理页面-->
43          </action>
44          <action name="queryStudentByClass" class="com.sanqing.action.QueryStudentByClass">
45              <result name="success">/teacher/studentManage.jsp</result><!--跳转到学生管理页面-->
46          </action>
47      </package>    
48 </struts>

 

2.

 1 package com.sanqing.action;
 2 
 3 import java.util.List;
 4 
 5 import javax.servlet.http.HttpServletRequest;
 6 
 7 import org.apache.struts2.ServletActionContext;
 8 
 9 import com.opensymphony.xwork2.ActionSupport;
10 import com.sanqing.po.Subject;
11 import com.sanqing.service.SubjectService;
12 import com.sanqing.service.SubjectServiceImpl;
13 
14 /*
15  * 获得随机试题
16  */
17 public class GetRandomSubject extends ActionSupport{
18     private SubjectService subjectService = new SubjectServiceImpl();
19     public String execute() throws Exception {
20         List<Subject> subjects = subjectService.randomFindSubject(20);//获得试题记录
21         HttpServletRequest request = ServletActionContext.getRequest();
22         request.setAttribute("subjects", subjects);
23         return SUCCESS;
24     }
25 }

 

3.

 1 package com.sanqing.action;
 2 
 3 import java.util.ArrayList;
 4 import java.util.List;
 5 
 6 import javax.servlet.http.HttpServletRequest;
 7 
 8 import org.apache.struts2.ServletActionContext;
 9 
10 import com.opensymphony.xwork2.ActionSupport;
11 import com.sanqing.po.Subject;
12 import com.sanqing.service.SubjectService;
13 import com.sanqing.service.SubjectServiceImpl;
14 import com.sanqing.util.Page;
15 import com.sanqing.util.PageResult;
16 
17 public class LikeQuerySubjectAction extends ActionSupport {
18     private String subjectTitle;    //试题标题
19     private int currentPage;        //当前页
20     private SubjectService subjectService = new SubjectServiceImpl();
21 
22     public int getCurrentPage() {
23         return currentPage;
24     }
25 
26     public void setCurrentPage(int currentPage) {
27         this.currentPage = currentPage;
28     }
29 
30     public String getSubjectTitle() {
31         return subjectTitle;
32     }
33 
34     public void setSubjectTitle(String subjectTitle) {
35         this.subjectTitle = subjectTitle;
36     }
37 
38     public String execute() throws Exception {
39         Page page = new Page();
40         page.setEveryPage(10);//每页显示10条记录
41         page.setCurrentPage(currentPage);//设置当前页
42         PageResult pageResult = subjectService.likeQueryBySubjectTitle(subjectTitle, page);
43         List<Subject> subjects = pageResult.getList();//获得试题记录
44         List<Subject> newSubjects = new ArrayList<Subject>();//新的记录
45         //给关键字标红
46         for(Subject subject : subjects) {
47             String newTitle = subject.getSubjectTitle().replaceAll(subjectTitle,
48                         "<font color='red'>" + subjectTitle + "</font>");
49             subject.setSubjectTitle(newTitle);
50             newSubjects.add(subject);
51         }
52         
53         page = pageResult.getPage();//获得分页信息
54         HttpServletRequest request = ServletActionContext.getRequest();
55         request.setAttribute("subjects", newSubjects);
56         request.setAttribute("page", page);
57         return SUCCESS;
58     }
59 }

 

4.

 1 package com.sanqing.action;
 2 
 3 import java.util.Map;
 4 
 5 import com.opensymphony.xwork2.ActionContext;
 6 import com.opensymphony.xwork2.ActionSupport;
 7 import com.sanqing.po.Student;
 8 import com.sanqing.service.StudentService;
 9 import com.sanqing.service.StudentServiceImpl;
10 import com.sanqing.service.TeacherService;
11 import com.sanqing.service.TeacherServiceImpl;
12 
13 public class LoginAction extends ActionSupport {
14     private String id;             // 接受用户编号
15     private String password;    // 接受用户密码
16     private String role;        // 接受用户角色
17     private StudentService studentService = 
18         new StudentServiceImpl();//学生业务逻辑组件引用
19     private TeacherService teacherService = 
20         new TeacherServiceImpl();//教师业务逻辑组件引用
21 
22     public String getId() {
23         return id;
24     }
25 
26     public void setId(String id) {
27         this.id = id;
28     }
29 
30     public String getPassword() {
31         return password;
32     }
33 
34     public void setPassword(String password) {
35         this.password = password;
36     }
37 
38     public String getRole() {
39         return role;
40     }
41 
42     public void setRole(String role) {
43         this.role = role;
44     }
45 
46     public String execute() throws Exception {
47         if("student".equals(role)) {//如果以学生身份登录
48             if(studentService.allowLogin(id, password)) {
49                 Student studentInfo = studentService.getStudentInfo(id);
50                 //保存学生记录到session范围
51                 Map session = ActionContext.getContext().getSession();
52                 session.put("studentInfo", studentInfo);
53                 return "studentSuccess";
54             }else {
55                 addActionError("该学生编号不存在,或者密码不正确!");
56                 return this.INPUT;
57             }
58         }else {
59             if(teacherService.allowLogin(id, password)) {
60                 return "teacherSuccess";
61             }else {
62                 addActionError("该教师编号不存在,或者密码不正确!");
63                 return this.INPUT;
64             }
65         }
66     }
67 }

 

5.

 1 package com.sanqing.action;
 2 
 3 import java.util.ArrayList;
 4 import java.util.List;
 5 
 6 import javax.servlet.http.HttpServletRequest;
 7 
 8 import org.apache.struts2.ServletActionContext;
 9 
10 import com.opensymphony.xwork2.ActionSupport;
11 import com.sanqing.po.Student;
12 import com.sanqing.service.StudentService;
13 import com.sanqing.service.StudentServiceImpl;
14 
15 public class QueryStudentByClass extends ActionSupport{
16     private String sclass;
17     private StudentService studentService = new StudentServiceImpl();
18     public String getSclass() {
19         return sclass;
20     }
21 
22     public void setSclass(String sclass) {
23         this.sclass = sclass;
24     }
25     public String execute() throws Exception {
26         HttpServletRequest request = ServletActionContext.getRequest();
27         List<Student> students = studentService.getStudentByClass(sclass);
28         request.setAttribute("students", students);
29         return this.SUCCESS;
30     }
31     
32 }

 

6.

 1 package com.sanqing.action;
 2 
 3 import java.util.ArrayList;
 4 import java.util.List;
 5 
 6 import javax.servlet.http.HttpServletRequest;
 7 
 8 import org.apache.struts2.ServletActionContext;
 9 
10 import com.opensymphony.xwork2.ActionSupport;
11 import com.sanqing.po.Student;
12 import com.sanqing.service.StudentService;
13 import com.sanqing.service.StudentServiceImpl;
14 
15 public class QueryStudentByName extends ActionSupport{
16     private String studentName;
17     private StudentService studentService = new StudentServiceImpl();
18     public String getStudentName() {
19         return studentName;
20     }
21     public void setStudentName(String studentName) {
22         this.studentName = studentName;
23     }
24     public String execute() throws Exception {
25         HttpServletRequest request = ServletActionContext.getRequest();
26         List<Student> students = studentService.getStudentByName(studentName);
27         request.setAttribute("students", students);
28         return this.SUCCESS;
29     }
30     
31 }

 

7.

 1 package com.sanqing.action;
 2 
 3 import java.util.List;
 4 
 5 import javax.servlet.http.HttpServletRequest;
 6 
 7 import org.apache.struts2.ServletActionContext;
 8 
 9 import com.opensymphony.xwork2.ActionSupport;
10 import com.sanqing.po.Subject;
11 import com.sanqing.service.SubjectService;
12 import com.sanqing.service.SubjectServiceImpl;
13 import com.sanqing.util.Page;
14 import com.sanqing.util.PageResult;
15 
16 public class QuerySubjectAction extends ActionSupport {
17     private int currentPage;        //当前页
18     private SubjectService subjectService = new SubjectServiceImpl();
19 
20     public int getCurrentPage() {
21         return currentPage;
22     }
23 
24     public void setCurrentPage(int currentPage) {
25         this.currentPage = currentPage;
26     }
27 
28     public String execute() throws Exception {
29         Page page = new Page();
30         page.setEveryPage(10);//每页显示10条记录
31         page.setCurrentPage(currentPage);//设置当前页
32         PageResult pageResult = subjectService.querySubjectByPage(page);
33         List<Subject> subjects = pageResult.getList();//获得试题记录
34         page = pageResult.getPage();//获得分页信息
35         HttpServletRequest request = ServletActionContext.getRequest();
36         request.setAttribute("subjects", subjects);
37         request.setAttribute("page", page);
38         return SUCCESS;
39     }
40 }

 

8.

 1 package com.sanqing.action;
 2 
 3 import java.util.ArrayList;
 4 import java.util.List;
 5 import java.util.Map;
 6 
 7 import javax.servlet.http.HttpServletRequest;
 8 
 9 import org.apache.struts2.ServletActionContext;
10 
11 import com.opensymphony.xwork2.ActionContext;
12 import com.opensymphony.xwork2.ActionSupport;
13 import com.sanqing.po.Subject;
14 import com.sanqing.service.SubjectService;
15 import com.sanqing.service.SubjectServiceImpl;
16 
17 /*
18  * 显示试题答案
19  */
20 public class ShowSubjectAnswer extends ActionSupport{
21     private SubjectService subjectService = new SubjectServiceImpl();
22     public String execute() throws Exception {
23         List<Subject> subjects = new ArrayList<Subject>();//保存学生考过的题目
24         HttpServletRequest request = ServletActionContext.getRequest();
25         Map session = ActionContext.getContext().getSession();
26         List<Integer> subjectIDs = (List<Integer>) session.get("subjectIDs");
27         for(Integer subjectID : subjectIDs) {
28             Subject subject = subjectService.showSubjectParticular(subjectID);//通过试题编号查找试题
29             subjects.add(subject);
30         }
31         request.setAttribute("subjects", subjects);
32         return SUCCESS;
33     }
34 }

 

9.

 1 package com.sanqing.action;
 2 
 3 import com.opensymphony.xwork2.ActionSupport;
 4 import com.sanqing.po.Subject;
 5 import com.sanqing.service.SubjectService;
 6 import com.sanqing.service.SubjectServiceImpl;
 7 
 8 //该Action用来接收试题参数,并调用业务逻辑组件SubjectService来完成试题添加
 9 public class SubjectAddAction extends ActionSupport{
10     private String subjectTitle;
11     private String subjectOptionA;
12     private String subjectOptionB;
13     private String subjectOptionC;
14     private String subjectOptionD;
15     private String subjectAnswer;
16     private String subjectParse;
17     private SubjectService subjectService = new SubjectServiceImpl();
18     
19     public String getSubjectTitle() {
20         return subjectTitle;
21     }
22     public void setSubjectTitle(String subjectTitle) {
23         this.subjectTitle = subjectTitle;
24     }
25     public String getSubjectOptionA() {
26         return subjectOptionA;
27     }
28     public void setSubjectOptionA(String subjectOptionA) {
29         this.subjectOptionA = subjectOptionA;
30     }
31     public String getSubjectOptionB() {
32         return subjectOptionB;
33     }
34     public void setSubjectOptionB(String subjectOptionB) {
35         this.subjectOptionB = subjectOptionB;
36     }
37     public String getSubjectOptionC() {
38         return subjectOptionC;
39     }
40     public void setSubjectOptionC(String subjectOptionC) {
41         this.subjectOptionC = subjectOptionC;
42     }
43     public String getSubjectOptionD() {
44         return subjectOptionD;
45     }
46     public void setSubjectOptionD(String subjectOptionD) {
47         this.subjectOptionD = subjectOptionD;
48     }
49     public String getSubjectAnswer() {
50         return subjectAnswer;
51     }
52     public void setSubjectAnswer(String subjectAnswer) {
53         this.subjectAnswer = subjectAnswer;
54     }
55     public String getSubjectParse() {
56         return subjectParse;
57     }
58     public void setSubjectParse(String subjectParse) {
59         this.subjectParse = subjectParse;
60     }
61     public String execute() throws Exception {
62         Subject subject = new Subject();
63         subject.setSubjectTitle(subjectTitle);
64         subject.setSubjectOptionA(subjectOptionA);
65         subject.setSubjectOptionB(subjectOptionB);
66         subject.setSubjectOptionC(subjectOptionC);
67         subject.setSubjectOptionD(subjectOptionD);
68         subject.setSubjectAnswer(subjectAnswer);
69         subject.setSubjectParse(subjectParse);
70         if(subjectService.saveSubject(subject)) {
71             return SUCCESS;
72         }else {
73             this.addActionError("该试题已经添加过了,请不要重复添加!");
74             return INPUT;
75         }
76     }
77 }

 

10.

 1 package com.sanqing.action;
 2 
 3 import com.opensymphony.xwork2.ActionSupport;
 4 import com.sanqing.service.SubjectService;
 5 import com.sanqing.service.SubjectServiceImpl;
 6 
 7 public class SubjectDeleteAction extends ActionSupport{
 8     private int subjectID;
 9     private SubjectService subjectService = new SubjectServiceImpl();
10     public int getSubjectID() {
11         return subjectID;
12     }
13     public void setSubjectID(int subjectID) {
14         this.subjectID = subjectID;
15     }
16     public String execute() throws Exception {
17         subjectService.deleteSubject(subjectID);
18         return SUCCESS;
19     }
20 }

 

11.

 1 package com.sanqing.action;
 2 
 3 import org.apache.struts2.ServletActionContext;
 4 
 5 import com.opensymphony.xwork2.ActionSupport;
 6 import com.sanqing.po.Subject;
 7 import com.sanqing.service.SubjectService;
 8 import com.sanqing.service.SubjectServiceImpl;
 9 /*
10  * 查看试题详细信息
11  */
12 public class SubjectParticularAction extends ActionSupport{
13     private int subjectID;
14     private SubjectService subjectService = new SubjectServiceImpl();
15     public int getSubjectID() {
16         return subjectID;
17     }
18     public void setSubjectID(int subjectID) {
19         this.subjectID = subjectID;
20     }
21     public String execute() throws Exception {
22         Subject subject = subjectService.showSubjectParticular(subjectID);
23         ServletActionContext.getRequest().setAttribute("subject", subject);
24         return SUCCESS;
25     }
26 }

 

12.

 1 package com.sanqing.action;
 2 
 3 import org.apache.struts2.ServletActionContext;
 4 
 5 import com.opensymphony.xwork2.ActionSupport;
 6 import com.sanqing.po.Subject;
 7 import com.sanqing.service.SubjectService;
 8 import com.sanqing.service.SubjectServiceImpl;
 9 
10 public class SubjectUpdateAction extends ActionSupport{
11     private int subjectID;
12     private String subjectTitle;
13     private String subjectOptionA;
14     private String subjectOptionB;
15     private String subjectOptionC;
16     private String subjectOptionD;
17     private String subjectAnswer;
18     private String subjectParse;
19     private SubjectService subjectService = new SubjectServiceImpl();
20     
21     public int getSubjectID() {
22         return subjectID;
23     }
24     public void setSubjectID(int subjectID) {
25         this.subjectID = subjectID;
26     }
27     public String getSubjectTitle() {
28         return subjectTitle;
29     }
30     public void setSubjectTitle(String subjectTitle) {
31         this.subjectTitle = subjectTitle;
32     }
33     public String getSubjectOptionA() {
34         return subjectOptionA;
35     }
36     public void setSubjectOptionA(String subjectOptionA) {
37         this.subjectOptionA = subjectOptionA;
38     }
39     public String getSubjectOptionB() {
40         return subjectOptionB;
41     }
42     public void setSubjectOptionB(String subjectOptionB) {
43         this.subjectOptionB = subjectOptionB;
44     }
45     public String getSubjectOptionC() {
46         return subjectOptionC;
47     }
48     public void setSubjectOptionC(String subjectOptionC) {
49         this.subjectOptionC = subjectOptionC;
50     }
51     public String getSubjectOptionD() {
52         return subjectOptionD;
53     }
54     public void setSubjectOptionD(String subjectOptionD) {
55         this.subjectOptionD = subjectOptionD;
56     }
57     public String getSubjectAnswer() {
58         return subjectAnswer;
59     }
60     public void setSubjectAnswer(String subjectAnswer) {
61         this.subjectAnswer = subjectAnswer;
62     }
63     public String getSubjectParse() {
64         return subjectParse;
65     }
66     public void setSubjectParse(String subjectParse) {
67         this.subjectParse = subjectParse;
68     }
69     public String execute() throws Exception {
70         Subject subject = new Subject();
71         subject.setSubjectID(subjectID);
72         subject.setSubjectTitle(subjectTitle);
73         subject.setSubjectOptionA(subjectOptionA);
74         subject.setSubjectOptionB(subjectOptionB);
75         subject.setSubjectOptionC(subjectOptionC);
76         subject.setSubjectOptionD(subjectOptionD);
77         subject.setSubjectAnswer(subjectAnswer);
78         subject.setSubjectParse(subjectParse);
79         subjectService.updateSubject(subject);//更新
80         ServletActionContext.getRequest().setAttribute("subject", subject);
81         this.addActionMessage("更新成功!");
82         return SUCCESS;
83     }
84 }

 

13.

 1 package com.sanqing.action;
 2 
 3 import org.apache.struts2.ServletActionContext;
 4 
 5 import com.opensymphony.xwork2.ActionSupport;
 6 import com.sanqing.po.Subject;
 7 import com.sanqing.service.SubjectService;
 8 import com.sanqing.service.SubjectServiceImpl;
 9 
10 /*
11  * 获得更新试题前的试题
12  */
13 public class SubjectUpdateBefore extends ActionSupport {
14     private int subjectID;
15     private SubjectService subjectService = new SubjectServiceImpl();
16     public int getSubjectID() {
17         return subjectID;
18     }
19     public void setSubjectID(int subjectID) {
20         this.subjectID = subjectID;
21     }
22     public String execute() throws Exception {
23         Subject subject = subjectService.showSubjectParticular(subjectID);
24         ServletActionContext.getRequest().setAttribute("subject", subject);
25         return SUCCESS;
26     }
27 }

 

14.

 1 package com.sanqing.action;
 2 
 3 import java.util.ArrayList;
 4 import java.util.List;
 5 import java.util.Map;
 6 
 7 import javax.servlet.http.HttpServletRequest;
 8 
 9 import org.apache.struts2.ServletActionContext;
10 
11 import com.opensymphony.xwork2.ActionContext;
12 import com.opensymphony.xwork2.ActionSupport;
13 import com.sanqing.po.Student;
14 import com.sanqing.service.StudentService;
15 import com.sanqing.service.StudentServiceImpl;
16 import com.sanqing.service.SubjectService;
17 import com.sanqing.service.SubjectServiceImpl;
18 
19 /*
20  * 提交试卷,并打分
21  */
22 public class SubmitExamAction extends ActionSupport{
23     private List<Integer> subjectID;//学生考试的题目
24     private SubjectService subjectService = new SubjectServiceImpl();
25     private StudentService studentService = new StudentServiceImpl();
26     public List<Integer> getSubjectID() {
27         return subjectID;
28     }
29     public void setSubjectID(List<Integer> subjectID) {
30         this.subjectID = subjectID;
31     }
32     public String execute() throws Exception {
33         HttpServletRequest request = ServletActionContext.getRequest();
34         List<String> studentAnswers = new ArrayList<String>();
35         for(int i = 0; i < 20; i++) {
36             String answer = request.getParameter("subjectAnswer"+i);
37             studentAnswers.add(answer);
38         }
39         int GeneralPoint = subjectService.accountResult(subjectID, studentAnswers);
40         
41         //设置成绩到学生信息中
42         Map session = ActionContext.getContext().getSession();
43         Student student = (Student)session.get("studentInfo");
44         String studentID = student.getStudentID();
45         studentService.setStudentResult(studentID, GeneralPoint);
46         request.setAttribute("studentName", student.getStudentName());//保存学生姓名和总分数
47         request.setAttribute("GeneralPoint", GeneralPoint);
48         session.put("subjectIDs", subjectID);//将考试题目保存到session,方便后面显示答案使用
49         return SUCCESS;
50     }
51 }

 

posted @ 2016-03-25 17:37  shamgod  阅读(1422)  评论(0编辑  收藏  举报
haha