您的位置:首页 > 其它

Action层重构

2015-12-24 10:28 232 查看
通用接口层ICommonAction:
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即可;



转发至微博
 



转发至微博
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: