用泛化实现DAO大家就比较了解, 不知有没有人用泛化实现action呢, 代码如下:
java 代码
-
- import java.io.Serializable;
- import java.lang.reflect.ParameterizedType;
-
- import org.apache.log4j.Logger;
-
- import com.kelefa.common.dao.AbstractDAO;
- import com.opensymphony.xwork.Action;
- import com.opensymphony.xwork.ActionSupport;
-
- /*
-
-
-
-
-
-
-
- public abstract class AddBase<DAO extends AbstractDAO<VO, ID>, VO, ID extends Serializable>
- extends ActionSupport
- {
- private static final Logger log = Logger.getLogger( AddBase.class );
-
- private static final long serialVersionUID = 1L;
-
-
-
-
- protected DAO dao;
-
- public void setDao( DAO dao )
- {
- this.dao = dao;
- }
-
- protected VO vo = newValueObject();
-
- public VO getVo()
- {
- return vo;
- }
-
- public AddBase()
- {
- }
-
-
- @SuppressWarnings( "unchecked" )
- protected VO newValueObject()
- {
- Class entityClass = (Class) ((ParameterizedType) getClass()
- .getGenericSuperclass()).getActualTypeArguments()[1];
-
- if ( log.isDebugEnabled() )
- log.debug( "entityClass: " + entityClass.getCanonicalName() );
-
- VO newInstance = null;
- try
- {
- newInstance = (VO) entityClass.newInstance();
- }
- catch ( InstantiationException ex )
- {
- log.warn( ex.getMessage(), ex );
- }
- catch ( IllegalAccessException ex )
- {
- log.warn( ex.getMessage(), ex );
- }
-
- return newInstance;
- }
-
-
-
-
- @Override
- public String execute()
- throws Exception
- {
- dao.insert( vo );
-
- return Action.SUCCESS;
- }
- }
这样派生类不用写一行代码即可实现添加功能, 例如:
java 代码
-
- import com.kelefa.common.action.AddBase;
- import com.kingonsoft.house.dao.cms.ADTypeDAO;
- import com.kingonsoft.house.entity.cms.ADType;
-
-
- public class AddADType
- extends AddBase
- {
- private static final long serialVersionUID = 1L;
- }
不知大家对这种方法有何看法.