SSH项目实战-员工信息管理

来源:互联网 发布:nginx 1.10.2 编辑:程序博客网 时间:2024/04/19 23:30
员工信息管理系统

×需求分析

...

××功能模块分析

管理员模块

   注册/登录

员工模块

   1)添加一个员工

   2)对指定的员工信息修改

   3)删除选择的员工

   4)列表展示

×××设

一.系统设计

1)搭建系统框架结构

2)确定项目的关键点和难点

3)确定引用组件、共用类版本

   struts ***

   hibernate  ***

   spring  ***

   jdk  ***

二.数据库设计

管理员表

  t_admin

员工表

  t_employee

部门表

  t_dept

三.代码

编码顺序

  1)设计数据库

     ssh_demo

     建表  t_admin t_employee  t_dept

  2)建立web项目、引入jar文件、环境准备

  3)设计javabean,写映射

     Admin.java  封装管理员

    Employee.java 员工

    Dept.java部门

    Admin.hbm.xml

    Employee.hbm.xml

    Dept.hbm.xml

  4)设计接口

    IAdminDao.java 管理员模块

        void save(Admin admin)

        Admin findByAdmin(Admin admin)

    IDeptDao.java 部门模块

        List<Dept> getAll()

        findById(int id)

    IEmployee.java 员工模块

        void save(Employee emp);

        void update(Employee emp);

        void delete(int id);

        Employee findById(int id);

        List<Employee> getAll();

        List<Employee> getEmployee(String empName);

 5)dao接口实现

...

6)Service 接口设计

...

7)Service实现

...

8)Action实现

...

9)jsp页面 

   index.jsp 首页列表

系统优化

10)用户登录拦截器

  UserInterceptor 检查是否登录

11)Dao优化

   BaseDao.java所有dao的通用方法,dao都必须继承此类

实例代码  

数据库脚本

//部门表CREATE TABLE t_dept(    deptId INT(11) PRIMARY KEY NOT NULL AUTO_INCREMENT,    deptName VARCHAR(20));//员工表CREATE TABLE t_employee(    empId INT(11) PRIMARY KEY NOT NULL AUTO_INCREMENT,    empName VARCHAR(20),    salary DOUBLE,    dept_id INT(11),    CONSTRAINT FKFDCF5A19C3031517 FOREIGN KEY (dept_id) REFERENCES t_dept (deptId));CREATE INDEX FKFDCF5A19C3031517 ON t_employee (dept_id);//管理员表CREATE TABLE t_admin(    id INT(11) PRIMARY KEY  AUTO_INCREMENT,    adminName VARCHAR(20),    pwd VARCHAR(20));

  

实体类及其相关的映射配置

package com.cx.entity;/** * Created by cxspace on 16-8-12. */public class Admin {    private int id;    private String adminName;    private String pwd;    public String getAdminName() {        return adminName;    }    public void setAdminName(String adminName) {        this.adminName = adminName;    }    public int getId() {        return id;    }    public void setId(int id) {        this.id = id;    }    public String getPwd() {        return pwd;    }    public void setPwd(String pwd) {        this.pwd = pwd;    }}

  

<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"><hibernate-mapping package="com.cx.entity">    <class name="Admin" table="t_admin">        <id name="id" column="id">            <generator class="native"></generator>        </id>        <property name="adminName" column="adminName" ></property>        <property name="pwd" column="pwd" ></property>    </class></hibernate-mapping>

  

package com.cx.entity;import java.util.HashSet;import java.util.Set;/** * Created by cxspace on 16-8-2. */public class Dept {    private int deptId;    private String deptName;    public int getDeptId() {        return deptId;    }    public void setDeptId(int deptId) {        this.deptId = deptId;    }    public String getDeptName() {        return deptName;    }    public void setDeptName(String deptName) {        this.deptName = deptName;    }}

  

<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"><hibernate-mapping package="com.cx.entity">    <class name="Dept" table="t_dept">        <id name="deptId" column="deptId">            <generator class="native"></generator>        </id>        <property name="deptName" column="deptName"></property>    </class></hibernate-mapping>

  

package com.cx.entity;/** * Created by cxspace on 16-8-2. */public class Employee {    private int empId;    private String empName;    private double salary;    private Dept dept;    public Dept getDept() {        return dept;    }    public void setDept(Dept dept) {        this.dept = dept;    }    public int getEmpId() {        return empId;    }    public void setEmpId(int empId) {        this.empId = empId;    }    public String getEmpName() {        return empName;    }    public void setEmpName(String empName) {        this.empName = empName;    }    public double getSalary() {        return salary;    }    public void setSalary(double salary) {        this.salary = salary;    }}

  

<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"><hibernate-mapping package="com.cx.entity">    <class name="Employee" table="t_employee">        <id name="empId" column="empId">            <generator class="native"></generator>        </id>        <property name="empName" column="empName"></property>        <property name="salary" column="salary"></property>        <!--        多对一映射配置        Employee 映射关键点:        1.  映射的部门属性  :  dept        2.  映射的部门属性,对应的外键字段: dept_id        3.  部门的类型        -->        <many-to-one name="dept" column="dept_id" class="Dept" lazy="false"></many-to-one>    </class></hibernate-mapping>

  

dao 接口

package com.cx.dao;import com.cx.entity.Admin;/** * Created by cxspace on 16-8-12. */public interface IAdminDao {    /*       保存     */    public void save(Admin admin);    /*     根据管理员信息查询     */    Admin findByAdmin(Admin admin);}

  

package com.cx.dao;import java.util.List;/** * Created by cxspace on 16-8-12. */public interface IBaseDao<T> {    void save(T obj);    void update(T obj);    void delete(int id);    T findById(int id);    List<T> getAll();}

  

package com.cx.dao;import com.cx.entity.Dept;import java.util.List;/** * Created by cxspace on 16-8-12. * * 部门dao接口 */public interface IDeptDao {   /*     查询全部    */    public List<Dept> getAll();    /*      根据主键查询     */    public Dept findById(int id);}

  

package com.cx.dao;import com.cx.entity.Employee;import java.util.List;/** * Created by cxspace on 16-8-12. * * 员工模块的dao接口 * */public interface IEmployeeDao {    /*      保存员工     */    public void save(Employee emp);    /*     更新员工信息     */    public void update(Employee emp);    /*      根据主键删除     */    public void delete(int id);    /*      根据主键查询     */    public Employee findById(int id);    /*      查询全部     */    public List<Employee> getAll();    /*     根据员工名称查询     */    List<Employee> getAll(String empName);}

  

dao实现

package com.cx.dao.impl;import com.cx.dao.IAdminDao;import com.cx.entity.Admin;import org.hibernate.SessionFactory;/** * Created by cxspace on 16-8-12. */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();        //在不报错的情况下永远只有一个结果的时候,就用uniqueResult()    }    @Override    public void save(Admin admin) {         sessionFactory.getCurrentSession().save(admin);    }}

  

package com.cx.dao.impl;import com.cx.dao.IBaseDao;import org.hibernate.SessionFactory;import java.lang.reflect.ParameterizedType;import java.lang.reflect.Type;import java.util.List;/** * Created by cxspace on 16-8-12. */public class BaseDao<T> implements IBaseDao<T> {    private String className;    private SessionFactory sessionFactory;    public void setSessionFactory(SessionFactory sessionFactory) {        this.sessionFactory = sessionFactory;    }    public SessionFactory getSessionFactory() {        return sessionFactory;    }    //当前操作的实际的bean类型    private Class<T> clazz;    //反射泛型    public BaseDao(){        Type type = this.getClass().getGenericSuperclass();        //转换为参数化类型        ParameterizedType pt = (ParameterizedType) type;        //得到实际类型        Type types[] = pt.getActualTypeArguments();        clazz = (Class<T>) types[0];        className = clazz.getSimpleName();    }    @Override    public void delete(int id) {        sessionFactory.getCurrentSession().createQuery("delete from "+className+"where id = ?").setParameter(0,id).executeUpdate();    }    @Override    public void save(T obj) {        sessionFactory.getCurrentSession().save(obj);    }    @Override    public void update(T obj) {        sessionFactory.getCurrentSession().update(obj);    }    @Override    public T findById(int id) {        return (T) sessionFactory.getCurrentSession().get(clazz,id);    }    @Override    public List<T> getAll() {        return sessionFactory.getCurrentSession().createQuery("from "+className).list();    }}

  

package com.cx.dao.impl;import com.cx.dao.IDeptDao;import com.cx.entity.Dept;import org.hibernate.SessionFactory;import java.util.List;/** * Created by cxspace on 16-8-12. */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);    }    @Override    public List<Dept> getAll() {        return (List<Dept>)sessionFactory.getCurrentSession().createQuery("from Dept").list();    }}

  

package com.cx.dao.impl;import com.cx.dao.IEmployeeDao;import com.cx.entity.Employee;import org.hibernate.SessionFactory;import java.util.List;/** * Created by cxspace on 16-8-12. */public class EmployeeDao extends BaseDao<Employee> implements IEmployeeDao{    /*    加了泛型模板后这些全部不用写    private SessionFactory sessionFactory;    public void setSessionFactory(SessionFactory sessionFactory) {        this.sessionFactory = sessionFactory;    }    @Override    public void delete(int id) {        sessionFactory.getCurrentSession().createQuery("delete from Employee where empId = ?")                      .setParameter(0,id).executeUpdate();    }    @Override    public void save(Employee emp) {        sessionFactory.getCurrentSession().save(emp);    }    @Override    public void update(Employee emp) {        sessionFactory.getCurrentSession().update(emp);    }    @Override    public Employee findById(int id) {        return (Employee) sessionFactory.getCurrentSession().get(Employee.class,id);    }    @Override    public List<Employee> getAll() {        return (List<Employee>) sessionFactory.getCurrentSession().createQuery("from Employee").list();    }    @Override    public List<Employee> getAll(String empName) {        return (List<Employee>) sessionFactory.getCurrentSession()                .createQuery("from Employee where empName = ?")                .setParameter(0,"%"+empName+"%").list();    }    */    @Override    public List<Employee> getAll(String empName) {        return (List<Employee>) getSessionFactory().getCurrentSession()                .createQuery("from Employee where empName = ?")                .setParameter(0,"%"+empName+"%").list();    }}

  

service接口

package com.cx.service;import com.cx.entity.Admin;/** * Created by cxspace on 16-8-12. */public interface IAdminService {    /*     注册     */    public void register(Admin admin);    /*     登录     */    public Admin login(Admin admin);}

  

package com.cx.service;import com.cx.entity.Dept;import java.util.List;/** * Created by cxspace on 16-8-12. */public interface IDeptService {    /*     chauncey全部信息     */    public List<Dept> getAll();    /*     根据主键查询     */    public Dept findById(int id);}

  

package com.cx.service;import com.cx.entity.Employee;import java.util.List;/** * Created by cxspace on 16-8-12. */public interface IEmployeeService {    /*    保存员工   */    public void save(Employee emp);    /*     更新员工信息     */    public void update(Employee emp);    /*      根据主键删除     */    public void delete(int id);    /*      根据主键查询     */    public Employee findById(int id);    /*      查询全部     */    public List<Employee> getAll();    /*     根据员工名称查询     */    List<Employee> getAll(String empName);    /*     删除多个员工     */    public void deleteMany(int [] ids);}

  

service实现

package com.cx.service.impl;import com.cx.dao.IAdminDao;import com.cx.entity.Admin;import com.cx.service.IAdminService;/** * Created by cxspace on 16-8-12. */public class AdminService implements IAdminService{    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);    }}

  

package com.cx.service.impl;import com.cx.dao.IDeptDao;import com.cx.entity.Dept;import com.cx.service.IDeptService;import java.util.List;/** * Created by cxspace on 16-8-12. */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();    }}

  

package com.cx.service.impl;import com.cx.dao.IEmployeeDao;import com.cx.entity.Employee;import com.cx.service.IEmployeeService;import java.util.List;/** * Created by cxspace on 16-8-12. */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 save(Employee emp) {        employeeDao.save(emp);    }    @Override    public void update(Employee emp) {        employeeDao.update(emp);    }    @Override    public Employee findById(int id) {        return employeeDao.findById(id);    }    @Override    public List<Employee> getAll() {        return employeeDao.getAll();    }    @Override    public List<Employee> getAll(String empName) {        return employeeDao.getAll(empName);    }    @Override    public void deleteMany(int[] ids) {        if (ids != null && ids.length > 0){            for(int id : ids){                delete(id);            }        }    }}

  

action

package com.cx.action;import com.cx.entity.Admin;import com.cx.service.IAdminService;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionSupport;import com.opensymphony.xwork2.ModelDriven;/** * Created by cxspace on 16-8-12. */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() throws Exception {        Admin adminInfo = adminService.login(admin);        //验证        if (adminInfo==null){            return "loginFaild";        }else {            ActionContext.getContext().getSession().put("adminInfo",adminInfo);            return "index";        }    }}

  

package com.cx.action;import com.cx.entity.Dept;import com.cx.entity.Employee;import com.cx.service.IDeptService;import com.cx.service.IEmployeeService;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionSupport;import com.opensymphony.xwork2.ModelDriven;import com.opensymphony.xwork2.util.ValueStack;import org.apache.struts2.interceptor.RequestAware;import java.util.List;import java.util.Map;/** * Created by cxspace on 16-8-12. */public class EmployeeAction extends ActionSupport implements RequestAware,ModelDriven<Employee>{    /*        数据封装-模型驱动     */    private Employee employee = new Employee();    public void setEmployee(Employee employee) {        this.employee = employee;    }    private int deptId;    public void setDeptId(int deptId) {        this.deptId = deptId;    }    public Employee getEmployee() {        return employee;    }    @Override    public Employee getModel() {        return employee;  //返回实例化后的对象    }    private IEmployeeService employeeService;    private IDeptService deptService;    public void setDeptService(IDeptService deptService) {        this.deptService = deptService;    }    public void setEmployeeService(IEmployeeService employeeService) {        this.employeeService = employeeService;    }    /*    列表显示     */    public String list(){        System.out.println("进入action");        List<Employee> listEmp = employeeService.getAll();        Map<String,Object> request = (Map<String, Object>) ActionContext.getContext().get("request");        request.put("listEmp",listEmp);        System.out.println("查到数据");        return "list";    }    /*        添加员工-进入添加员工页面     */    public String viewAdd(){        List<Dept> listDept = deptService.getAll();        request.put("listDept",listDept);        return "add";    }    /*       添加员工逻辑     */    public String save(){        //调用service保存员工        Dept dept = deptService.findById(deptId);        //设置到员工对象中        employee.setDept(dept);        employeeService.save(employee);        return "listAction"; //重定向到action    }    /*      修改员工-进入修改视图     */    public String viewUpdate(){        System.out.println("a");        //获取Id        int id = employee.getEmpId();        System.out.println("b");        //根据员工主键查询,关闭懒加载,已经查到部门信息        Employee emp = employeeService.findById(id);        //查询所有的部门        System.out.println("c");        List<Dept> listDept = deptService.getAll();        //数据回显        System.out.println("d");        ValueStack vs = ActionContext.getContext().getValueStack();        System.out.println("e");        vs.pop();        System.out.println("f");        vs.push(emp); //入栈        //保存        request.put("listDept",listDept);        return "edit";    }    public String update(){        //根据部门id,查询部门对象;再设置到员工属性中        Dept dept = deptService.findById(deptId);        employee.setDept(dept);        //更新员工        employeeService.update(employee);        return "listAction";    }    /*        删除员工信息     */    public String delete(){        //获取要删除员工的主键        int empId = employee.getEmpId();        employeeService.delete(empId);        return "listAction";    }    private Map<String, Object> request;    @Override    public void setRequest(Map<String, Object> request) {        this.request = request;    }}

  

未登录拦截器

package com.cx.action;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionInvocation;import com.opensymphony.xwork2.interceptor.AbstractInterceptor;/** * Created by cxspace on 16-8-12. * * 校验用户是否登录 */public class UserInterceptor extends AbstractInterceptor {    @Override    public String intercept(ActionInvocation actionInvocation) throws Exception {        String methodName = actionInvocation.getProxy().getMethod();        ActionContext ac = actionInvocation.getInvocationContext();        Object obj = ac.getSession().get("adminInfo");        if (!"login".equals(methodName) && !"list".equals(methodName)) {            if (obj == null) {                return "login";            } else {                return actionInvocation.invoke();            }        }else {            return actionInvocation.invoke();        }    }}

  

spring配置

<?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="com/cx/config/bean-base.xml"/>    <import resource="com/cx/config/bean-dao.xml"/>    <import resource="com/cx/config/bean-service.xml"/>    <import resource="com/cx/config/bean-action.xml"/></beans>

  

<?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">    <!--连接池-->    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>        <property name="jdbcUrl" value="jdbc:mysql:///EmpSys"></property>        <property name="user" value="root"></property>        <property name="password" value="33269456.cx"></property>        <property name="initialPoolSize" value="3"></property>        <property name="maxPoolSize" value="10"></property>        <property name="maxStatements" value="100"></property>        <property name="acquireIncrement" value="2"></property>    </bean>    <!--spring管理sessionFactory,注入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.MySQL5Dialect</prop>                <prop key="hibernate.show_sql">true</prop>                <prop key="hibernate.hbm2ddl.auto">update</prop>            </props>        </property>        <property name="mappingLocations">            <list>                <value>classpath:com/cx/entity/*.hbm.xml</value>            </list>        </property>    </bean>    <!--事务相关配置-->    <!--事务管理类-->    <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">        <property name="sessionFactory" ref="sessionFactory"></property>    </bean>    <!--事务增强-->    <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>    <!--aop配置-->    <aop:config>        <aop:pointcut id="pt" expression="execution(* com.cx.service.impl.*.*(..))"/>        <aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/>    </aop:config></beans>

  

<?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">    <bean id="adminDao" class="com.cx.dao.impl.AdminDao">        <property name="sessionFactory" ref="sessionFactory"></property>    </bean>    <bean id="deptDao" class="com.cx.dao.impl.DeptDao">    <property name="sessionFactory" ref="sessionFactory"></property>    </bean>    <bean id="employeeDao" class="com.cx.dao.impl.EmployeeDao">        <property name="sessionFactory" ref="sessionFactory"></property>    </bean></beans>

  

<?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">    <bean id="deptService" class="com.cx.service.impl.DeptService">        <property name="deptDao" ref="deptDao"></property>    </bean>    <bean id="adminService" class="com.cx.service.impl.AdminService">        <property name="adminDao" ref="adminDao"></property>    </bean>    <bean id="employeeService" class="com.cx.service.impl.EmployeeService">        <property name="employeeDao" ref="employeeDao"></property>    </bean></beans>

  

<?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">    <bean id="employeeAction" class="com.cx.action.EmployeeAction">        <property name="employeeService" ref="employeeService"></property>        <property name="deptService" ref="deptService"></property>    </bean></beans>

  

struts配置

<?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" extends="struts-default">        <!--拦截器配置-->        <interceptors>            <interceptor name="userInterceptor" class="com.cx.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="error">/error/error.jsp</result>            <result name="null">/error/null.jsp</result>            <result name="login">/login.jsp</result>        </global-results>        <!--全局异常-->        <global-exception-mappings>            <exception-mapping exception="java.lang.Exception" result="error"></exception-mapping>            <exception-mapping exception="java.lang.NullPointerException" result="null"></exception-mapping>        </global-exception-mappings>        <!--action创建交给spring容器完成-->        <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="com.cx.action.AdminAction" method="{1}">            <result name="loginFaild">/login.jsp</result>            <result name="index" type="redirectAction">emp_list</result>        </action>    </package></struts>

  

web.xml总配置

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"         version="3.1">    <!-- 配置spring的OpenSessionInView模式 【目的:JSP页面访问懒加载数据】 必须陪到struts前面-->    <filter>        <filter-name>OpenSessionInView</filter-name>        <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>    </filter>    <filter-mapping>        <filter-name>OpenSessionInView</filter-name>        <url-pattern>*.action</url-pattern>    </filter-mapping>    <!--struts配置-->    <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>    <!--spring配置-->    <context-param>        <param-name>contextConfigLocation</param-name>        <param-value>classpath:bean.xml</param-value>    </context-param>    <listener>        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>    </listener></web-app>

  

页面

error.jsp

<%--  Created by IntelliJ IDEA.  User: cxspace  Date: 16-8-12  Time: 下午4:28  To change this template use File | Settings | File Templates.--%><%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head>    <title>错误页面</title></head><body>   发生错误</body></html>

  

null.jsp

<%--  Created by IntelliJ IDEA.  User: cxspace  Date: 16-8-12  Time: 下午4:30  To change this template use File | Settings | File Templates.--%><%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head>    <title>error</title></head><body>空指针异常</body></html>

  

add.jsp

<%@ taglib prefix="s" uri="/struts-tags" %><%--  Created by IntelliJ IDEA.  User: cxspace  Date: 16-8-12  Time: 下午8:08  To change this template use File | Settings | File Templates.--%><%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head>    <title>添加</title></head><body>    <s:form action="/emp_save.action" method="POST" theme="simple">        <table border="1" align="center" cellpadding="5" cellspacing="0">            <tr>                <td>员工姓名</td>                <td>                    <s:textfield name="empName" id="empName" value=""></s:textfield>                </td>            </tr>            <tr>                <td>员工薪水</td>                <td>                    <s:textfield name="salary" id="salary" value=""></s:textfield>                </td>            </tr>            <tr>                <td>选择部门</td>                <td>                    <!--                    <select name="">                        <option value="">请选择</option>                        <option value=""></option>                    </select>                    -->                    <!--struts下拉列表标签-->                    <s:select list="#request.listDept" headerKey="-1"                              headerValue="请选择" listValue="deptName"                              name="deptId" listKey="deptId" value="-1">                    </s:select>                </td>            </tr>            <tr>                <td colspan="2">                    <s:submit value="添加员工"></s:submit>                </td>            </tr>        </table>    </s:form></body></html>

  

edit.jsp

<%@ taglib prefix="s" uri="/struts-tags" %><%--  Created by IntelliJ IDEA.  User: cxspace  Date: 16-8-12  Time: 下午9:21  To change this template use File | Settings | File Templates.--%><%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head>    <title>修改</title></head><body><s:form action="/emp_update.action" method="POST" theme="simple">    <!--修改操作,隐藏域保存主键值-->    <s:hidden name="id" id="id" value="%{empId}"></s:hidden>    <table border="1" align="center" cellpadding="5" cellspacing="0">        <tr>            <td>员工姓名</td>            <td>                <s:textfield name="empName" id="empName"></s:textfield>            </td>        </tr>        <tr>            <td>员工薪水</td>            <td>                <s:textfield name="salary" id="salary"></s:textfield>            </td>        </tr>        <tr>            <td>选择部门</td>            <td>                <s:select list="#request.listDept" headerKey="-1"                          headerValue="请选择" listValue="deptName"                          name="deptId" listKey="deptId" value="dept.deptId">                </s:select>            </td>        </tr>        <tr>            <td colspan="2">                <s:submit value="修改员工信息"></s:submit>            </td>        </tr>    </table></s:form></body></html>

  

list.jsp

<%@ taglib prefix="s" uri="/struts-tags" %><%--  Created by IntelliJ IDEA.  User: cxspace  Date: 16-8-12  Time: 上午10:24  To change this template use File | Settings | File Templates.--%><%@ page contentType="text/html;charset=UTF-8" language="java" %><html>  <head>    <title>list</title>  </head>  <body>  <div align="center" style="width: 80%">    <s:if test="#session.adminInfo != null">      欢迎你:<s:property value="#session.adminInfo.adminName"/>             <s:a href="#">退出</s:a>    </s:if>    <s:else>      <s:a href="admin_login">登录</s:a>      <s:a href="#">注册</s:a>    </s:else>    <s:a href="emp_viewAdd">添加员工</s:a>  </div>     <table border="1" align="center" width="80%" cellspacing="0" cellpadding="5">       <tr>         <th>序号</th>         <th>员工编号</th>         <th>员工姓名</th>         <th>员工薪水</th>         <th>操作</th>       </tr>       <s:if test="#request.listEmp != null">         <s:iterator var="emp" value="#request.listEmp" status="st">           <tr>           <td><s:property value="#st.count"></s:property></td>           <td><s:property value="#emp.empId"></s:property></td>           <td><s:property value="#emp.empName"></s:property></td>           <td><s:property value="#emp.salary"></s:property></td>           <td>             <s:a href="emp_viewUpdate?empId=%{#emp.empId}"> 修改 </s:a>             <s:a href="emp_delete?empId=%{#emp.empId}"> 删除 </s:a>           </td>           </tr>         </s:iterator>       </s:if>       <s:else>         <tr>           <td colspan="5">对不起,没有找到数据</td>         </tr>       </s:else>     </table>  </body></html>

login.jsp

<%@ taglib prefix="s" uri="/struts-tags" %><%--  Created by IntelliJ IDEA.  User: cxspace  Date: 16-8-12  Time: 下午11:26  To change this template use File | Settings | File Templates.--%><%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head>    <title>登录</title></head><body><s:form action="/admin_login" method="POST"><table border="1" align="center" width="80%" cellspacing="0" cellpadding="5">    <tr>        <td>管理员帐号:</td>        <td>            <s:textfield name="adminName" id="adminName" value=""></s:textfield>        </td>    </tr>    <tr>        <td>密码:</td>        <td>            <s:password name="pwd" id="pwd" value=""></s:password>        </td>    </tr>    <tr>        <td colspan="2">            <s:submit value="登录"></s:submit>        </td>    </tr></table></s:form></body></html>

  

源码github链接 源码github链接 

 

0 0
原创粉丝点击