小项目:基于Struts框架的员工管理系统的java实现

来源:互联网 发布:阿里云客服怎么工作 编辑:程序博客网 时间:2024/05/18 01:19


1.导入struts包

c3p0-0.9.1.2.jarcommons-beanutils-1.8.3.jarcommons-dbutils-1.6.jarcommons-fileupload-1.2.2.jarcommons-io-2.0.1.jarcommons-lang3-3.1.jarfreemarker-2.3.19.jarjavassist-3.11.0.GA.jarmysql-connector-java-5.1.12-bin.jarognl-3.0.5.jarstruts2-core-2.3.4.1.jarxwork-core-2.3.4.1.jar
2.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">    <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></web-app>

3.stuts.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"><!-- START SNIPPET: xworkSample --><struts><!-- 更改主题 --><constant name="struts.ui.theme" value="simple"></constant><package name="emp" extends="struts-default"><global-results><result name="error">/error/error.jsp</result></global-results><action name="emp_*" class="com.bxh.action.EmployeeAction" method="{1}"><!--一、 防表单重复提交,二、配置防表单重复提交拦截器 --><interceptor-ref name="defaultStack"></interceptor-ref><interceptor-ref name="token"><!-- 三、指定拦截器那些方法需要 --><param name="includeMethods">save</param></interceptor-ref><result name="invalid.token" type="redirectAction">emp_list</result><result name="list">/WEB-INF/list.jsp</result><result name="update">/WEB-INF/update.jsp</result><result name="addsuccess" type="redirectAction">emp_list</result></action></package></struts><!-- END SNIPPET: xworkSample -->

3./src/c3p0-config.xml

<c3p0-config>  <default-config>     <property name="driverClass">com.mysql.jdbc.Driver</property>      <property name="jdbcUrl">jdbc:mysql:///hib_demo</property>      <property name="user">root</property>      <property name="password">123456</property>      <property name="initialPoolSize">5</property>      <property name="maxPoolSize">10</property>   </default-config>  <named-config name="oracleConfig">    <property name="driverClass">com.mysql.jdbc.Driver</property>      <property name="jdbcUrl">jdbc:mysql:///hib_demo</property>      <property name="user">root</property>      <property name="password">root</property>      <property name="initialPoolSize">5</property>      <property name="maxPoolSize">10</property>    </named-config></c3p0-config>

5.原码目录结构:


import java.util.List;import com.bxh.entity.Employee;public interface IEmployeeDao  {//查询员工List<Employee> getAll();//根据主键查询Employee findById(int id);//添加员工void save(Employee emp);//修改员工void update(Employee emp);}import java.sql.SQLException;import java.util.List;import org.apache.commons.dbutils.handlers.BeanHandler;import org.apache.commons.dbutils.handlers.BeanListHandler;import com.bxh.dao.IEmployeeDao;import com.bxh.entity.Employee;import com.bxh.utils.JdbcUtils;public class EmployeeDao implements IEmployeeDao{public List<Employee> getAll() {// TODO Auto-generated method stubString sql="select * from employee ";try {return JdbcUtils.getQuerrRunner().query(sql, new BeanListHandler<Employee>(Employee.class));} catch (SQLException e) {// TODO Auto-generated catch blockthrow new RuntimeException(e);}}public Employee findById(int id) {// TODO Auto-generated method stubString sql="select * from employee where id=?";try {return JdbcUtils.getQuerrRunner().query(sql, new BeanHandler<Employee>(Employee.class),id);} catch (SQLException e) {// TODO Auto-generated catch blockthrow new RuntimeException(e);}}public void save(Employee emp) {// TODO Auto-generated method stubString sql="insert into employee(empName,workDate) values(?,?)";try { JdbcUtils.getQuerrRunner().update(sql, emp.getEmpName(),emp.getWorkDate());} catch (SQLException e) {// TODO Auto-generated catch blockthrow new RuntimeException(e);}}public void update(Employee emp) {// TODO Auto-generated method stubString sql="update employee set empName=?,workDate=? where id=?";try { JdbcUtils.getQuerrRunner().update(sql, emp.getEmpName(),emp.getWorkDate(),emp.getId());} catch (SQLException e) {// TODO Auto-generated catch blockthrow new RuntimeException(e);}}}import java.util.List;import com.bxh.entity.Employee;public interface IEmployeeService  {//查询员工List<Employee> getAll();//根据主键查询Employee findById(int id);//添加员工void save(Employee emp);//修改员工void update(Employee emp);}import java.sql.SQLException;import java.util.List;import org.apache.commons.dbutils.handlers.BeanHandler;import org.apache.commons.dbutils.handlers.BeanListHandler;import com.bxh.dao.IEmployeeDao;import com.bxh.dao.impl.EmployeeDao;import com.bxh.entity.Employee;import com.bxh.service.IEmployeeService;import com.bxh.utils.JdbcUtils;public class EmployeeService implements IEmployeeService{private IEmployeeDao employeeDao=new EmployeeDao();public List<Employee> getAll() {try {return employeeDao.getAll();} catch (Exception e) {// TODO Auto-generated catch blockthrow new RuntimeException(e);}}public Employee findById(int id) {try {return employeeDao.findById(id);} catch (Exception e) {// TODO Auto-generated catch blockthrow new RuntimeException(e);}}public void save(Employee emp) {// TODO Auto-generated method stubtry {employeeDao.save(emp);} catch (Exception e) {// TODO Auto-generated catch blockthrow new RuntimeException(e);}}public void update(Employee emp) {try {employeeDao.update(emp);} catch (Exception e) {// TODO Auto-generated catch blockthrow new RuntimeException(e);}}}import java.util.List;import com.bxh.entity.Employee;import com.bxh.service.IEmployeeService;import com.bxh.service.impl.EmployeeService;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionSupport;import com.opensymphony.xwork2.ModelDriven;import com.opensymphony.xwork2.util.ValueStack;//员工管理Actionpublic class EmployeeAction extends ActionSupport implements ModelDriven<Employee>{/*封装数据*/private Employee employee=new Employee();public void setEmp(Employee employee) {this.employee = employee;}public Employee getEmp() {return employee;}/**调用service***/private IEmployeeService employeeService=new EmployeeService();public String save(){try {employeeService.save(employee);return list();//return "addsuccess";} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();return ERROR;}}//显示列表页面public String list(){try {List<Employee> listEmp=employeeService.getAll();ActionContext.getContext().getContextMap().put("listEmp", listEmp);return "list";} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();return ERROR;}}//实现驱动模型方法public Employee getModel() {// TODO Auto-generated method stubreturn employee;}//进入修改页面public String viewUpdate(){try {int id=employee.getId();Employee emp=employeeService.findById(id);//数据回显技术ValueStack vs=ActionContext.getContext().getValueStack();vs.pop();vs.push(emp);return "update";} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();return ERROR;}}//修改成功后进入显示页面public String update(){try {employeeService.update(employee);return list();} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();return ERROR;}}}import java.util.Date;public class Employee {private int id;private String empName;private Date workDate;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getEmpName() {return empName;}public void setEmpName(String empName) {this.empName = empName;}public Date getWorkDate() {return workDate;}public void setWorkDate(Date workDate) {this.workDate = workDate;}}import javax.sql.DataSource;import org.apache.commons.dbutils.QueryRunner;import com.mchange.v2.c3p0.ComboPooledDataSource;/** * 封装常用的操作 * @author bxh * */public class JdbcUtils {// 初始化连接池private static DataSource dataSource;static {dataSource = new ComboPooledDataSource();}public static DataSource getDataSource() {return dataSource;}/** * 创建DbUtils常用工具类对象 */public static QueryRunner getQuerrRunner() {return new QueryRunner(dataSource);}}


阅读全文
0 0
原创粉丝点击