編寫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>