Spring+Structs2+Hibernate实例
Structs2中涉及到Action,JSP,Session会话,OGNL表达式,ModelDriven(模型驱动),拦截器栈
Spring 控制反转,管理所有类,Bean注入
HIbernate 事务管理,事务注解,c3p0连接池
JavaScript 、log4j等
版本信息
Eclipse版本 Neon.1a Release (4.6.1)
Spring 4.3.4
Structs 2.3.31
Hibernate 4.3.11
Tomcat 8.0
JDK 1.8
数据库 MySQL5.7.12
项目结构
jar 包下载:http://pan.baidu.com/s/1qYB5qm4
源代码(包含jar包):http://pan.baidu.com/s/1slcXq4t
登录的流程:
一个登录界面,输入用户名和密码,表单提交到Action,Action通过ModelDriven 获取数据,把数据和数据库进行查询匹配,调用业务层,业务层再调用DAO层,DAO层进行查询,在Action中判断返回查询是否为null,如果为null,Action返回一个登录错误的结果类型(如INPUT),再次回到登录页,可以this.addActionError("用户名或密码失败");
添加错误信息;如果返回结果不为null,把查询的信息保存到session中,通过EL表达式或者Structs标签,显示欢迎某某某。后台页面则是由3个页面拼接而成,菜单栏则是用js控件
<frameset rows="80,*">
<frame name="top"
src="${pageContext.request.contextPath}/frame/top.jsp">
<frameset cols="150,*" id="main">
<frame src="${pageContext.request.contextPath}/frame/left.jsp">
<frame name="right"
src="${pageContext.request.contextPath}/frame/right.jsp">
</frameset>
</frameset>
显示数据库的信息,则是把返回的信息保存到值栈中,页面中通过OGNL表达式获取,分页则是通过PageBean模型,保存各种总页数,分页大小,而且把数据库查询的信息也存到里面。
关键是Spring加入的影响,IOC注入的思想,以及HIbernate事务管理的特点
基本的配置文件
全部文件看源码
jdbc.properties
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/ssh_employee?characterEncoding=utf8&useSSL=true
jdbc.username=root
jdbc.password=root
log4j.properties
log4j.rootLogger=DEBUG,Console
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
log4j.logger.org.apache=INFO
log4j.logger.java.sql.ResultSet=INFO
log4j.logger.java.sql.PreparedStatement=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.Connection=DEBUG
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd ">
<!-- 引入外部的属性文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 配置c3p0 连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- 配置hibernate 相关属性-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<!-- 注入连接池-->
<property name="dataSource" ref="dataSource"></property>
<!-- hibernate 的相关属性 -->
<property name="hibernateProperties" >
<props>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.format_sql">true </prop>
<!--
它包含4个属性:
* create : 会根据你的model类来生成表,但是每次运行都会删除上一次的表,重新生成表,哪怕2次没有任何改变
* create-drop : 根据model类生成表,但是sessionFactory一关闭,表就自动删除
* update : 最常用的属性,也根据model类生成表,即使表结构改变了,表中的行仍然存在,不会删除以前的行
* validate : 只会和数据库中的表进行比较,不会创建新表,但是会插入新值
-->
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<!-- hibernate 映射文件 -->
<property name="mappingResources">
<list>
<value>com/jxust/employee/domain/Department.hbm.xml</value>
<value>com/jxust/employee/domain/Employee.hbm.xml</value>
</list>
</property>
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 开启事务注解 -->
<tx:annotation-driven transaction-manager="transactionManager" />
<!-- 配置Action 的类 action由spring管理-->
<bean id="employeeActionBean" class="com.jxust.employee.action.EmployeeAction" scope="prototype">
<property name="employeeService" ref="employeeServiceBean"></property>
<property name="departmentService" ref="departmentServiceBean"></property>
</bean>
<bean id="departmentActionBean" class="com.jxust.employee.action.DepartmentAction" scope="prototype">
<property name="departmentService" ref="departmentServiceBean"></property>
</bean>
<!-- 配置业务层的类 -->
<bean id="employeeServiceBean" class="com.jxust.employee.service.impl.EmployeeServiceImpl">
<property name="employeeDao" ref="employeeDaoBean"></property>
</bean>
<bean id="departmentServiceBean" class="com.jxust.employee.service.impl.DepartmentServiceImpl">
<property name="departmentDao" ref="departmentDaoBean"></property>
</bean>
<!-- 配置dao层的类 -->
<bean id="employeeDaoBean" class="com.jxust.employee.dao.impl.EmployeeDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="departmentDaoBean" class="com.jxust.employee.dao.impl.DepartmentDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
</beans>
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="default" namespace="/" extends="struts-default">
<interceptors >
<interceptor name="frameInterceptor" class="com.jxust.employee.interceptor.UserLoginInterceptor"></interceptor>
<!-- 自定义拦截器栈myStack,组合了defaultStack和auth -->
<interceptor-stack name="myStack">
<interceptor-ref name="defaultStack"></interceptor-ref>
<interceptor-ref name="frameInterceptor"></interceptor-ref>
</interceptor-stack>
</interceptors>
<action name="employee_*" method="{1}" class="employeeActionBean">
<result name ="loginSuccess">/WEB-INF/page/frame.jsp</result>
<result name="input">/index.jsp</result>
<result name="saveUI">/frame/addEmployee.jsp</result>
<result name="findAll">/frame/listEmployee.jsp</result>
<result name="editSuccess">/frame/editEmployee.jsp</result>
<result name="deleteSuccess" type="redirectAction">/employee_findAll.action</result>
<result name="updateSuccess" type="redirectAction">/employee_findAll.action</result>
<result name="saveSuccess" type="redirectAction">/employee_findAll.action</result>
</action>
<action name="department_*" method="{1}" class="departmentActionBean">
<result name="findAll">/frame/listDep.jsp</result>
<result name="savaUI">/frame/addDepartment.jsp</result>
<result name="editSuccess">/frame/editDepartment.jsp</result>
<!--重定向到Action,相当于刷新页面 -->
<result name="saveSuccess" type="redirectAction">/department_findAll.action</result>
<result name="updateSuccess" type="redirectAction">/department_findAll.action</result>
<result name="deleteSuccess" type="redirectAction">/department_findAll.action</result>
</action>
<!-- 通过此Action访问后台管理页面,需要判断用户是否已登录,如果登录,则跳转到登录页面-->
<action name="frame">
<result>/WEB-INF/page/frame.jsp</result>
<result name="login">/index.jsp</result>
<!-- 引用自定义的拦截器栈 -->
<interceptor-ref name="myStack"></interceptor-ref>
</action>
</package>
</struts>
DepartmentAction.java
package com.jxust.employee.action;
import com.jxust.employee.domain.Department;
import com.jxust.employee.domain.PageBean;
import com.jxust.employee.service.DepartmentService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
public class DepartmentAction extends ActionSupport implements ModelDriven<Department>{
private Department department = new Department();
@Override
public Department getModel() {
return department;
}
//当前页数
private Integer currPage =1;
public void setCurrPage(Integer currPage) {
this.currPage = currPage;
}
//注入部门管理的Service
private DepartmentService departmentService;
public void setDepartment(Department department) {
this.department = department;
}
public void setDepartmentService(DepartmentService departmentService) {
this.departmentService = departmentService;
}
//查询分页的方法
public String findAll(){
PageBean<Department> pageBean = departmentService.findByPage(currPage);
ActionContext.getContext().getValueStack().push(pageBean);
return "findAll";
}
//跳转到添加部门的页面的方法
public String saveUI(){
return "savaUI";
}
//添加部门的方法
public String save(){
departmentService.save(department);
return "saveSuccess";
}
//编辑部门的执行的方法,返回编辑页的数据
public String edit(){
//模型驱动的返回,默认在值栈之中
department = departmentService.findById(department.getDid());
return "editSuccess";
}
//修改部门的执行的方法
public String update(){
departmentService.update(department);
return "updateSuccess";
}
//删除部门的方法
public String delete(){
department = departmentService.findById(department.getDid());
//先查询再删除,这样可以级联删除
departmentService.delete(department);
return "deleteSuccess";
}
}