项目源码:https://github.com/joshul/ssh_1
软件环境
win7、mysql5.5、jdk1.8、tomcat 8
开发工具
idea + mysql Workbench
功能简述
- 首先是登陆页面,管理员登陆权限设定只能是人事部员工的才可以登陆。
- 登陆只能可以在部门管理栏和员工管理,部门管理可以进行添加新部门、删除旧部门和编辑操作。
- 员工管理同样也可以进行编辑、删除、添加新员工。
使用到的技术
- Spring
- Struts2
- hibernate
- c3p0连接池
- jsp
页面展示:
登录页
详情页
详情页
编辑页面
数据表
代码实现过程
1. 业务分析
需要有一个登陆页面,输入姓名和密码可实现登陆。进去到后台管理页面,页面中有人力资源管理部分,分别有部门管理和员工管理,当点击部门管理是会跳转到显示所有部门的页面,在这个页面中我们可以在这里添加或删除部门或修改,当然也具有分页的显示的功能。员工管理具有和部门管理一样的功能,也可以对员工进行添加、删除、编辑的管理,在添加员工栏中会有选择所属部门的选型框。
2. 编码过程
2.1建立工程导入jar包
jar包
2.2建立工程目录
工程目录
2.2导入工程所需配置文件
- 2.2.1
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
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_3_0.xsd">
<!-- Spring的框架的核心监听器 -->
<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>OpenSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>OpenSessionInViewFilter</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<!-- Struts2的框架的核心过滤器的配置 -->
<filter>
<filter-name>struts</filter-name>
<!-- FilterDispatcher是struts2.0.x到2.1.2版本的核心过滤器.StrutsPrepareAndExecuteFilter是自2.1.3开始就替代了FilterDispatcher.-->
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
web.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="ssh" extends="struts-default" namespace="/">
<!-- 注册拦截器 -->
<interceptors>
<interceptor name="AuthInterceptor" class="com.muke.employee.interceptor.AuthInterceptor"></interceptor>
<!-- 自定义一个拦截器栈 myStack:含默认的拦截器:defaultStack 和 自己创建的拦截器:AuthInterceptor -->
<interceptor-stack name="myStack">
<interceptor-ref name="defaultStack"></interceptor-ref>
<interceptor-ref name="AuthInterceptor">
<!-- AuthInterceptor继承 MethodFilterInterceptor类,所以可以使用excludeMethods将某些方法排除在拦截器之外-->
<param name="excludeMethods">login</param>
</interceptor-ref>
</interceptor-stack>
</interceptors>
<action name="employee_*" class="employeeAction" method="{1}">
<!-- 使用在/toIndex.jsp页面中使用top.window.location.href="/index.jsp"跳出frame框架 -->
<result name="input">/toIndex.jsp</result>
<result name="login">/toIndex.jsp</result>
<result name="success" type="redirect">/frame.jsp</result>
<result name="findAll">/jsp/employee/list.jsp</result>
<result name="saveUI">/jsp/employee/add.jsp</result>
<result name="saveSuccess" type="redirectAction">employee_findAll.action</result>
<result name="editSuccess">/jsp/employee/edit.jsp</result>
<result name="updateSuccess" type="redirectAction">employee_findAll.action</result>
<result name="deleteSuccess" type="redirectAction">employee_findAll.action</result>
<interceptor-ref name="myStack"></interceptor-ref>
</action>
<action name="department_*" class="departmentAction" method="{1}">
<!-- 使用在/toIndex.jsp页面中使用top.window.location.href="/index.jsp"跳出frame框架 -->
<result name="login">/toIndex.jsp</result>
<result name="findAll">/jsp/department/list.jsp</result>
<result name="saveUI">/jsp/department/add.jsp</result>
<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>
<result name="editSuccess">/jsp/department/edit.jsp</result>
<interceptor-ref name="myStack"></interceptor-ref>
</action>
</package>
</struts>
struts.xml
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssh_employee
jdbc.username=root
jdbc.password=root
jdbc.properties
<?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:aop="http://www.springframework.org/schema/aop"
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.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 引入外部的属性文件:连接池属性 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 配置连接池 -->
<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.hibernate3.LocalSessionFactoryBean">
<!-- 注入连接池 -->
<property name="dataSource" ref="dataSource"/>
<!-- 配置Hibernate的属性 -->
<property name="hibernateProperties">
<props>
<!-- 设置dialect方言为MySQL -->
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<!-- 是否显示sql语句:true -->
<prop key="hibernate.show_sql">true</prop>
<!-- 是否格式化sql语句:true -->
<prop key="hibernate.fomat_sql">true</prop>
<!-- hibernate.hbm2ddl.auto参数的作用主要用于:自动创建|更新|验证数据库表结构
validate:加载hibernate时,验证创建数据库表结构;
create:每次加载hibernate,重新创建数据库表结构;
create-drop:加载hibernate时创建,退出时删除表结构;
update:加载hibernate自动更新数据库结构 -->
<prop key="hibernate.hbm2ddl.auto" >update</prop>
</props>
</property>
<!-- 加载Hibernte的映射文件 -->
<property name="mappingResources">
<list>
<value>com/muke/employee/domain/Department.hbm.xml</value>
<value>com/muke/employee/domain/Employee.hbm.xml</value>
</list>
</property>
</bean>
<!-- **配置Action的类 -->
<bean id="employeeAction" class="com.muke.employee.action.EmployeeAction" scope="prototype">
<property name="employeeService" ref="employeeService"/>
<property name="departmentService" ref="departmentService"/>
</bean>
<bean id="departmentAction" class="com.muke.employee.action.DepartmentAction" scope="prototype">
<property name="departmentService" ref="departmentService"/>
</bean>
<!-- **配置Service业务层的类 -->
<bean id="employeeService" class="com.muke.employee.service.impl.EmployeeServiceImpl">
<property name="employeeDao" ref="employeeDao"/>
</bean>
<bean id="departmentService" class="com.muke.employee.service.impl.DepartmentServiceImpl">
<property name="departmentDao" ref="departmentDao"/>
</bean>
<!-- **配置 Dao的类 -->
<bean id="employeeDao" class="com.muke.employee.dao.impl.EmployeeDaoImpl">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="departmentDao" class="com.muke.employee.dao.impl.DepartmentDaoImpl">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 开启注解事务 -->
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>
applicationContext.xml