高分求答:JSF中不能取得bean
web.xml中有如下配置
<context-param> <param-name>javax.faces.CONFIG_FILES</param-name> <param-value> ...... /WEB-INF/faces-managed-beans-user.xml, ... ... </param-value></context-param>
<managed-bean> <managed-bean-name>alertConfigBean</managed-bean-name> <managed-bean-class> com.test.bean.rule.AlertConfigBean </managed-bean-class> <managed-bean-scope>application</managed-bean-scope></managed-bean>...... <managed-bean> <managed-bean-name>ruleBean</managed-bean-name> <managed-bean-class> com.infosec.secsrv.scms20.bean.rule.RuleBean </managed-bean-class> <managed-bean-scope>session</managed-bean-scope> </managed-bean>
</managed-bean>
<managed-bean>
<managed-bean-name>controllerBean</managed-bean-name>
<managed-bean-class>myproject.ControllerBean</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
<managed-property>
<property-name>modelBean</property-name>
<value>#{modelBean}</value>
</managed-property>
</managed-bean>
</faces-config>
[解决办法]
看到这个问题:首先要说,jsf一般只作为显示层的实现。jsf管理的backing bean是作为控制器来连接UI和业务逻辑bean。你如果要得到backing bean,其实大可不必。backing bean只做为VO(传递数据)和调用后台业务Bean;如果要获得业务逻辑bean的话,其实你可以结合spring框架来做。有spring管理业务逻辑bean和backing bean,以上是个人建议。
具体怎么获得jsf管理的bean呢?
在网上查了些资料,内容如下:
如果要在其它类中取得Bean对象,则可以先取得javax.faces.context.FacesContext,它代表了JSF目前的执行环境对象,接着尝试取得javax.faces.el.ValueBinding对象,从中取得指定的Bean对象,例如:
FacesContext context = FacesContext.getCurrentInstance();
ValueBinding binding =
context.getApplication().createValueBinding("#{user}");
UserBean user = (UserBean) binding.getValue(context);
如果只是要尝试取得Bean的某个属性,则可以如下:
FacesContext context = FacesContext.getCurrentInstance();
ValueBinding binding =
context.getApplication().createValueBinding(
"#{user.name}");
String name = (String) binding.getValue(context);
本人没有验证,请lz自己验证下