我在我的项目中使用JSF 2和Spring 3,直到现在,我只使用请求和会话作用域。我想对一些JSF bean使用视图作用域(出于多种原因)。
问题是JSF ManagedBean必须实现Serializable接口及其所有属性。我有一些困难得到Spring bean序列化(例如:VilleService…),因为它继续要求更多的类是可序列化的,即使它是不可能的(hibernate类或mysql…)。
下面是一个例子:
public class VilleBean extends BaseBean implements Serializable {
private Ville ville;
private List<Ville> villes;
private VilleService villeService;
private PaysService paysService;
private Pays pays;
private List<Pays> payss;
private PojoConverter<Pays> paysConverter = null;
public VilleBean() {
ville = new Ville();
pays = new Pays();
}
public void setVilleService(VilleService villeService) {
this.villeService = villeService;
}
public void setPaysService(PaysService paysService) {
this.paysService = paysService;
}
// getters and setters for attributes : ville, pays, villes, payss
public void addVilleAction() {
ville.setPays(pays);
villeService.create(ville);
}
public void deleteVilleAction(Ville ville) {
villeService.delete(ville);
villes = villeService.readAll();
}
public void updateVilleAction() {
ville.setPays(pays);
villeService.update(ville);
}
public PojoConverter<Pays> getPaysConverter() {
this.paysConverter = new PojoConverter<Pays>(getPayss());
return this.paysConverter;
}
}
public abstract class BaseBean {
protected FacesContext context = FacesContext.getCurrentInstance();
protected MessageFactory msg;
protected static final Logger log = Logger.getLogger(BaseBean.class);
}
POJO: public class Ville implements Serializable {
private int id;
private String intitule;
private Pays pays;
// setters and getters
}
public class Pays implements Serializable {
private int id;
private String intitule;
// setters and getters
}
服务接口:
public interface VilleService extends ICrud<Ville> {
}
Service Impl:
public class VilleServiceBase implements VilleService {
private IDao<Ville> dao;
// getter and setter for dao
@Override
public List<Ville> readAll() throws Exception {
return dao.readAll();
}
@Override
public void create(Ville entity) throws Exception {
dao.create(entity);
}
//****
Util:
public interface ICrud<T> {
public java.util.List readAll() throws Exception;
public void create(T entity) throws Exception;
public void update(T entity) throws Exception;
//****
public interface IDao<T> {
public java.util.List<T> readAll();
public T create(T entity);
//****
public class DaoBase<T> extends org.springframework.orm.hibernate3.support.HibernateDaoSupport implements IDao<T> {
private final Class<T> type;
public DaoBase(Class<T> type) {
this.type = type;
}
@Override
public T load(int id) {
return (T) this.getHibernateTemplate().get(this.type, new Integer(id));
}
@Override
public T create(T entity) {
//****
this.getHibernateTemplate().save(entity);
return entity;
}
Spring beans: Spring .xml:
<!-- Ville Service Implementation -->
<bean id="villeServiceBase" class="commun.ref.crud.VilleServiceBase">
<property name="dao">
<ref bean="villeDao"/>
</property>
</bean>
<!-- Ville Service Proxy -->
<bean id="villeService" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target">
<ref local="villeServiceBase"/>
</property>
<property name="proxyInterfaces">
<value>commun.ref.crud.VilleService</value>
</property>
<property name="interceptorNames">
<list>
<value>manageableServiceTransactionInterceptor</value>
<value>hibernateInterceptor</value>
</list>
</property>
</bean>
<bean id="villeDao" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target">
<bean class="smile.util.DaoBase">
<constructor-arg>
<value>commun.ref.Ville</value>
</constructor-arg>
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
</property>
<property name="proxyInterfaces">
<value>smile.util.IDao</value>
</property>
<property name="interceptorNames">
<list>
<value>hibernateInterceptor</value>
</list>
</property>
</bean>
JSF配置:faces-config.xml:
<application>
<el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
</application>
<application>
<variable-resolver>
org.springframework.web.jsf.DelegatingVariableResolver
</variable-resolver>
</application>
******
<managed-bean>
<managed-bean-name>villeBean</managed-bean-name>
<managed-bean-class>commun.ref.ui.VilleBean</managed-bean-class>
<managed-bean-scope>view</managed-bean-scope>
<managed-property>
<property-name>villeService</property-name>
<value>#{villeService}</value>
</managed-property>
<managed-property>
<property-name>paysService</property-name>
<value>#{paysService}</value>
</managed-property>
</managed-bean>
你能告诉我,怎么做才能使它工作吗?
对于不想序列化或不能序列化的字段,请使用transient
修饰符。例如:
private transient VilleService villeService;
当你使用MyFaces时,将这个参数添加到web.xml:
<context-param>
<param-name>org.apache.myfaces.SERIALIZE_STATE_IN_SESSION</param-name>
<param-value>false</param-value>
</context-param>
据我所知,这个参数是Mojarra默认的false
。这将禁用会话中的状态序列化。
如果你不使用会话钝化,你可以使用transient
属性的非序列化字段
private transient Connection connection;