用泛型写Struts2的BaseAction出错
2008-03-22 22:48
381 查看
想写一个用于Struts2的Action基础类BaseAction,里面有些常用的CRUD方法,可是,不知怎么回事,从后台到前台的转换可以顺利进行,但是要save前台的entity到数据库的时候,泛型总是把前台Struts2传来的对象作为Object处理,从而报错。
我得BaseAction如下:
/** *//**
* @(#)cn.pingyutech.base.action.BaseAction.java
* @author <a href="mailto:newweapon111@gmail.com">Liu Weiping</p>
* @version 1.0
* @date 2008-3-22
*
* Copyright 2008 PingYuTech, Inc. All rights reserved.
*
*/
package cn.pingyutech.base.action;
import java.io.Serializable;
import java.util.Collection;
import org.apache.log4j.Logger;
import cn.pingyutech.base.service.IBaseService;
import com.opensymphony.xwork2.ActionSupport;
/** *//**
* Base action using generic
*/
public abstract class BaseAction<T, FK extends Serializable> extends ActionSupport...{
private static final Logger log = Logger.getLogger(BaseAction.class);
private static final long serialVersionUID = 1L;
private Collection<T> entities;
private T current;
private FK id;
public abstract IBaseService<T, FK> getService();
/** *//**
* 列出所有数据
*
* @return SUCCESS: OK; INPUT: Failure.
*/
public String list() ...{
try ...{
entities = this.getService().findByAll();
return execute();
} catch (Exception e) ...{
log.debug(e);
return INPUT;
}
}
/** *//**
* 根据id载入实体。
*
* @return
*/
public String load() ...{
this.current = (T) this.getService().load(id);
return SUCCESS;
}
/** *//**
* 保存当前实体,使用了SaveOrUpdate方法。
*
* @return
*/
public String save() ...{
this.getService().save(current);
return SUCCESS;
}
/** *//**
* 删除当前实体。
*
* @return
*/
public String delete() ...{
this.getService().delete(current);
return SUCCESS;
}
/** *//**
* @return the entities
*/
public Collection<T> getEntities() ...{
return entities;
}
/** *//**
* @param entities the entities to set
*/
public void setEntities(Collection<T> entities) ...{
this.entities = entities;
}
/** *//**
* @return the current
*/
public T getCurrent() ...{
return current;
}
/** *//**
* @param current the current to set
*/
public void setCurrent(T current) ...{
this.current = current;
}
/** *//**
* @return the id
*/
public FK getId() ...{
return id;
}
/** *//**
* @param id the id to set
*/
public void setId(FK id) ...{
this.id = id;
}
}
而继承BaseAction的UserAction如下:
package test;
import org.apache.log4j.Logger;
import cn.pingyutech.base.action.BaseAction;
import cn.pingyutech.base.service.IBaseService;
public class CustomerAction extends BaseAction<Customer, Integer> ...{
private static final Logger log = Logger.getLogger(CustomerAction.class);
private static final long serialVersionUID = 1L;
private CustomerDao custDao;
private int cid;
public CustomerAction() ...{
super();
}
/** *//**
* Used by DWR to delete a entity.
*
* @param id
*/
public String remove(int id)...{
try ...{
this.getService().delete(new Customer(id));
return SUCCESS;
} catch (Exception e) ...{
log.debug(e);
return INPUT;
}
}
// ======= getters/setters ========= //
public int getCid() ...{
return cid;
}
public void setCid(int cid) ...{
this.cid = cid;
this.setId(cid);
}
public CustomerDao getCustDao() ...{
return custDao;
}
public void setCustDao(CustomerDao dao) ...{
this.custDao = dao;
}
/** *//**
* <p>这个方法是继承父类BaseCRUDDao的,是为了自定义Dao而设立的。</p>
*/
@Override
public IBaseService<Customer, Integer> getService() ...{
return custDao;
}
}
暂且做个记录,待有时间了再好好研究
我得BaseAction如下:
/** *//**
* @(#)cn.pingyutech.base.action.BaseAction.java
* @author <a href="mailto:newweapon111@gmail.com">Liu Weiping</p>
* @version 1.0
* @date 2008-3-22
*
* Copyright 2008 PingYuTech, Inc. All rights reserved.
*
*/
package cn.pingyutech.base.action;
import java.io.Serializable;
import java.util.Collection;
import org.apache.log4j.Logger;
import cn.pingyutech.base.service.IBaseService;
import com.opensymphony.xwork2.ActionSupport;
/** *//**
* Base action using generic
*/
public abstract class BaseAction<T, FK extends Serializable> extends ActionSupport...{
private static final Logger log = Logger.getLogger(BaseAction.class);
private static final long serialVersionUID = 1L;
private Collection<T> entities;
private T current;
private FK id;
public abstract IBaseService<T, FK> getService();
/** *//**
* 列出所有数据
*
* @return SUCCESS: OK; INPUT: Failure.
*/
public String list() ...{
try ...{
entities = this.getService().findByAll();
return execute();
} catch (Exception e) ...{
log.debug(e);
return INPUT;
}
}
/** *//**
* 根据id载入实体。
*
* @return
*/
public String load() ...{
this.current = (T) this.getService().load(id);
return SUCCESS;
}
/** *//**
* 保存当前实体,使用了SaveOrUpdate方法。
*
* @return
*/
public String save() ...{
this.getService().save(current);
return SUCCESS;
}
/** *//**
* 删除当前实体。
*
* @return
*/
public String delete() ...{
this.getService().delete(current);
return SUCCESS;
}
/** *//**
* @return the entities
*/
public Collection<T> getEntities() ...{
return entities;
}
/** *//**
* @param entities the entities to set
*/
public void setEntities(Collection<T> entities) ...{
this.entities = entities;
}
/** *//**
* @return the current
*/
public T getCurrent() ...{
return current;
}
/** *//**
* @param current the current to set
*/
public void setCurrent(T current) ...{
this.current = current;
}
/** *//**
* @return the id
*/
public FK getId() ...{
return id;
}
/** *//**
* @param id the id to set
*/
public void setId(FK id) ...{
this.id = id;
}
}
而继承BaseAction的UserAction如下:
package test;
import org.apache.log4j.Logger;
import cn.pingyutech.base.action.BaseAction;
import cn.pingyutech.base.service.IBaseService;
public class CustomerAction extends BaseAction<Customer, Integer> ...{
private static final Logger log = Logger.getLogger(CustomerAction.class);
private static final long serialVersionUID = 1L;
private CustomerDao custDao;
private int cid;
public CustomerAction() ...{
super();
}
/** *//**
* Used by DWR to delete a entity.
*
* @param id
*/
public String remove(int id)...{
try ...{
this.getService().delete(new Customer(id));
return SUCCESS;
} catch (Exception e) ...{
log.debug(e);
return INPUT;
}
}
// ======= getters/setters ========= //
public int getCid() ...{
return cid;
}
public void setCid(int cid) ...{
this.cid = cid;
this.setId(cid);
}
public CustomerDao getCustDao() ...{
return custDao;
}
public void setCustDao(CustomerDao dao) ...{
this.custDao = dao;
}
/** *//**
* <p>这个方法是继承父类BaseCRUDDao的,是为了自定义Dao而设立的。</p>
*/
@Override
public IBaseService<Customer, Integer> getService() ...{
return custDao;
}
}
暂且做个记录,待有时间了再好好研究
相关文章推荐
- Struts2泛型通用crud的action,让一切变得简单起来
- struts2用注解开发出错一 There is no Action mapped for namespace [/] and action name [zhujie] associated with
- struts2上传图片linux报错:Exception Name: No result defined for action com.enation.app.base.core.action.Adv
- java SSH三大框架整合,实现了泛型baseAction,泛型baseService,泛型baseDao
- BaseAction抽取之泛型的实例化
- Struts2配置默认action(首页)、配置404防止请求出错问题解决
- Struts2 Action中调用get***命名方法,并且返回json,执行出错
- struts2中BaseAction的模板
- struts2 一个CRUD的BaseAction
- Struts2中Action中动态调用,为什么出错了???
- struts2的BaseAction<T>继承ActionSupport实现ModelDriven<T>
- struts2中BaseAction的模板
- java SSH三大框架整合,实现了泛型baseAction,泛型baseService,泛型baseDao
- 在struts2 action中使用spring aop出错
- struts2用注解开发出错二 There is no Action mapped for namespace [/] and action name [zhujie] associated with
- struts2 一个CRUD的BaseAction
- Struts2 表的提交报错 :No result defined for action net.zdsoft.eis.base.data.action.BasicSemesterAdminActio
- struts2两个input方法的action,出错后总是返回到Input页面的解决方法
- 自己封装用的Struts2BaseAction
- struts2 继承BaseAction获取request、response、session