自己做的练手小程序,欢迎大家 批评指导 并 提出建议..
开发环境 - eclipse(myeclipse6.5) + tomcat 6 + mysql
下边只贴出主要代码.
struts.xml
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <include file="struts-default.xml" /> <package name="employee" extends="struts-default" namespace="/"> <interceptors> <!-- 定义 核对登陆 的拦截器 --> <interceptor name="checkLogin" class="org.lzpeng.interceptor.CheckLogin" /> <!-- 定义一个拦截器堆栈 --> <interceptor-stack name="default"> <interceptor-ref name="checkLogin" /> <interceptor-ref name="defaultStack" /> </interceptor-stack> </interceptors> <!-- 设置全局默认拦截器 --> <default-interceptor-ref name="default" /> <!-- 定义全局 result --> <global-results> <result name="login">viewPage/login.jsp</result> </global-results> <!-- 获得所有雇员 --> <action name="list" method="getAllEmployees" class="org.lzpeng.actions.EmployeesAction"> <result name="success">/viewPage/employees.jsp</result> </action> <!-- 增加或更新雇员的表单需要加载所有的部门 --> <action name="storeOrUpdate" method="setForinsertOrUpdate" class="org.lzpeng.actions.EmployeesAction"> <result name="success">viewPage/employeeForm.jsp</result> </action> <!-- 增加或更新雇员信息 --> <action name="insertOrUpdate" method="insertOrUpdate" class="org.lzpeng.actions.EmployeesAction"> <result name="success" type="redirect-action"> list!getAllEmployees </result> <result name="input">viewPage/employeeForm.jsp</result> </action> <!-- 删除雇员 --> <action name="delete" method="delete" class="org.lzpeng.actions.EmployeesAction"> <result name="success" type="redirect-action"> list!getAllEmployees </result> </action> </package> <package name="manager" extends="struts-default"> <!-- 转发到登陆界面 --> <action name="login"> <result>/viewPage/login.jsp</result> </action> <!-- 验证用户--> <action name="check" method="validateUser" class="org.lzpeng.actions.ManagerAction"> <!-- 重复提交表单处理 --> <result name="invalid.token">/errorPage.jsp</result> <!-- 密码错误 --> <result name="input">/viewPage/login.jsp</result> <!-- Action名后 +!xxx xxx为方法名 --> <result name="success" type="redirect-action"> /list!getAllEmployees.action </result> <interceptor-ref name="token" /> <interceptor-ref name="defaultStack" /> </action> </package> </struts>
BaseHibernateDAO.java
package org.lzpeng.dao.hibernate; import java.util.List; import org.hibernate.Session; import org.hibernate.Transaction; import org.hibernate.criterion.Example; /** * HIBERNATE 抽象基础类 * <p> * 包含了简单的查询和增删改的方法 * * @author LZPeng * @version 1.0 * @date 2008/7/20 * @since JDK5 */ public abstract class BaseHibernateDAO { @SuppressWarnings("unused") private Session session = null; /*************************************************************************** * select * * @param clz * 类实体 * @param id * 必须实现 Serializable 接口 * @return ret Object */ protected Object get(Class<?> clz, java.io.Serializable id) { Object ret = null; Session session = getSession(); try { ret = session.get(clz, id); } catch (Exception e) { e.printStackTrace(); } finally { this.closeSession(); } return ret; } /*************************************************************************** * insert * * @param item * 需要增加的实体 */ protected void add(Object item) { Transaction tx = null; Session session = getSession(); try { tx = session.beginTransaction(); session.save(item); tx.commit(); } catch (Exception e) { if (null != tx) { tx.rollback(); } e.printStackTrace(); } finally { closeSession(); } } /*************************************************************************** * update * * @param item * 需要更新的实体对象 */ protected void update(Object item) { Transaction tx = null; Session session = getSession(); try { tx = session.beginTransaction(); session.update(item); tx.commit(); } catch (Exception e) { if (null != tx) { tx.rollback(); } e.printStackTrace(); } finally { closeSession(); } } /*************************************************************************** * delete * * @param clz * 类的实例 * @param id * 实现 Serializable 的类 */ protected void delete(Class<?> clz, java.io.Serializable id) { Transaction tx = null; Session session = getSession(); try { tx = session.beginTransaction(); session.delete(get(clz, id)); tx.commit(); } catch (Exception e) { if (null != tx) { tx.rollback(); } e.printStackTrace(); } finally { closeSession(); } } /*************************************************************************** * * @param clazz * 类实例 * @param condition * 对应实体类的类型对象 * @return results Object 类型实体的 List 集合 * * @throws RuntimeException * if an error occured */ @SuppressWarnings("unchecked") protected List search(Class<?> clazz, Object condition) { try { List results = getSession().createCriteria(clazz).add( Example.create(condition)).list(); return results; } catch (RuntimeException re) { throw re; } finally { closeSession(); } } /*************************************************************************** * 获取 Hibernate session * * @return Hibernate session */ protected Session getSession() { return HibernateUtil.getSession(); } /** * 设置 HIBERNATE session * * @param session */ public void setSession(Session session) { this.session = session; } /** * 关闭 session */ protected void closeSession() { this.session = null; } }
EmployeesAction.java
package org.lzpeng.actions; import org.lzpeng.vo.Department; import org.lzpeng.vo.Employee; import org.lzpeng.service.EmployeeService; import org.lzpeng.service.DepartmentService; import org.lzpeng.service.impl.DepartmentDaoService; import org.lzpeng.service.impl.EmployeeDaoService; import java.util.List; import java.util.Map; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; /** * <p> * Employee 的 Action 控制类 * </p> * * @author LZPeng * @version 1.0 * @date 2008/7/22 * @see EmployeeService * @since JDK5 */ public class EmployeesAction extends ActionSupport { private static final long serialVersionUID = -7351331038736790079L; private EmployeeService empService = new EmployeeDaoService();// 雇员服务类对象 private DepartmentService deptService = new DepartmentDaoService();// 部门服务类对象 private Employee employee = null; // 雇员 private List<Employee> employees = null; // 雇员集合 private List<Department> departments = null;// 部门集合 /*************************************************************************** * 获得所有雇员的信息 * * @return SUCCESS * */ public String getAllEmployees() { employees = empService.getAllEmployees(); return SUCCESS; } /*************************************************************************** * 增加或者更新雇员时需要将所有部门查出来显示给用户 * <p> * 如果不在 action 将 departments 放入session 中 迭代器可以拿到值 , 但 <s:select / > 标签不可以.. * * @return SUCCESS */ @SuppressWarnings("unchecked") public String setForinsertOrUpdate() { departments = deptService.getAllDepartments();// 获得所有部门 Map<String, List<Department>> session = ActionContext.getContext() .getSession(); session.put("departments", departments); // 将部门信息放入 session if (employee != null && employee.getEmployeeid() != null) { /* 如果是更新,得到此雇员信息 */ employee = empService.getEmployeeByID(employee.getEmployeeid()); } return SUCCESS; } /*************************************************************************** * 执行插入或者更新 * * @return */ public String insertOrUpdate() { if (employee.getEmployeeid() == null) { empService.insertEmployee(employee); } else { empService.updateEmployee(employee); } return SUCCESS; } /*************************************************************************** * 通过ID删除雇员 * */ public String delete() { empService.deleteEmployeeByID(employee.getEmployeeid()); return SUCCESS; } /*************************************************************************** * action 的默认执行方法 * * @return SUCCESS */ @Override public String execute() { return SUCCESS; } // ------------------------------------------------------------------------- // Setter and Getter methods // ------------------------------------------------------------------------- public List<Employee> getEmployees() { return employees; } public void setEmployees(List<Employee> employees) { this.employees = employees; } public Employee getEmployee() { return employee; } public void setEmployee(Employee employee) { this.employee = employee; } public List<Department> getDepartments() { return departments; } public void setDepartments(List<Department> departments) { this.departments = departments; } }
EmployeeDaoService.java
package org.lzpeng.service.impl; import java.util.List; import org.lzpeng.service.EmployeeService; import org.lzpeng.vo.Employee; import org.lzpeng.dao.EmployeeDao; import org.lzpeng.dao.hibernate.EmployeeHibernateDao; /** * 对雇员的持久化操作 * <p> * 继承自 EmployeeService * * @author LZPeng * @version 1.0 * @date 2008/7/20 * @see EmployeeService * @since JDK5 */ public class EmployeeDaoService implements EmployeeService { private EmployeeDao dao; public EmployeeDaoService() { this.dao = new EmployeeHibernateDao();// 雇员的数据库操作类对象 } /*************************************************************************** * 获得所有雇员信息 * * @return Employee的集合List */ public List<Employee> getAllEmployees() { return dao.getAllEmployees(); } public void updateEmployee(Employee emp) { dao.update(emp); } public void deleteEmployeeByID(Integer id) { dao.delete(id); } /*************************************************************************** * 根据雇员ID获得雇员POJO对象 * * @param id * 雇员编号 * @return 雇员POJO对象 */ public Employee getEmployeeByID(Integer id) { return dao.getEmployee(id); } /*************************************************************************** * 增加一个雇员 * * @param emp * Employee对象 */ public void insertEmployee(Employee emp) { dao.insert(emp); } }
未解决的(主要)问题:
问题一 :
第一次访问到login.jsp, 此界面包含(include) head.jsp 但head.jsp不能正常显示.报异常.刷新界面(F5)后,正常显示,异常消失.
问题二 : 关于非空验证提示信息重复
在登录界面,连续2次在不填写数据情况下点 登录 按钮,提示重复..