JavaWeb项目开发案例精粹-第3章在线考试系统-002配置文件及辅助类

1.

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app version="2.5" 
 3     xmlns="http://java.sun.com/xml/ns/javaee" 
 4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 5     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
 6     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 7   <welcome-file-list>
 8     <welcome-file>login.jsp</welcome-file>
 9   </welcome-file-list>
10       <filter>                                <!--定义核心Filter FilterDispatcher -->
11         <filter-name>struts2</filter-name>    <!-- 定义核心Filter的名称 -->
12         <filter-class>                        <!--定义核心Filter的实现类 -->
13             org.apache.struts2.dispatcher.FilterDispatcher
14         </filter-class>
15     </filter>
16     <filter-mapping>
17         <filter-name>struts2</filter-name><!--核心Filter的名称 -->
18         <url-pattern>/*</url-pattern><!--使用该核心Filter过滤所有的Web请求 -->
19     </filter-mapping>
20 </web-app>

2.

 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>

3.

 1 <?xml version='1.0' encoding='UTF-8'?>
 2 <!DOCTYPE hibernate-configuration PUBLIC
 3           "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
 4           "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
 5 <hibernate-configuration>
 6     <session-factory>
 7         <property name="dialect">
 8             org.hibernate.dialect.MySQLDialect</property>        <!-- 数据库方言 -->
 9         <property name="connection.url">
10             jdbc:mysql://localhost:3306/db_examsystem</property><!-- 数据库连接URL -->
11         <property name="connection.username">root</property>    <!-- 数据库用户名 -->
12         <property name="connection.password">1234</property>    <!-- 数据库用户密码 -->
13         <property name="connection.driver_class">                <!-- 数据库驱动类 -->
14             com.mysql.jdbc.Driver</property>
15         <mapping resource="com/sanqing/po/Student.hbm.xml"/>
16         <mapping resource="com/sanqing/po/Teacher.hbm.xml"/>
17         <mapping resource="com/sanqing/po/Subject.hbm.xml"/>    
18     </session-factory>
19 </hibernate-configuration>

4.

 1 package com.sanqing.hibernate;
 2 
 3 import org.hibernate.HibernateException;
 4 import org.hibernate.Session;
 5 import org.hibernate.cfg.Configuration;
 6 
 7 public class HibernateSessionFactory {
 8     private static String CONFIG_FILE_LOCATION 
 9                     = "/hibernate.cfg.xml";                    //指定配置文件路径
10     private static final ThreadLocal<Session> threadLocal 
11                     = new ThreadLocal<Session>();            //定义ThreadLocal对象
12     private  static Configuration configuration 
13                     = new Configuration();                    //定义Configuration对象
14     private static org.hibernate.SessionFactory sessionFactory;//定义SessionFactory对象
15     private static String configFile = CONFIG_FILE_LOCATION;
16     static {
17         try {
18             configuration.configure(configFile);//读取配置文件
19             sessionFactory = 
20                 configuration.buildSessionFactory();//根据配置文件创建SessionFactory对象
21         } catch (Exception e) {
22             System.err
23                     .println("%%%% Error Creating SessionFactory %%%%");
24             e.printStackTrace();
25         }
26     }
27     private HibernateSessionFactory() {
28     }
29     public static Session getSession() throws HibernateException {
30         Session session = (Session) threadLocal.get();//从ThreadLocal对象中获得Session对象
31         if (session == null || !session.isOpen()) {//判断获得的Session对象是否为空或者未打开
32             if (sessionFactory == null) {//如果没有创建SessionFactory对象,则首先创建
33                 rebuildSessionFactory();
34             }
35             //如果SessionFactory对象不为空,则调用其openSession方法创建Session对象
36             session = (sessionFactory != null) ? sessionFactory.openSession(): null;
37             threadLocal.set(session);//在ThreadLocal对象中保存该Session对象
38         }
39         return session;
40     }
41     public static void rebuildSessionFactory() {
42         try {
43             configuration.configure(configFile);//读取配置文件
44             sessionFactory = 
45                 configuration.buildSessionFactory();//根据配置文件创建sessionFactory对象
46         } catch (Exception e) {
47             System.err
48                     .println("%%%% Error Creating SessionFactory %%%%");
49             e.printStackTrace();
50         }
51     }
52     public static void closeSession() throws HibernateException {
53         Session session = (Session) threadLocal.get();//从ThreadLocal对象中获得Session对象
54         threadLocal.set(null);//将当前线程Session对象从ThreadLocal对象中移除
55         if (session != null) {
56             session.close();
57         }
58     }
59     public static org.hibernate.SessionFactory getSessionFactory() {//取得SessionFactory对象
60         return sessionFactory;
61     }
62     public static void setConfigFile(String configFile) {//设置新的配置文件
63         HibernateSessionFactory.configFile = configFile;
64         sessionFactory = null;
65     }
66     public static Configuration getConfiguration() {//获得Configuration对象
67         return configuration;
68     }
69 }

5.

 1 package com.sanqing.util;
 2 public class Page {
 3     private int everyPage;            //每页显示记录数
 4     private int totalCount;            //总记录数
 5     private int totalPage;            //总页数
 6     private int currentPage;        //当前页
 7     private int beginIndex;            //查询起始点
 8     private boolean hasPrePage;        //是否有上一页
 9     private boolean hasNextPage;    //是否有下一页
10     public Page(int everyPage, int totalCount, int totalPage, 
11             int currentPage,int beginIndex, boolean hasPrePage,
12             boolean hasNextPage) {    //自定义构造方法
13         this.everyPage = everyPage;
14         this.totalCount = totalCount;
15         this.totalPage = totalPage;
16         this.currentPage = currentPage;
17         this.beginIndex = beginIndex;
18         this.hasPrePage = hasPrePage;
19         this.hasNextPage = hasNextPage;
20     }
21     public Page(){}                    //默认构造函数
22     public int getEveryPage() {        //获得每页显示记录数
23         return everyPage;
24     }
25     public void setEveryPage(int everyPage) {//设置每页显示记录数
26         this.everyPage = everyPage;
27     }
28     public int getTotalCount() {//获得总记录数
29         return totalCount;
30     }
31     public void setTotalCount(int totalCount) {//设置总记录数
32         this.totalCount = totalCount;
33     }
34     public int getTotalPage() {//获得总页数
35         return totalPage;
36     }
37     public void setTotalPage(int totalPage) {//设置总页数
38         this.totalPage = totalPage;
39     }
40     public int getCurrentPage() {//获得当前页
41         return currentPage;
42     }
43     public void setCurrentPage(int currentPage) {//设置当前页
44         this.currentPage = currentPage;
45     }
46     public int getBeginIndex() {//获得查询起始点
47         return beginIndex;
48     }
49     public void setBeginIndex(int beginIndex) {//设置查询起始点
50         this.beginIndex = beginIndex;
51     }
52     public boolean isHasPrePage() {//获得是否有上一页
53         return hasPrePage;
54     }
55     public void setHasPrePage(boolean hasPrePage) {//设置是否有上一页
56         this.hasPrePage = hasPrePage;
57     }
58     public boolean isHasNextPage() {//获得是否有下一页
59         return hasNextPage;
60     }
61     public void setHasNextPage(boolean hasNextPage) {//设置是否有下一页
62         this.hasNextPage = hasNextPage;
63     }
64 }

6.

 1 package com.sanqing.util;
 2 
 3 import java.util.List;
 4 
 5 public class PageResult {
 6     private Page page;        //分页信息
 7     private List list;        //记录信息
 8     public PageResult(Page page, List list) {
 9         this.page = page;
10         this.list = list;
11     }
12     public Page getPage() {
13         return page;
14     }
15     public void setPage(Page page) {
16         this.page = page;
17     }
18     public List getList() {
19         return list;
20     }
21     public void setList(List list) {
22         this.list = list;
23     }
24 }

7.

 1 package com.sanqing.util;
 2 /*
 3  * 分页信息辅助类
 4  */
 5 public class PageUtil {
 6     public static Page createPage(int everyPage,int totalCount,int currentPage) {//创建分页信息对象
 7         everyPage = getEveryPage(everyPage);
 8         currentPage = getCurrentPage(currentPage);
 9         int totalPage = getTotalPage(everyPage, totalCount);
10         int beginIndex = getBeginIndex(everyPage, currentPage);
11         boolean hasPrePage = getHasPrePage(currentPage);
12         boolean hasNextPage = getHasNextPage(totalPage, currentPage);
13         return new Page(everyPage, totalCount, totalPage, currentPage,
14                 beginIndex, hasPrePage,  hasNextPage);
15     }
16     public static int getEveryPage(int everyPage) {        //获得每页显示记录数
17         return everyPage == 0 ? 10 : everyPage;
18     }
19     public static int getCurrentPage(int currentPage) {    //获得当前页
20         return currentPage == 0 ? 1 : currentPage;
21     }
22     public static int getTotalPage(int everyPage,int totalCount) {//获得总页数
23         int totalPage = 0;
24         if(totalCount != 0 &&totalCount % everyPage == 0) {
25             totalPage = totalCount / everyPage;
26         } else {
27             totalPage = totalCount / everyPage + 1;
28         }
29         return totalPage;
30     }
31     public static int getBeginIndex(int everyPage,int currentPage) {//获得起始位置
32         return (currentPage - 1) * everyPage;
33     }
34     public static boolean getHasPrePage(int currentPage) {//获得是否有上一页
35         return currentPage == 1 ? false : true;
36     }
37     public static boolean getHasNextPage(int totalPage, int currentPage) {    //获得是否有上一页
38         return currentPage == totalPage || totalPage == 0 ? false : true;
39     }
40 }
原文地址:https://www.cnblogs.com/shamgod/p/5320328.html