1.导入struts包
c3p0-0.9.1.2.jar2.web.xml配置
commons-beanutils-1.8.3.jar
commons-dbutils-1.6.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
mysql-connector-java-5.1.12-bin.jar
ognl-3.0.5.jar
struts2-core-2.3.4.1.jar
xwork-core-2.3.4.1.jar
<?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 stub
String sql="select * from employee ";
try {
return JdbcUtils.getQuerrRunner().query(sql, new BeanListHandler<Employee>(Employee.class));
} catch (SQLException e) {
// TODO Auto-generated catch block
throw new RuntimeException(e);
}
}
public Employee findById(int id) {
// TODO Auto-generated method stub
String 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 block
throw new RuntimeException(e);
}
}
public void save(Employee emp) {
// TODO Auto-generated method stub
String sql="insert into employee(empName,workDate) values(?,?)";
try {
JdbcUtils.getQuerrRunner().update(sql, emp.getEmpName(),emp.getWorkDate());
} catch (SQLException e) {
// TODO Auto-generated catch block
throw new RuntimeException(e);
}
}
public void update(Employee emp) {
// TODO Auto-generated method stub
String 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 block
throw 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 block
throw new RuntimeException(e);
}
}
public Employee findById(int id) {
try {
return employeeDao.findById(id);
} catch (Exception e) {
// TODO Auto-generated catch block
throw new RuntimeException(e);
}
}
public void save(Employee emp) {
// TODO Auto-generated method stub
try {
employeeDao.save(emp);
} catch (Exception e) {
// TODO Auto-generated catch block
throw new RuntimeException(e);
}
}
public void update(Employee emp) {
try {
employeeDao.update(emp);
} catch (Exception e) {
// TODO Auto-generated catch block
throw 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;
//员工管理Action
public 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 block
e.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 block
e.printStackTrace();
return ERROR;
}
}
//实现驱动模型方法
public Employee getModel() {
// TODO Auto-generated method stub
return 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 block
e.printStackTrace();
return ERROR;
}
}
//修改成功后进入显示页面
public String update(){
try {
employeeService.update(employee);
return list();
} catch (Exception e) {
// TODO Auto-generated catch block
e.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);
}
}