编写DepartmentAction类:
@Controller @Scope("prototype") public class DepartmentAction extends ActionSupport implements ModelDriven<Department> { @Resource private DepartmentService departmentService; private Long parentId; private int pageNum = 1;// 当前页 private int pageSize = 2;// 每页显示多少条数据 private Department department = new Department(); @Override public Department getModel() { return department; } /** * 删除 * * @return * @throws Exception */ public String delete() throws Exception { departmentService.delete(department.getId()); return "toList"; } /** * 增加页面 * * @return * @throws Exception */ public String addUI() throws Exception { // 显示所有部门(准备数据,department列表) List<Department> departmentList = departmentService.findAll(); // 放到Map中 ActionContext.getContext().put("departmentList", departmentList); return "saveDepartmentUI"; } /** * 添加 * * @return * @throws Exception */ public String add() throws Exception { // 根据ID查询部门信息 Department parent = departmentService.findById(parentId); // 设置上级部门信息 department.setParent(parent); // 添加部门信息 departmentService.add(department); return "toList"; } /** * 修改页面 * * @return * @throws Exception */ public String editorUI() throws Exception { // 显示所有部门(准备数据,department列表) List<Department> departmentList = departmentService.findAll(); // 放到Map中 ActionContext.getContext().put("departmentList", departmentList); // 准备回显数据 Department departmentResult = departmentService.findById(department.getId()); ActionContext.getContext().getValueStack().push(departmentResult); // 给所属部门设值回显(相对应的属性有值) if (departmentResult.getParent() != null) { parentId = departmentResult.getParent().getId(); } return "saveDepartmentUI"; } /** * 修改 * * @return * @throws Exception */ public String editor() throws Exception { // 取出原对象 Department departmentResult = departmentService.findById(department.getId()); // 更新到原对象信息 departmentResult.setName(department.getName()); departmentResult.setDescription(department.getDescription()); departmentResult.setParent(departmentService.findById(parentId));// 设置上级部门 // 修改 departmentService.update(departmentResult); return "toList"; } /** * 分页 * * @return */ public String showBypage() throws Exception { //List<Department> departmentList =departmentService.findAll(); Department department =null; PageBean pageBean = departmentService.getPageBeanByDepartment(pageNum,pageSize,department); ActionContext.getContext().getValueStack().push(pageBean); return "showBypage"; } public Long getParentId() { return parentId; } public void setParentId(Long parentId) { this.parentId = parentId; } public int getPageNum() { return pageNum; } public void setPageNum(int pageNum) { this.pageNum = pageNum; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } }
departmentService类
@Service @Transactional @SuppressWarnings("unchecked") public class DepartmentServiceImpl implements DepartmentService{ @Resource private DepartmentDao departmentDao ; @Resource private SessionFactory sessionFactory ; @Override public List<Department> findAll() { // TODO Auto-generated method stub return departmentDao.findAll(); } @Override public void delete(Long id) { departmentDao.delete(id); } @Override public void add(Department department) { departmentDao.save(department); } @Override public Department findById(Long id) { return departmentDao.getById(id); } @Override public void update(Department department) { departmentDao.update(department); } @Override public List<Department> findTopList() { return sessionFactory.getCurrentSession().createQuery("" + "FROM Department d WHERE d.parent IS NULL" + "").list(); } @Override public List<Department> findChildList(Long parentId) { return sessionFactory.getCurrentSession().createQuery("" + "FROM Department d WHERE d.parent.id =?") .setParameter(0, parentId) .list(); } @Override public PageBean getPageBeanByDepartment(int pageNum, int pageSize, Department departmentResult) { List list = sessionFactory.getCurrentSession().createQuery("" + "FROM Department d order BY d.name ASC") .setFirstResult((pageNum-1)*pageSize) .setMaxResults(pageSize) .list(); Long count =(Long)sessionFactory.getCurrentSession().createQuery("" + "SELECT COUNT(*) FROM Department d ").uniqueResult(); return new PageBean(pageNum, pageSize, count.intValue(), list); } }
struts2.xml
<!-- 部门管理 --> <action name="department_*" class="departmentAction" method="{1}"> <result name="list"> /WEB-INF/jsp/departmentAction/list.jsp</result> <result name="showBypage"> /WEB-INF/jsp/departmentAction/list.jsp</result> <result name="saveDepartmentUI"> /WEB-INF/jsp/departmentAction/saveDepartmentUI.jsp</result> <result name="toList" type="redirectAction">department_showBypage?parentId=${parentId}</result> </action>