包及资源存放
lib下需要的包
web.xml配置
<?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">
<!-- 配置spring的监听器,用于初始化applicationcontext对象 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext*.xml</param-value>
</context-param>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>cn.itcast.oa.listener.InitServletContextListener</listener-class>
</listener>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
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: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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- 自动扫描与装备bean -->
<context:component-scan base-package="cn.itcast.oa"></context:component-scan>
<!-- 加载外部的配置文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 配置数据库连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- 基本信息 -->
<property name="jdbcUrl" value="${jdbcUrl}"></property>
<property name="driverClass" value="${driverClass}"></property>
<property name="user" value="${user}"></property>
<property name="password" value="${password}"></property>
<!-- 其他配置信息 -->
<!--初始化时获取三个连接,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
<property name="initialPoolSize" value="3"></property>
<!--连接池中保留的最小连接数。Default: 3 -->
<property name="minPoolSize" value="3"></property>
<!--连接池中保留的最大连接数。Default: 15 -->
<property name="maxPoolSize" value="5"></property>
<!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
<property name="acquireIncrement" value="3"></property>
<!-- 控制数据源内加载的PreparedStatements数量。如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0 -->
<property name="maxStatements" value="8"></property>
<!-- maxStatementsPerConnection定义了连接池内单个连接所拥有的最大缓存statements数。Default: 0 -->
<property name="maxStatementsPerConnection" value="5"></property>
<!--最大空闲时间,1800秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
<property name="maxIdleTime" value="1800"></property>
</bean>
<!-- 配置sessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
</bean>
<!-- 配置声明式的事务管理(采用基于注解的方式) -->
<bean name="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
sruts2配置
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!-- 配置为开发模式,(开发模式的好处是,修改配置后不需要重启服务,而且能打印错误) -->
<constant name="struts.devMode" value="true" />
<!-- 配置扩展名为action -->
<constant name="struts.action.extension" value="action" />
<constant name="struts.ui.theme" value="simple" />
<package name="default" namespace="/" extends="struts-default">
<!-- 拦截器 -->
<interceptors>
<!-- 声明一个拦截器 -->
<interceptor name="checkePrivilege" class="cn.itcast.oa.Interceptor.CheckePrivilegeInterceptor"></interceptor>
<!-- 重写defaultStack -->
<interceptor-stack name="defaultStack">
<interceptor-ref name="checkePrivilege"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</interceptor-stack>
</interceptors>
<!-- 配置全局的result -->
<global-results>
<result name="loginUI">/WEB-INF/jsp/userAction/loginUI.jsp</result>
<result name="noPrivilegeError">/noPrivilegeError.jsp</result>
</global-results>
<!-- 测试用的action -->
<action name="test" class="testAction">
<result name="success">/test.jsp</result>
</action>
<!-- 岗位 -->
<action name="roleAction_*" class="roleAction" method="{1}">
<result name="list">/WEB-INF/jsp/roleAction/list.jsp</result>
<result name="addUI">/WEB-INF/jsp/roleAction/addUI.jsp</result>
<result name="editUI">/WEB-INF/jsp/roleAction/editUI.jsp</result>
<result name="setPrivilegeUI">/WEB-INF/jsp/roleAction/setPrivilegeUI.jsp</result>
<result name="toList" type="redirectAction">roleAction_list</result>
</action>
<!-- 部门 -->
<action name="departmentAction_*" class="departmentAction" method="{1}">
<result name="list">/WEB-INF/jsp/departmentAction/list.jsp</result>
<result name="saveUI">/WEB-INF/jsp/departmentAction/saveUI.jsp</result>
<result name="toList" type="redirectAction">departmentAction_list?parentId=${parentId}</result>
</action>
<!-- 用户 -->
<action name="userAction_*" class="userAction" method="{1}">
<result name="list">/WEB-INF/jsp/userAction/list.jsp</result>
<result name="saveUI">/WEB-INF/jsp/userAction/saveUI.jsp</result>
<result name="toList" type="redirectAction">userAction_list</result>
<result name="toIndex">/index.jsp</result>
<result name="logout">/WEB-INF/jsp/userAction/logout.jsp</result>
</action>
<!-- 主页 -->
<action name="homeAction_*" class="homeAction" method="{1}">
<result name="{1}">/WEB-INF/jsp/homeAction/{1}.jsp</result>
</action>
</package>
</struts>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- 数据库信息 -->
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!--<property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
<property name="connection.url">jdbc:mysql:///gicasoftoa</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
-->
<!-- 其他配置信息 -->
<property name="show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 映射配置 -->
<!-- -->
<mapping resource="cn/itcast/oa/domain/User.hbm.xml" />
<mapping resource="cn/itcast/oa/domain/Role.hbm.xml" />
<mapping resource="cn/itcast/oa/domain/Department.hbm.xml" />
<mapping resource="cn/itcast/oa/domain/Privilege.hbm.xml" />
</session-factory>
</hibernate-configuration>
jdbc.properties配置
jdbcUrl= jdbc:mysql:///gicasoftoa?userUnicode=true&characterEncoding=gbk
driverClass =com.mysql.jdbc.Driver
user= root
password=root
基础的baseDao接口类
package cn.itcast.oa.base;
import java.util.List;
/***
* 用户增删改查的基础类
*
* @author Administrator
*
* @param <T>
*/
public interface BaseDao<T> {
/**
* 保存实体
*
* @param entity
*/
void save(T entity);
/**
* 按id号,删除实体
*
* @param id
*/
void delete(Long id);
/**
* 更新实体
*
* @param entity
* 实体
*/
void update(T entity);
/**
* 按单个id查询实体
*
* @param id
* 传入需要删除的实体id
* @return 返回实体
*/
T getById(Long id);
/**
* 查询实体返回list集合
*
* @param ids
* 需要删除实体的id集合
* @return 返回list实体集合
*/
List<T> getByIds(Long[] ids);
/**
* 查询所有
*
* @return 返回list实体集合
*/
List<T> findAll();
}
BaseDao的实现类BaseDaoImpl
package cn.itcast.oa.base.impl;
import java.lang.reflect.ParameterizedType;
import java.util.Collections;
import java.util.List;
import javax.annotation.Resource;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.transaction.annotation.Transactional;
import cn.itcast.oa.base.BaseDao;
@Transactional
public abstract class BaseDaoImpl<T> implements BaseDao<T> {
@Resource
private SessionFactory sessionFactory;
protected Class<T> clazz;
@SuppressWarnings("unchecked")
public BaseDaoImpl() {
// 通过反射得到T的真实类型
ParameterizedType pt = (ParameterizedType) this.getClass()
.getGenericSuperclass();
this.clazz = (Class<T>) pt.getActualTypeArguments()[0];
System.out.println("className:" + this.clazz.getName());
}
public void save(T entity) {
getSession().save(entity);
}
public void delete(Long id) {
Object obj = getSession().get(clazz, id);
getSession().delete(obj);
}
public void update(T entity) {
getSession().update(entity);
}
@SuppressWarnings("unchecked")
public T getById(Long id) {
return (T) getSession().get(clazz, id);
}
@SuppressWarnings("unchecked")
public List<T> getByIds(Long[] ids) {
if (ids == null || ids.length == 0) {
return Collections.EMPTY_LIST;
}
return getSession().createQuery(
" FROM " + clazz.getSimpleName() + " where id in(:ids)")
.setParameterList("ids", ids).list();
}
@SuppressWarnings("unchecked")
public List<T> findAll() {
return getSession().createQuery("FROM " + clazz.getSimpleName()).list();
}
/**
* 获取当前可用的session
*
* @return
*/
protected Session getSession() {
return sessionFactory.getCurrentSession();
}
}
BaseAction类
package cn.itcast.oa.base;
import java.lang.reflect.ParameterizedType;
import javax.annotation.Resource;
import cn.itcast.oa.service.DepartmentService;
import cn.itcast.oa.service.PrivilegeService;
import cn.itcast.oa.service.RoleService;
import cn.itcast.oa.service.UserService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
public abstract class BaseAction<T> extends ActionSupport implements ModelDriven<T> {
private static final long serialVersionUID = 1L;
@Resource
protected RoleService roleService;
@Resource
protected DepartmentService departmentService;
@Resource
protected UserService userService;
@Resource
protected PrivilegeService privilegeSrvice;
protected T model;
@SuppressWarnings("unchecked")
public BaseAction() {
try {
// 得到model的类型信息
ParameterizedType pt = (ParameterizedType) this.getClass()
.getGenericSuperclass();
Class clazz = (Class) pt.getActualTypeArguments()[0];
// 通过反射生成model的实例
model = (T) clazz.newInstance();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public T getModel() {
return model;
}
}
</pre>Department.java实体类<p></p><pre name="code" class="java">package cn.itcast.oa.domain;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
/***
* 部门
* @author Administrator
*
*/
public class Department implements Serializable{
private Long id;
private Set<User> users = new HashSet<User>();
private Department parent;
private Set<Department> children = new HashSet<Department>();
private String name;
private String description;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Set<User> getUsers() {
return users;
}
public void setUsers(Set<User> users) {
this.users = users;
}
public Department getParent() {
return parent;
}
public void setParent(Department parent) {
this.parent = parent;
}
public Set<Department> getChildren() {
return children;
}
public void setChildren(Set<Department> children) {
this.children = children;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
service接口类
package cn.itcast.oa.service;
import java.util.List;
import cn.itcast.oa.base.BaseDao;
import cn.itcast.oa.domain.Department;
public interface DepartmentService extends BaseDao<Department>{
/**
*
* @return
*/
List<Department> findTopList();
List<Department> findChildren(Long parentId);
}
package cn.itcast.oa.service.impl;
import java.util.List;
import org.springframework.stereotype.Service;
import cn.itcast.oa.base.impl.BaseDaoImpl;
import cn.itcast.oa.domain.Department;
import cn.itcast.oa.service.DepartmentService;
@Service
public class DepartmentServiceImpl extends BaseDaoImpl<Department> implements
DepartmentService {
public List<Department> findTopList() {
return getSession().createQuery(
"from Department d where d.parent is null").list();
}
public List<Department> findChildren(Long parentId) {
return getSession().createQuery(//
"FROM Department d WHERE d.parent.id=?")//
.setParameter(0, parentId)//
.list();
}
}
action类
package cn.itcast.oa.view.action;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import com.opensymphony.xwork2.ActionContext;
import cn.itcast.oa.base.BaseAction;
import cn.itcast.oa.base.ModelDrivenBaseAction;
import cn.itcast.oa.domain.Department;
import cn.itcast.oa.util.DepartmentUtil;
@Controller
@Scope("prototype")
public class DepartmentAction extends BaseAction<Department>{
private static final long serialVersionUID = 1L;
private Long parentId;
/**
* 查询所有部门
* @return
*/
public String list(){
List<Department> departmentList = null;
if(parentId==null){
//顶级部门
departmentList = departmentService.findTopList();
}else{//子部门
departmentList = departmentService.findChildren(parentId);
Department parent = departmentService.getById(parentId);
ActionContext.getContext().put("parent", parent);
}
ActionContext.getContext().put("departmentList", departmentList);
return "list";
}
/**
* 删除部门
* @return
*/
public String delete(){
departmentService.delete(model.getId());
return "toList";
}
/**
* 添加部门页面
* @return
*/
public String addUI(){
List<Department> topList = departmentService.findTopList();
List<Department> departmentList= DepartmentUtil.getAllDepartments(topList);
ActionContext.getContext().put("departmentList", departmentList);
return "saveUI";
}
/**
* 添加部门
* @return
*/
public String add(){
if(parentId!=null){
model.setParent(departmentService.getById(parentId));
}
departmentService.save(model);
return "toList";
}
/**
* 修改部门页面
* @return
*/
public String editUI(){
List<Department> departmentList = departmentService.findAll();
ActionContext.getContext().put("departmentList", departmentList);
Department dp = departmentService.getById(model.getId());
ActionContext.getContext().getValueStack().push(dp);
return "saveUI";
}
/**
* 修改部门
* @return
*/
public String edit(){
Department dp = departmentService.getById(model.getId());
dp.setName(model.getName());
dp.setDescription(model.getDescription());
departmentService.update(dp);
return "toList";
}
public Long getParentId() {
return parentId;
}
public void setParentId(Long parentId) {
this.parentId = parentId;
}
}