17、简单的企业人事管理系统(ssh)

    xiaoxiao2022-06-28  27

    –声明,本博客仅本人用于学习笔记 三大框架: Struts框架 1. params拦截器: 请求数据封装 2. 类型转换/数据处理 3. struts配置 4. 文件上传/下载/国际化处理 5. 数据效验/拦截器 6. Ognl表达式 7. 数据回显/模型驱动/防止表单重复提交

    Hibernate框架 1. Api简介 2. 映射 多对一/一对多: 部门与员工 多对多/一对一 集合映射/组件映射/继承映射 3. Hibernate 缓存 4. 常用属性:inverse/lazy/cascade 5. 对象状态:临时/持久/游离

    Spring框架 1) Spring IOC容器 【Inversion Of Control,即控制反转】 创建对象/处理对象依赖关系 2) Aop编程 3) Spring声明式事务管理 4) 框架整合 Spring与Struts Spring与Hibernate 5)Spring对jdbc、hibernate操作的封装 JdbcTemplate (dataSource)/ HibernateTemplate(SessionFactory)

    目标: 案例 【软件项目声明周期】

    需求分析 系统概述: 企业人事管理系统! 要求对员工信息进行维护; 后台系统先登陆,才能操作员工: 添加/修改/删除 没有登陆,只能查看列表,不能操作!

    功能分类: 【管理员模块:】 注册/登陆 【员工模块】 1) 添加一个员工, 指定添加的部门 2) 对指定的员工信息修改 3) 删除选择员工 4)列表展示

    设计 2.1 系统设计 【系统架构师/技术经理】 主要做下面的事情, 1) 搭建系统框架结构 (基于mvc结构应用) 2) 确定项目的关键点/难点 3) 确定引用组件、公用类的版本 Struts2.3 Hibernate3.6 Spring3.2 2.2 数据库设计 管理员表: t_admin 员工表: t_employee 部门: t_dept

    代码 步骤分析 编码顺序:

    1) 设计数据库: hib_demo 建表: t_admin/t_employee/t_dept 2) 建立web项目、引入jar文件、准备环境 …..

    3) 设计javvabean、写映射 Admin.java 封装管理员 Employee.java 员工 Dept.java 部门

    Admin.hbm.xml Employee.hbm.xml Dept.hbm.xml

    4) Dao设计接口 IAdminDao.java 管理员模块 void save(Admin admin); Admin findByAdmin(Admin admin); IDeptDao.java 部门模块 List getAll(); Dept findById(int id); IEmployeeDao.java 员工模块 Void save(Employee emp); Void update(Employee emp); Void delete(int id); Employee findById(int id); List getAll(); List getAll(String employeeName); 5) Dao接口实现

    6)Service接口设计 IAdminService.java 管理员模块 void register(Admin admin); Admin login(Admin admin); 7)Service接口实现 8) Action实现 EmployeeAction.java 员工模块 AdminAction.java 管理员模块 9)jsp页面 Index.jsp/list.jsp 首页列表 http://localhost:8080/项目 首页列表

    优化部分: 10) 用户登陆拦截器 UserInterceptor.java 检查是否登陆 只有登陆才能操作; 否则只能查看 11) Dao 操作优化 BaseDao.java 所有dao的通用方法,dao都必须继承此类 (反射泛型)

    实现步骤代码:

    1) 设计数据库: hib_demo CREATE TABLE t_admin( id INT PRIMARY KEY AUTO_INCREMENT, adminName VARCHAR(20), pwd VARCHAR(20) ) 表: t_dept/ t_employee 2) 建立web项目、引入jar文件、准备环境 【struts相关jar】 commons-fileupload-1.2.2.jar commons-io-2.0.1.jar commons-lang3-3.1.jar freemarker-2.3.19.jar javassist-3.11.0.GA.jar ognl-3.0.5.jar struts2-core-2.3.4.1.jar xwork-core-2.3.4.1.jar

    【hibernate 相关 jar】 antlr-2.7.6.jar commons-collections-3.1.jar dom4j-1.6.1.jar hibernate3.jar hibernate-jpa-2.0-api-1.0.0.Final.jar javassist-3.12.0.GA.jar jta-1.1.jar slf4j-api-1.6.1.jar

    【Spring-core】 commons-logging-1.1.3.jar spring-beans-3.2.5.RELEASE.jar spring-context-3.2.5.RELEASE.jar spring-core-3.2.5.RELEASE.jar spring-expression-3.2.5.RELEASE.jar

    【Spring-web】 spring-web-3.2.5.RELEASE.jar struts2-spring-plugin-2.3.4.1.jar

    【Spring-Aop】 aopalliance.jar aspectjrt.jar aspectjweaver.jar spring-aop-3.2.5.RELEASE.jar

    【Spring-orm】 c3p0-0.9.1.2.jar mysql-connector-java-5.1.12-bin.jar spring-orm-3.2.5.RELEASE.jar spring-tx-3.2.5.RELEASE.jar spring-jdbc-3.2.5.RELEASE.jar

    结构图

    3) 设计javabean、写映射

    public class Admin { private int id; private String adminName; private String pwd; } <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="cn.itcast.entity"> <class name="Admin" table="t_admin"> <id name="id"> <generator class="native"></generator> </id> <property name="adminName" length="20"></property> <property name="pwd" length="20"></property> </class> </hibernate-mapping> public class Dept { private int id; private String name; } <hibernate-mapping package="cn.itcast.entity"> <class name="Dept" table="t_dept"> <id name="id" column="deptId"> <generator class="native"></generator> </id> <property name="name" column="deptName"></property> </class> </hibernate-mapping> public class Employee { private int id; private String empName; private double salary; private Dept dept; } <hibernate-mapping package="cn.itcast.entity"> <class name="Employee" table="t_employee"> <id name="id" column="empId"> <generator class="native"></generator> </id> <property name="empName"></property> <property name="salary"></property> <!-- 多对一 --> <many-to-one name="dept" column="dept_id" class="Dept"></many-to-one> </class> </hibernate-mapping>

    4) Dao设计接口

    IBaseDao

    /** * 所有dao的通用操作接口定义 * */ public interface IBaseDao<T> { /** * 保存 * @param emp */ void save(T emp); /** * 跟新对象信息 * @param emp */ void update(T emp); /** * 根据主键删除 * @param id */ void delete(int id); /** * 根据主键查询 * @param id * @return */ T findById(int id); /** * 查询全部 * @return */ List<T> getAll(); } /** * 部门模块dao接口设计 * */ public interface IDeptDao { /** * 查询全部 * @return 返回全部信息 */ List<Dept> getAll(); /** * 根据主键查询 * @param id 主键 * @return 返回查询后的结果 */ Dept findById(int id); } /** * 员工模块dao接口设计 */ public interface IEmployeeDao { /** * 保存员工 * @param emp */ void save(Employee emp); /** * 跟新员工信息 * @param emp */ void update(Employee emp); /** * 根据主键删除 * @param id */ void delete(int id); /** * 根据主键查询 * @param id * @return */ Employee findById(int id); /** * 查询全部 * @return */ List<Employee> getAll(); /** * 根据员工名称条件查询 * @param employeeName * @return */ List<Employee> getAll(String employeeName); } /** * 管理员模块dao接口 * */ public interface IAdminDao { /** * 保存 * @param admin 管理员对象 */ void save(Admin admin); /** * 根据管理员信息查询 * @param admin 管理员对象 * @return 返回查询后的结果 */ Admin findByAdmin(Admin admin); }

    Dao实现方法

    /** * 所有dao的通用操作,希望所有的dao都继承此类 * * * @param <T> */ public class BaseDao<T> implements IBaseDao<T> { // 当前操作的实际的bean类型 private Class<T> clazz; // 获取类名称 private String className; // 反射泛型 public BaseDao(){ Type type = this.getClass().getGenericSuperclass(); // 转换为参数化类型 ParameterizedType pt = (ParameterizedType) type; // BaseDao<Employee> // 得到实际类型 Type types[] = pt.getActualTypeArguments(); // 获取实际类型 clazz = (Class<T>) types[0]; className = clazz.getSimpleName();//例如:Employee } // 容器注入 private SessionFactory sessionFactory; public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } public SessionFactory getSessionFactory() { return sessionFactory; } @Override public void delete(int id) { sessionFactory .getCurrentSession() .createQuery("delete from " + className + " where id=?") .setParameter(0, id).executeUpdate(); } @SuppressWarnings("unchecked") @Override public T findById(int id) { return (T) sessionFactory.getCurrentSession().get(clazz, id); } @SuppressWarnings("unchecked") @Override public List<T> getAll() { return sessionFactory.getCurrentSession().createQuery("from " + className).list(); } @Override public void save(T t) { sessionFactory.getCurrentSession().save(t); } @Override public void update(T t) { sessionFactory.getCurrentSession().update(t); } } public class AdminDao implements IAdminDao { // IOC容器(依赖)注入SessionFactory对象 private SessionFactory sessionFactory; public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } @Override public Admin findByAdmin(Admin admin) { return (Admin) sessionFactory.getCurrentSession()// .createQuery("from Admin where adminName=? and pwd=?")// .setString(0, admin.getAdminName())// .setString(1, admin.getPwd())// .uniqueResult(); } @Override public void save(Admin admin) { sessionFactory.getCurrentSession().save(admin); } } public class DeptDao implements IDeptDao { // 容器注入 private SessionFactory sessionFactory; public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } @Override public Dept findById(int id) { return (Dept) sessionFactory.getCurrentSession().get(Dept.class, id); } @SuppressWarnings("unchecked") @Override public List<Dept> getAll() { return sessionFactory.getCurrentSession().createQuery("from Dept").list(); } } public class EmployeeDao extends BaseDao<Employee> implements IEmployeeDao { @SuppressWarnings("unchecked") @Override public List<Employee> getAll(String employeeName) { return getSessionFactory().getCurrentSession()// .createQuery("from Employee where empName like ?")// .setParameter(0, "%" +employeeName + "%")// .list(); } @Override public Employee findById(int id) { String hql = "from Employee e left join fetch e.dept where e.id=?"; return (Employee) getSessionFactory() .getCurrentSession() .createQuery(hql) .setParameter(0, id) .uniqueResult(); } }

    5) Service设计接口

    /** * 管理员业务逻辑层接口 * * * */ public interface IAdminService { /** * 注册 * @param admin */ void register(Admin admin); /** * 登陆 * @param admin * @return */ Admin login(Admin admin); } /** * 部门模块业务逻辑层接口 * * */ public interface IDeptService { /** * 查询全部 * @return 返回全部信息 */ List<Dept> getAll(); /** * 根据主键查询 * @param id 主键 * @return 返回查询后的结果 */ Dept findById(int id); } /** * 员工模块业务逻辑层接口 * * */ public interface IEmployeeService { /** * 保存员工 * @param emp */ void save(Employee emp); /** * 跟新员工信息 * @param emp */ void update(Employee emp); /** * 根据主键查询 * @param id * @return */ Employee findById(int id); /** * 查询全部 * @return */ List<Employee> getAll(); /** * 根据员工名称条件查询 * @param employeeName * @return */ List<Employee> getAll(String employeeName); /** * 根据主键删除 * @param id */ void delete(int id); /** * 删除多个员工 */ void deleteMany(int[] ids); }

    Service实现

    public class AdminService implements IAdminService { // 注入dao 【这里一定要用接口接收】 private IAdminDao adminDao; //JDK public void setAdminDao(IAdminDao adminDao) { this.adminDao = adminDao; } @Override public Admin login(Admin admin) { return adminDao.findByAdmin(admin); } @Override public void register(Admin admin) { adminDao.save(admin); } } public class DeptService implements IDeptService { private IDeptDao deptDao; public void setDeptDao(IDeptDao deptDao) { this.deptDao = deptDao; } @Override public Dept findById(int id) { return deptDao.findById(id); } @Override public List<Dept> getAll() { return deptDao.getAll(); } } public class EmployeeService implements IEmployeeService { // 容器注入 private IEmployeeDao employeeDao; public void setEmployeeDao(IEmployeeDao employeeDao) { this.employeeDao = employeeDao; } @Override public void delete(int id) { employeeDao.delete(id); } @Override public void deleteMany(int[] ids) { if (ids != null && ids.length >0) { for (int id : ids){ delete(id); } } } @Override public Employee findById(int id) { return employeeDao.findById(id); } @Override public List<Employee> getAll() { return employeeDao.getAll(); } @Override public List<Employee> getAll(String employeeName) { return employeeDao.getAll(employeeName); } @Override public void save(Employee emp) { employeeDao.save(emp); } @Override public void update(Employee emp) { employeeDao.update(emp); } }

    6) Action设计

    /** * 管理员登陆注册模块 * 1. 登陆 * * */ public class AdminAction extends ActionSupport implements ModelDriven<Admin> { // 封装请求数据 private Admin admin = new Admin(); public void setAdmin(Admin admin) { this.admin = admin; } public Admin getAdmin() { return admin; } @Override public Admin getModel() { return admin; } // 调用Service private IAdminService adminService; public void setAdminService(IAdminService adminService) { this.adminService = adminService; } /** * 登陆 */ public String login(){ // 登陆验证 Admin adminInfo = adminService.login(admin); // 验证 if (adminInfo == null){ // 登陆失败 return "loginFaild"; } else { // 登陆成功, 保存数据到session ActionContext.getContext().getSession().put("adminInfo", adminInfo); return "index"; } } } /** * 员工模块控制器开发: * 1. 员工列表展示 * 2. 添加员工 * 3. 修改员工信息 * 5. 删除 * * */ public class EmployeeAction extends ActionSupport implements ModelDriven<Employee>, RequestAware{ /*******一、封装数据********/ private Employee employee = new Employee(); // 【模型驱动】 // 封装请求的部门id(下拉列表的实际的值) private int deptId; public void setEmployee(Employee employee) { this.employee = employee; } public Employee getEmployee() { return employee; } public void setDeptId(int deptId) { this.deptId = deptId; } public int getDeptId() { return deptId; } @Override public Employee getModel() { return employee; // 返回实例化后的对象 } /*******二、注入员工Service********/ private IEmployeeService employeeService; public void setEmployeeService(IEmployeeService employeeService) { this.employeeService = employeeService; } // 部门Service private IDeptService deptService; public void setDeptService(IDeptService deptService) { this.deptService = deptService; } /** * 1. 员工列表展示 */ public String list() { // 查询所有员工 List<Employee> listEmp = employeeService.getAll(); // 保存到request request.put("listEmp", listEmp); return "list"; } /** * 2. 添加员工 - 进入添加页面 */ public String viewAdd(){ // 查询所有部门信息, 保存到request List<Dept> listDept = deptService.getAll(); request.put("listDept", listDept); return "add"; } /** * 2. 添加员工 - 添加员工数据 */ public String save(){ // 先根据部门主键查询 Dept dept = deptService.findById(deptId); // 设置到员工对象中 employee.setDept(dept); // 调用Service,保存员工 employeeService.save(employee); return "listAction"; // 重定向到Action } /** * 3. 修改员工信息 - 进入修改视图 */ public String viewUpdate(){ // 获取要修改的记录的id int id = employee.getId(); // 1. 根据员工的主键查询 (lazy="false") Employee emp = employeeService.findById(id); // 已经有部门信息 // 2. 查询所有的部门 List<Dept> listDept = deptService.getAll(); // 数据回显 ValueStack vs = ActionContext.getContext().getValueStack(); vs.pop();// 移除栈顶元素 vs.push(emp); // 入栈 // 保存 request.put("listDept", listDept); return "edit"; } /** * 4. 修改员工信息 - 确认修改 */ public String update() { //1. 先根据部门id, 查询部门对象; 再设置到员工属性中 Dept dept = deptService.findById(deptId); employee.setDept(dept); //2. 更新员工 employeeService.update(employee); return "listAction"; // 重定向到列表 } /** * 5. 修改员工信息 - 删除 */ public String delete(){ // 获取要删除员工的主键 int empId = employee.getId(); // 调用service删除 employeeService.delete(empId); return "listAction"; } // 接收框架运行时候传入的代表request对象的map private Map<String, Object> request; @Override public void setRequest(Map<String, Object> request) { this.request = request; } }

    7)拦截器

    /** * 效验用户是否登陆,只有登陆后才可以进行操作。 * 没有登陆,只能查看列表,不能操作! * * */ public class UserInterceptor extends AbstractInterceptor { @Override public String intercept(ActionInvocation invocation) throws Exception { // 得到当前执行的方法 String methodName = invocation.getProxy().getMethod(); // 得到ActionContext对象 ActionContext ac = invocation.getInvocationContext(); // 获取session, 从session中获取登陆的管理员账号 Object obj = ac.getSession().get("adminInfo"); // 判断: if (!"login".equals(methodName) && !"list".equals(methodName)){ // 验证 if (obj == null){ // 没有登陆 return "login"; } else { // 执行Action return invocation.invoke(); } } else { // 允许访问登陆、列表展示 return invocation.invoke(); } } }

    配置文件 bean.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:p="http://www.springframework.org/schema/p" 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.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"> <!-- 引入其他配置文件 --> <import resource="config/bean-base.xml"/> <import resource="config/bean-dao.xml"/> <import resource="config/bean-service.xml"/> <import resource="config/bean-action.xml"/> </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="emp" extends="struts-default"> <!-- 拦截器配置 --> <interceptors> <interceptor name="userInterceptor" class="cn.itcast.action.UserInterceptor"></interceptor> <interceptor-stack name="myStack"> <interceptor-ref name="defaultStack"></interceptor-ref> <interceptor-ref name="userInterceptor"></interceptor-ref> </interceptor-stack> </interceptors> <!-- 执行指定的拦截器 --> <default-interceptor-ref name="myStack"></default-interceptor-ref> <!-- 全局视图 --> <global-results> <result name="success">/index.jsp</result> <result name="login" type="redirect">/login.jsp</result> <!-- 错误视图配置 --> <result name="null">/error/null.jsp</result> <result name="error">/error/error.jsp</result> </global-results> <!-- 全局异常 --> <global-exception-mappings> <!-- result 会取找全局视图的名称 --> <exception-mapping result="null" exception="java.lang.NullPointerException"></exception-mapping> <exception-mapping result="error" exception="java.lang.Exception"></exception-mapping> </global-exception-mappings> <!-- Ation实例交给spring容器创建 --> <!-- 员工Action --> <action name="emp_*" class="employeeAction" method="{1}"> <!-- 列表展示 --> <result name="list">/WEB-INF/list.jsp</result> <!-- 进入添加页面视图 --> <result name="add">/WEB-INF/add.jsp</result> <!-- 添加成功,进入列表 (防止刷新就多一条记录问题,所以用重定向) --> <result name="listAction" type="redirectAction">emp_list</result> <!-- 进入修改页面 --> <result name="edit">/WEB-INF/edit.jsp</result> </action> <!-- 管理员Action --> <action name="admin_*" class="adminAction" method="{1}"> <!-- 登陆失败 --> <result name="loginFaild">/login.jsp</result> <!-- 登陆成功 --> <result name="index" type="redirectAction">emp_list</result> </action> </package> </struts>

    8)config配置文件

    bean-base.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:p="http://www.springframework.org/schema/p" 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.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"> <!-- 1. 连接池实例 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/hib_demo"></property> <property name="driverClass" value="com.mysql.jdbc.Driver"></property> <property name="user" value="root"></property> <property name="password" value="root"></property> <property name="initialPoolSize" value="3"></property> <property name="maxPoolSize" value="6"></property> </bean> <!-- 2. Spring管理SessionFactory 【全部配置都写到spring中】 --> <!-- # 注入DataSource、 注入常用配置属性、映射配置属性 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> </props> </property> <property name="mappingLocations"> <list> <value>classpath:cn/itcast/entity/*.hbm.xml</value> </list> </property> </bean> <!-- 3. 事务相关配置 --> <!-- 3.1 事务管理器类 --> <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- 3.2 事务增强(如何管理事务)--> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <tx:method name="get*" read-only="true"/> <tx:method name="find*" read-only="true"/> <tx:method name="*" read-only="false"/> </tx:attributes> </tx:advice> <!-- 3.3 Aop配置 = 切入点表达式(拦截目标对象,生成代理) + 事务增强应用--> <aop:config> <aop:pointcut expression="execution(* cn.itcast.service.impl.*.*(..))" id="pt"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/> </aop:config> </beans>

    bean-dao.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:p="http://www.springframework.org/schema/p" 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.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"> <!-- Dao 注入 SessionFactory --> <bean id="adminDao" class="cn.itcast.dao.impl.AdminDao"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <bean id="deptDao" class="cn.itcast.dao.impl.DeptDao"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <bean id="employeeDao" class="cn.itcast.dao.impl.EmployeeDao"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> </beans>

    bean-service.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:p="http://www.springframework.org/schema/p" 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.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"> <!-- Service 需要注入 Dao --> <bean id="adminService" class="cn.itcast.service.impl.AdminService"> <property name="adminDao" ref="adminDao"></property> </bean> <bean id="deptService" class="cn.itcast.service.impl.DeptService"> <property name="deptDao" ref="deptDao"></property> </bean> <bean id="employeeService" class="cn.itcast.service.impl.EmployeeService"> <property name="employeeDao" ref="employeeDao"></property> </bean> </beans>

    bean-action.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:p="http://www.springframework.org/schema/p" 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.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"> <!-- Action中需要注入Service --> <!-- 1. 员工管理模块 --> <bean id="employeeAction" class="cn.itcast.action.EmployeeAction" scope="prototype"> <property name="employeeService" ref="employeeService"></property> <property name="deptService" ref="deptService"></property> </bean> <!-- 2. 管理员模块 --> <bean id="adminAction" class="cn.itcast.action.AdminAction" scope="prototype"> <property name="adminService" ref="adminService"></property> </bean> </beans>

    9)Jsp编写

    heand.jsp

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@taglib uri="/struts-tags" prefix="s" %> <!-- 判断:如果用户登陆: 显示欢迎你,退出 --> <!-- 判断:如果用户没有登陆: 显示登陆、注册 --> <div align="center" style="width:80% "> <s:if test="#session.adminInfo != null"> 欢迎你: <s:property value="#session.adminInfo.adminName"/>   <s:a href="#">退出</s:a> <s:a href="emp_viewAdd">添加员工</s:a> </s:if> <s:else> <s:a href="login.jsp">登陆</s:a> <s:a href="register.jsp">注册</s:a> </s:else> </div>

    index.jsp

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%-- response.sendRedirect(request.getContextPath() + "/emp_list.action"); --%> <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <c:redirect url="/emp_list.action"></c:redirect>

    login.jsp

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@taglib uri="/struts-tags" prefix="s" %> <html> <head> <title>添加</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> </head> <body> <s:form action="/admin_login" method="post" theme="simple"> <table border="1" align="center" cellpadding="5" cellspacing="0"> <tr> <td>管理员账号</td> <td> <s:textfield name="adminName" id="adminName" value=""></s:textfield> </td> </tr> <tr> <td>密码:</td> <td> <s:textfield name="pwd" id="pwd" value=""></s:textfield> </td> </tr> <tr> <td colspan="2"> <s:submit value="登陆"></s:submit> </td> </tr> </table> </s:form> </body> </html>
    转载请注明原文地址: https://ju.6miu.com/read-1124214.html

    最新回复(0)