JavaWeb项目开发案例精粹-第3章在线考试系统-002配置文件及辅助类
1.
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
<filter> <!--定义核心Filter FilterDispatcher -->
<filter-name>struts2</filter-name> <!-- 定义核心Filter的名称 -->
<filter-class> <!--定义核心Filter的实现类 -->
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name><!--核心Filter的名称 -->
<url-pattern>/*</url-pattern><!--使用该核心Filter过滤所有的Web请求 -->
</filter-mapping>
</web-app>
2.
<?xml version="1.0" encoding="UTF-8" ?><!-- XML声明 -->
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
"http://struts.apache.org/dtds/struts-2.1.dtd"><!-- 指定Struts 2配置文件的DTD信息 -->
<struts><!-- 根节点 -->
<constant name="struts.i18n.encoding" value="gb2312"></constant>
<package name="struts2" extends="struts-default">
<action name="login" class="com.sanqing.action.LoginAction">
<result name="studentSuccess" type="chain">getRandomSubject</result><!--进入考试页面-->
<result name="teacherSuccess" type="redirect">/teacher/index.html</result><!--老师登录成功页面-->
<result name="input">/login.jsp</result><!--登录失败页面-->
</action>
<action name="subjectAdd" class="com.sanqing.action.SubjectAddAction">
<result name="success" type="redirect">/teacher/subjectAdd.jsp</result><!--重定向到试题添加页面-->
<result name="input">/teacher/subjectAdd.jsp</result><!--请求转发到试题添加页面-->
</action>
<action name="subjectQuery" class="com.sanqing.action.QuerySubjectAction">
<result name="success">/teacher/subjectManage.jsp</result><!--跳转到试题列表管理页面-->
</action>
<action name="subjectParticular" class="com.sanqing.action.SubjectParticularAction">
<result name="success">/teacher/subjectShow.jsp</result><!--跳转到试题详细显示页面-->
</action>
<action name="subjectUpadateBefore" class="com.sanqing.action.SubjectUpdateBefore">
<result name="success">/teacher/subjectUpdate.jsp</result><!--跳转到试题更新页面-->
</action>
<action name="subjectUpadate" class="com.sanqing.action.SubjectUpdateAction">
<result name="success">/teacher/subjectUpdate.jsp</result><!--跳转到试题更新页面-->
</action>
<action name="subjectLikeQuery" class="com.sanqing.action.LikeQuerySubjectAction">
<result name="success">/teacher/subjectManage.jsp</result><!--跳转到试题列表管理页面-->
</action>
<action name="getRandomSubject" class="com.sanqing.action.GetRandomSubject">
<result name="success">/student/index.jsp</result><!--跳转到考试页面-->
</action>
<action name="submitExam" class="com.sanqing.action.SubmitExamAction">
<result name="success">/student/examResult.jsp</result><!--跳转到考试页面-->
</action>
<action name="showSubjectAnswer" class="com.sanqing.action.ShowSubjectAnswer">
<result name="success">/student/showAnswer.jsp</result><!--跳转到考试页面-->
</action>
<action name="queryStudentByName" class="com.sanqing.action.QueryStudentByName">
<result name="success">/teacher/studentManage.jsp</result><!--跳转到学生管理页面-->
</action>
<action name="queryStudentByClass" class="com.sanqing.action.QueryStudentByClass">
<result name="success">/teacher/studentManage.jsp</result><!--跳转到学生管理页面-->
</action>
</package>
</struts>
3.
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="dialect">
org.hibernate.dialect.MySQLDialect</property> <!-- 数据库方言 -->
<property name="connection.url">
jdbc:mysql://localhost:3306/db_examsystem</property><!-- 数据库连接URL -->
<property name="connection.username">root</property> <!-- 数据库用户名 -->
<property name="connection.password">1234</property> <!-- 数据库用户密码 -->
<property name="connection.driver_class"> <!-- 数据库驱动类 -->
com.mysql.jdbc.Driver</property>
<mapping resource="com/sanqing/po/Student.hbm.xml"/>
<mapping resource="com/sanqing/po/Teacher.hbm.xml"/>
<mapping resource="com/sanqing/po/Subject.hbm.xml"/>
</session-factory>
</hibernate-configuration>
4.
package com.sanqing.hibernate; import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration; public class HibernateSessionFactory {
private static String CONFIG_FILE_LOCATION
= "/hibernate.cfg.xml"; //指定配置文件路径
private static final ThreadLocal<Session> threadLocal
= new ThreadLocal<Session>(); //定义ThreadLocal对象
private static Configuration configuration
= new Configuration(); //定义Configuration对象
private static org.hibernate.SessionFactory sessionFactory;//定义SessionFactory对象
private static String configFile = CONFIG_FILE_LOCATION;
static {
try {
configuration.configure(configFile);//读取配置文件
sessionFactory =
configuration.buildSessionFactory();//根据配置文件创建SessionFactory对象
} catch (Exception e) {
System.err
.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
private HibernateSessionFactory() {
}
public static Session getSession() throws HibernateException {
Session session = (Session) threadLocal.get();//从ThreadLocal对象中获得Session对象
if (session == null || !session.isOpen()) {//判断获得的Session对象是否为空或者未打开
if (sessionFactory == null) {//如果没有创建SessionFactory对象,则首先创建
rebuildSessionFactory();
}
//如果SessionFactory对象不为空,则调用其openSession方法创建Session对象
session = (sessionFactory != null) ? sessionFactory.openSession(): null;
threadLocal.set(session);//在ThreadLocal对象中保存该Session对象
}
return session;
}
public static void rebuildSessionFactory() {
try {
configuration.configure(configFile);//读取配置文件
sessionFactory =
configuration.buildSessionFactory();//根据配置文件创建sessionFactory对象
} catch (Exception e) {
System.err
.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
public static void closeSession() throws HibernateException {
Session session = (Session) threadLocal.get();//从ThreadLocal对象中获得Session对象
threadLocal.set(null);//将当前线程Session对象从ThreadLocal对象中移除
if (session != null) {
session.close();
}
}
public static org.hibernate.SessionFactory getSessionFactory() {//取得SessionFactory对象
return sessionFactory;
}
public static void setConfigFile(String configFile) {//设置新的配置文件
HibernateSessionFactory.configFile = configFile;
sessionFactory = null;
}
public static Configuration getConfiguration() {//获得Configuration对象
return configuration;
}
}
5.
package com.sanqing.util;
public class Page {
private int everyPage; //每页显示记录数
private int totalCount; //总记录数
private int totalPage; //总页数
private int currentPage; //当前页
private int beginIndex; //查询起始点
private boolean hasPrePage; //是否有上一页
private boolean hasNextPage; //是否有下一页
public Page(int everyPage, int totalCount, int totalPage,
int currentPage,int beginIndex, boolean hasPrePage,
boolean hasNextPage) { //自定义构造方法
this.everyPage = everyPage;
this.totalCount = totalCount;
this.totalPage = totalPage;
this.currentPage = currentPage;
this.beginIndex = beginIndex;
this.hasPrePage = hasPrePage;
this.hasNextPage = hasNextPage;
}
public Page(){} //默认构造函数
public int getEveryPage() { //获得每页显示记录数
return everyPage;
}
public void setEveryPage(int everyPage) {//设置每页显示记录数
this.everyPage = everyPage;
}
public int getTotalCount() {//获得总记录数
return totalCount;
}
public void setTotalCount(int totalCount) {//设置总记录数
this.totalCount = totalCount;
}
public int getTotalPage() {//获得总页数
return totalPage;
}
public void setTotalPage(int totalPage) {//设置总页数
this.totalPage = totalPage;
}
public int getCurrentPage() {//获得当前页
return currentPage;
}
public void setCurrentPage(int currentPage) {//设置当前页
this.currentPage = currentPage;
}
public int getBeginIndex() {//获得查询起始点
return beginIndex;
}
public void setBeginIndex(int beginIndex) {//设置查询起始点
this.beginIndex = beginIndex;
}
public boolean isHasPrePage() {//获得是否有上一页
return hasPrePage;
}
public void setHasPrePage(boolean hasPrePage) {//设置是否有上一页
this.hasPrePage = hasPrePage;
}
public boolean isHasNextPage() {//获得是否有下一页
return hasNextPage;
}
public void setHasNextPage(boolean hasNextPage) {//设置是否有下一页
this.hasNextPage = hasNextPage;
}
}
6.
package com.sanqing.util; import java.util.List; public class PageResult {
private Page page; //分页信息
private List list; //记录信息
public PageResult(Page page, List list) {
this.page = page;
this.list = list;
}
public Page getPage() {
return page;
}
public void setPage(Page page) {
this.page = page;
}
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
}
7.
package com.sanqing.util;
/*
* 分页信息辅助类
*/
public class PageUtil {
public static Page createPage(int everyPage,int totalCount,int currentPage) {//创建分页信息对象
everyPage = getEveryPage(everyPage);
currentPage = getCurrentPage(currentPage);
int totalPage = getTotalPage(everyPage, totalCount);
int beginIndex = getBeginIndex(everyPage, currentPage);
boolean hasPrePage = getHasPrePage(currentPage);
boolean hasNextPage = getHasNextPage(totalPage, currentPage);
return new Page(everyPage, totalCount, totalPage, currentPage,
beginIndex, hasPrePage, hasNextPage);
}
public static int getEveryPage(int everyPage) { //获得每页显示记录数
return everyPage == 0 ? 10 : everyPage;
}
public static int getCurrentPage(int currentPage) { //获得当前页
return currentPage == 0 ? 1 : currentPage;
}
public static int getTotalPage(int everyPage,int totalCount) {//获得总页数
int totalPage = 0;
if(totalCount != 0 &&totalCount % everyPage == 0) {
totalPage = totalCount / everyPage;
} else {
totalPage = totalCount / everyPage + 1;
}
return totalPage;
}
public static int getBeginIndex(int everyPage,int currentPage) {//获得起始位置
return (currentPage - 1) * everyPage;
}
public static boolean getHasPrePage(int currentPage) {//获得是否有上一页
return currentPage == 1 ? false : true;
}
public static boolean getHasNextPage(int totalPage, int currentPage) { //获得是否有上一页
return currentPage == totalPage || totalPage == 0 ? false : true;
}
}
JavaWeb项目开发案例精粹-第3章在线考试系统-002配置文件及辅助类的更多相关文章
- JavaWeb项目开发案例精粹-第3章在线考试系统-007View层
0.login.jsp <%@ page language="java" import="java.util.*" pageEncoding=" ...
- JavaWeb项目开发案例精粹-第3章在线考试系统-006实体层
1. package com.sanqing.po; /* * 学生表,保存学生编号,系统密码 */ public class Student { private String studentID; ...
- JavaWeb项目开发案例精粹-第3章在线考试系统-005action层
1. <?xml version="1.0" encoding="UTF-8" ?><!-- XML声明 --> <!DOCTYP ...
- JavaWeb项目开发案例精粹-第3章在线考试系统-004Service层
1. package com.sanqing.service; import java.util.List; import com.sanqing.po.Student; public interfa ...
- JavaWeb项目开发案例精粹-第3章在线考试系统-003Dao层
1. package com.sanqing.dao; import java.util.List; import com.sanqing.po.Student; public interface S ...
- JavaWeb项目开发案例精粹-第3章在线考试系统-001设计
1. 2. 3. 4. # MySQL-Front 5.0 (Build 1.0) /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE */; /*!40101 SET SQL ...
- JavaWeb项目开发案例精粹-第6章报价管理系统-05Action层
0. <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC &quo ...
- JavaWeb项目开发案例精粹-第6章报价管理系统-002辅助类及配置文件
1. <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www ...
- JavaWeb项目开发案例精粹-第2章投票系统-001设计
1.项目结构 2.数据库设计 # MySQL-Front 5.0 (Build 1.0) /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE */; /*!40101 SET ...
随机推荐
- 单元测试SimpleTest新手入门
最近学习单元测试,先用了下PHPunit,结果安装问题一大堆,于是立刻放弃改试simpletest,感觉简单多了.下面列出步骤. 1.下载simpletest(版本1.1.0), http://www ...
- 用cookie实现localstorage功能
在项目中需要利用到html5的localstorage.但在利用这个属性的时候却发现无法达到预定目标.经过不断的检查及排除,最后发现原因所在: 项目中使用的浏览器是支持localstorage的,但是 ...
- protected internal修饰符
见过这样的修饰符,但是没有仔细考虑过,今天做一个小练习. 先给出一个链接,别人在网上讨论的:http://wenku.baidu.com/view/4023f65abe23482fb4da4cfe.h ...
- 用python实现两个文本合并
一段时间前在网上看到一段面试题,要求如下: employee文件中记录了工号和姓名 cat employee.txt: 100 Jason Smith 200 John Doe 300 Sanjay ...
- java之jar命令详解
1. JAR 文件包 JAR 文件就是 Java Archive File,顾名思意,它的应用是与 Java 息息相关的,是 Java 的一种文档格式.JAR 文件非常类似 ZIP 文件——准确的说, ...
- c# 空接合(??)运算符的运用
相信很多人都看到??运算符,但是不一定每个人都知道它是用来做什么的,之前我也看到过很多次,但是因为一直没有去用过,所以也没有了解他的作用,今天又看到了,所以查了的MSDN,原来??运算符叫做空接合运算 ...
- python 获取文件大小,创建时间和访问时间
# -*- coding: UTF8 -*- import timeimport datetime import os 1. '''把时间戳转化为时间: 1479264792 to 2016-11-1 ...
- 2109&2535: [Noi2010]Plane 航空管制 - BZOJ
Description世博期间,上海的航空客运量大大超过了平时,随之而来的航空管制也频频发生.最近,小X就因为航空管制,连续两次在机场被延误超过了两小时.对此,小X表示很不满意. 在这次来烟台的路上, ...
- CI_Autocomplete_2.0.php轻松实现Bebeans与Codeigniter的智能提示
在你的NetBeans项目下建立一个CI_Autocomplete_2.0.php的文件,粘贴以下代码:(codeigniter太旧了,其实性能不行,应该没人更了,换了吧,别学这玩意了,坑人) < ...
- JS Web打印,实现预览新样式
问题描述: JS实现Web打印,要求打印前一种样式,打印预览时新样式 问题解决: (1)设置打印时的css样式,设置打印前的css样式 注: 以上为print. ...