import static com.xxx.travel.common.utils.ClassUtils.getActualTypeArguments;
import static com.xxx.travel.common.utils.ClassUtils.getUUField;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import javax.servlet.ServletContext;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.apache.struts2.util.ServletContextAware;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.xxx.travel.common.base.bo.Page;
import com.xxx.travel.common.base.service.interfaces.BaseServiceInterface;
import com.xxx.travel.common.utils.ClassUtils;
import com.opensymphony.xwork2.ModelDriven;
/**
* 实现了管理的控制器
* @author IXR
*/
@Results({
@Result(name=ManagerAction.MANAGER_SUCCESS,location="manager/${actionMapping.method}.jsp"),
@Result(name=ManagerAction.MANAGER_LIST,location="${actionMapping.name}!list",type="redirectAction")
})
public class ManagerAction<S extends BaseServiceInterface<M>,M> extends BaseAction implements ModelDriven<M>,ServletContextAware{
private static final long serialVersionUID = -6850333477033985650L;
protected static final String MANAGER_SUCCESS = "manager_success";//默认Result
protected static final String MANAGER_LIST = "manager_list";
private String resultName = MANAGER_SUCCESS;
protected S service;
private M model;
private int currentPage = 0;// 当前页码
/** 列表 */
public String list(){
Page<M> page = new Page<M>();
page.setModel(model);
page.setCurrentPage(currentPage);
page.setResultRows(service.findExampleByPage(page));
request.setAttribute("page", page);
getModelFields();
return resultName;
}
/** 编辑 */
public String edit(){
service.load(model);
return MANAGER_SUCCESS;
}
/** 更新 */
public String update(){
service.update(model);
return MANAGER_LIST;
}
/** 删除 */
public String delete(){
service.delete(model);
return MANAGER_LIST;
}
/** 添加 */
public String add(){
return MANAGER_SUCCESS;
}
/** 保存 */
public String save(){
try {
Field field = getModelUUField();
field.setAccessible(true);
field.set(model, UUID.randomUUID().toString());
service.save(model);
} catch (Exception e) {
// TODO : ERROR
LOG.error(e.getMessage());
}
return MANAGER_LIST;
}
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public M getModel() {
if(model == null){
try {
model = getModelClass().newInstance();
} catch (Exception e) {
// TODO : ERROR
LOG.error(e.getMessage());
}
}
return model;
}
/**
* 获取对象模型的字段信息
* @return List<Field>
* @author IXR
*/
public List<Field> getModelFields(){
List<Field> getModelFields = new ArrayList<Field>();
for (Field field : getModelClass().getDeclaredFields()) {
getModelFields.add(field);
}
return getModelFields;
}
public String getResultName() {
return resultName;
}
public void setResultName(String resultName) {
this.resultName = resultName;
}
public Field getModelUUField() {
return getUUField(getModelClass());
}
/**
* 根据M类型来算出带命名空间
* @return 返回类型为 Class<M>
* @author IXR
*/
@SuppressWarnings("unchecked")
public Class<M> getModelClass() {
return (Class<M>)getActualTypeArguments(this.getClass())[1];
}
@SuppressWarnings("unchecked")
public void setServletContext(ServletContext servletContext) {
WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(servletContext);
Class<S> bizClass = (Class<S>) ClassUtils.getActualTypeArguments(this.getClass())[0];
String[] names = context.getBeanNamesForType(bizClass);
if(names.length > 0){
service = (S)context.getBean(names[0]);
}
}
}
BaseServiceInterface:如下
import java.util.List;
import com.xxx.travel.common.base.bo.Page;
public interface BaseServiceInterface<M> {
/**
* 根据条件分页查询数据
*
* @param page
* 分页信息
* @return List<M>
* @author IXR
*/
public List<M> findExampleByPage(Page<M> page);
/**
* 保存数据
*
* @param m
* 模型对象
* @author IXR
*/
public void save(M m);
/**
* 根据条件查询数量
*
* @return Long
* @author IXR
*/
public Long findExampleCount(M m);
/**
* 加载数据
*
* @param m
* 模型对象
* @author IXR
*/
public void load(M m);
/**
* 删除对象
*
* @param m
* 模型对象
* @author IXR
*/
public void delete(M m);
/**
* 更新数据
*
* @param m
* 模型对象
* @author IXR
*/
public void update(M m);
/**
* 根据条件查询数据
*
* @param M
* 模型信息
* @return List<M>
* @author IXR
*/
public List<M> findExample(M m);
}