Action层重构
2015-12-24 10:28
232 查看
通用接口层ICommonAction:
通用接口实现层CommonActionImpl:
具体的Action实现类:
对应的jsp页面:增加页面addDepartment.jsp;
显示页面:
更新页面updateDepartment.jsp;
注意:定义在接口中的常量请遵循命名规范,达到COC;
定义在实现类中的属性:bean,id等请遵循命名规范,以达到复用,同时对应的beans等请也遵循命名规范;
在对应的jsp页面中才能以对应的名称取值,从而达到重构和复用的效果;
如果还有对应的CRUD页面你只要继承对应的action即可;
![](http://b.bst.126.net/newpage/images/microblog.png?1)
转发至微博
![](http://b.bst.126.net/newpage/images/microblog.png?1)
转发至微博
package com.lrq.web.actions.base; public interface ICommonAction<T> { public static final String ACTION2ACTION="action2action"; public static final String ADDUI="addUI"; public static final String UPDATEUI="updateUI"; public static final String SHOWALL="showAll"; public String showAll(); public String addUI(); public String updateUI(); public String update(); public String delete(); public String add(); }
通用接口实现层CommonActionImpl:
package com.lrq.web.actions.base.impl; import java.lang.reflect.ParameterizedType; import java.util.Collection; import org.apache.struts2.ServletActionContext; import com.lrq.service.base.ICommonService; import com.lrq.web.actions.base.ICommonAction; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; public abstract class CommonActionImpl<T> extends ActionSupport implements ICommonAction<T> { private static final long serialVersionUID = -6840896550075095868L; private Class clazz; public CommonActionImpl() { ParameterizedType type = (ParameterizedType) this.getClass() .getGenericSuperclass(); clazz = (Class) type.getActualTypeArguments()[0]; try { bean = (T) clazz.newInstance(); } catch (InstantiationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } } protected ICommonService iCommonService; private Long id; protected T bean; public String showAll() { Collection beans = iCommonService.getAllEntities(); ActionContext.getContext().put("beans", beans); return SHOWALL; } public String addUI() { return ADDUI; } public String updateUI() { Object entity = iCommonService.getEntity(clazz, id); ServletActionContext.getRequest().setAttribute("bean", entity); return UPDATEUI; } public String update() { iCommonService.updateEntity(bean); return ACTION2ACTION; } public String delete() { iCommonService.deleteEntity(id); return ACTION2ACTION; } public String add() { iCommonService.saveEntity(bean); return ACTION2ACTION; } public abstract void setiCommonService(ICommonService iCommonService); public Long getId() { return id; } public void setId(Long id) { this.id = id; } public T getBean() { return bean; } public void setBean(T bean) { this.bean = bean; } }
具体的Action实现类:
package com.lrq.web.actions; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Controller; import com.lrq.domain.Department; import com.lrq.service.IDepartmentService; import com.lrq.service.base.ICommonService; import com.lrq.web.actions.base.impl.CommonActionImpl; @Controller @Scope(value = "prototype") public class DepartmentAction extends CommonActionImpl<Department> { private static final long serialVersionUID = 1822017919933992130L; //private IDepartmentService iDepartmentService;如果有特定方法就打开; @Autowired public void setiDepartmentService(IDepartmentService iDepartmentService) { //this.iDepartmentService = iDepartmentService; this.setiCommonService(iDepartmentService); } @Override public void setiCommonService(ICommonService iCommonService) { this.iCommonService=iCommonService; } }
对应的jsp页面:增加页面addDepartment.jsp;
<s:url action="departmentAction_add" var="url"></s:url> <form action="${url }" method="post"> 部门名称:<input type="text" name="bean.dname"><br/> 部门描述:<textarea rows="5" cols="20" name="bean.description"></textarea><br/> <input type="reset" value="重置"> <input type="submit" value="提交"> </form>
显示页面:
<body> <table> <tr> <td>部门名称</td> <td>部门描述</td> <td>相关操作</td> </tr> <c:forEach items="${beans }" var="bean"> <tr> <td>${bean.dname }</td> <td>${bean.description }</td> <td> <s:a action="departmentAction_updateUI">修改 <s:param name="id">${bean.did }</s:param> </s:a> <s:a action="departmentAction_delete"> 除 <s:param name="id">${bean.did }</s:param> </s:a> </td> </tr> </c:forEach> <tr> <td> <s:a action="departmentAction_addUI">添 部门</s:a> </td> </tr> </table> </body>
更新页面updateDepartment.jsp;
<s:url action="departmentAction_update" var="url"></s:url> <form action="${url }" method="post"> <input type="hidden" name="bean.did" value="${bean.did}"> 部门名称:<input type="text" name="bean.dname" value="${bean.dname }"><br/> 部门描述:<textarea rows="5" cols="20" name="bean.description">${bean.description }</textarea><br/> <input type="reset" value="重置"> <input type="submit" value="提交"> </form>
注意:定义在接口中的常量请遵循命名规范,达到COC;
定义在实现类中的属性:bean,id等请遵循命名规范,以达到复用,同时对应的beans等请也遵循命名规范;
在对应的jsp页面中才能以对应的名称取值,从而达到重构和复用的效果;
如果还有对应的CRUD页面你只要继承对应的action即可;
![](http://b.bst.126.net/newpage/images/microblog.png?1)
转发至微博
![](http://b.bst.126.net/newpage/images/microblog.png?1)
转发至微博
相关文章推荐
- service层重构的解决方案(三种解决…
- mybatis笔记
- Spring定时器的使用
- 数据结构课程设计——学生成绩管理系统
- struts总结完整整理版
- struts2总结:草稿
- Spring的总结
- struts2中默认拦截器栈中的拦截器…
- 动态代理模式--自定义数据源的终极…
- Git reset 回退项目某个节点版本
- kali postgress connection
- Ajax—初识
- 装饰设计模式---自定义数据源
- 读《结网》
- 关于tomcat服务器getParameter对ge…
- dom4j和xpath解析带名称空间的xml…
- 【转载】Linux下的javaee开发环境…
- oracle数据库之sql查询语句笔记
- oracle数据库笔记之数据库基本管理
- 前端规范(html5,js)