JSF ManagedBean-注入的属性在STATE_SAVING_METHOD = client上无法正常工作 [英] JSF ManagedBean - injected properties not working correctly on STATE_SAVING_METHOD=client
问题描述
两天后我陷入困境,无法摆脱困境.
I am into a problem from two days and I can not get out from this.
反序列化后,我遇到的问题是使用MangedBean属性(我想).
该属性(purchaseManager)是通过Spring设置的,并使用DAO扩展了MyBatis作为数据映射器以与DB交互.
实际上,在第一次访问该页面时,init()方法中的purchaseManager.getAll()可以正常工作.
当我尝试通过按钮调用refreshList()作为操作时,在DAO中的getSqlSession()上出现了NullPointerException.
The problem I am having is using a MangedBean property after the deserialization (I guess).
The property (purchaseManager) is set up with Spring, and use a DAO which extends MyBatis as data mapper to interact with the DB.
In fact, on the first access to the page, purchaseManager.getAll() inside init() method works fine.
When i try to call refreshList() as an action from a button, I have a NullPointerException on the getSqlSession() inside the DAO.
仅留下相关代码的情况如下:
Letting only the relevant code the situation is as follow:
@ManagedBean(name = "purchaseController")
@ViewScoped
public class PurchaseController implements Serializable{
@ManagedProperty(value = "#{purchaseManager}")
private PurchaseManager purchaseManager;
@PostConstruct
public void init(){
purchaseManager.getAll();
}
public void refreshList(){
purchaseManager.getAll();
}
}
public class PurchaseManagerImpl implements PurchaseManager, Serializable {
PurchaseDAO purchaseDAO;
public void getAll() {
purchaseDAO.getAll()
}
}
public class PurchaseDAOImpl extends SqlSessionDaoSupport implements PurchaseDAO, Serializable {
public void getAll() {
SqlSession session = getSqlSession(); // when the call comes from refreshList(), session is null
session.selectList("PAYMENT.getAll", null);
}
}
in web.xml
<context-param>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>server</param-value>
</context-param>
如果我将STATE_SAVING_METHOD更改为服务器,则应用程序可以正常运行,但不是我想要的.如果将ManageBean用作RequestScope也是一样,但这也会对我的要求造成不利影响.
If I change the STATE_SAVING_METHOD to server the application works fine but is not what I want. Same thing if I make the ManageBean as RequestScope but this too will penalize my requirements.
在此先感谢任何人的帮助! Ermal
Thank you in advance to anyone for any kind of help! Ermal
推荐答案
解决了将<aop:scoped-proxy proxy-target-class="false" />
添加到通过Spring声明的服务/管理器定义中的错误.这样就可以注入完全可序列化的代理实例.
Solved the error adding <aop:scoped-proxy proxy-target-class="false" />
to the definition of the service/manager declared through Spring. This makes possible the injection of a fully serializable proxy instance.
<bean id="purchaseManager" class="al.ozone.bl.manager.impl.PurchaseManagerImpl">
<property name="purchaseDAO" ref="purchaseDAO" />
<aop:scoped-proxy proxy-target-class="false" />
</bean>
proxy-target-class="false"
用于告诉PurchaseManagerImpl
已经实现了一个接口.如果设置为true
或省略,则必须使用CGLIB2库.
proxy-target-class="false"
is for telling that PurchaseManagerImpl
implements already an interface. If setted to true
or omitted, CGLIB2 library must be used.
通过这种方式,JSF可以使用Spring + MyBatis正确地从数据库中获取数据.
In this way JSF is correctly taking data from DB using Spring+MyBatis.
关于这一点(对我而言)(更理论上)的迷雾是:
The mistery (for me) on this point (more theorical) is :
- 在幕后是否正确处理了MyBatis对象(
PurchaseDAOImpl
)和dataSource
? - 是否在每个HTTP请求上重新创建或还原了它们?
- Is MyBatis object (
PurchaseDAOImpl
) and thedataSource
, correctly handled behind the scenes? - Are they recreated or restored on each HTTP request?
请记住,我有STATE_SAVING_METHOD=client
和BackingBean作为ViewScope
.
我的目标是使服务器更轻巧,因为我希望有大量的用户交互.
Remember that I have STATE_SAVING_METHOD=client
and BackingBean as ViewScope
.
My Goal is to have the server lighter possible because I expect an hight number of user interactions.
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="poolPreparedStatements" value="true" />
<property name="defaultAutoCommit" value="false" />
</bean>
非常感谢任何人对此事有所启发!
Thank you very much to anyone for some light on this matter!
咨询链接:
Spring会话范围的bean (控制器)和对服务的引用(按照序列化方式
http://www.infoq.com/presentations/Whats -Spring-in-Spring-3.0
这篇关于JSF ManagedBean-注入的属性在STATE_SAVING_METHOD = client上无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!