我的JSF 2.2项目中有一个sessionScoped ManagedBean
,我想调用另一个ManagedBean
作为@ManagedProperty
这是我的第一个sessionScoped
bean:
public class MainWorkerMB implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
@ManagedProperty("#{fragmentHandlerMB}")
private FragmentHandlerMB fragmentHandlerMB;
public FragmentHandlerMB getFragmentHandlerMB() {
return fragmentHandlerMB;
}
public void setFragmentHandlerMB(FragmentHandlerMB fragmentHandlerMB) {
this.fragmentHandlerMB = fragmentHandlerMB;
}
public void search() {
fragmentHandlerMB.changeFrag("search_result.xhtml" , "Result Page" , -1);
}
这是我的另一个bean,它也是sessionScoped
:
@ManagedBean(eager=true)
@SessionScoped
public class FragmentHandlerMB implements Serializable {
@PostConstruct
public void init() {
log.info("constructed..");
// other code parts //
}
public FragmentHandlerMB() {
super();
}
public void changeFrag(String fragToOpen, String fragToOpenName, int pageIndex) {
//...
}
}
之后当我尝试调用我的secound bean方法时,我得到了NullPointerException
:
Caused By: java.lang.NullPointerException
at com.kebodev.managed.MainWorkerMB.search(MainWorkerMB.java:29)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
Truncated. see log file for complete stacktrace
所以看起来我的豆子不存在。
你能帮帮我吗? 谢谢!答案 0 :(得分:1)
我通过移除<managed-bean>
中的整个faces-config.xml
配置并将@ManagedBean @SessionScoped
anotation添加到ManagedBean.