EDIT: This is an ADF application which uses JSF 2.0.
I have an application-scoped managed bean which I am referencing in the managed property of a request-scoped bean. I am getting a NullPointerException when trying to access the app-scoped bean within the PostConstruct method of the request-scoped bean. I am not sure whether I am not understanding some fundamentals about when an app-scoped bean is available to a request-scoped bean, or whether I just have a mistake in my implementation.
App-scoped bean:
#ManagedBean(eager=true)
#ApplicationScoped
public class SecurityApplication {
public String test() {
return "test result";
}
#PostConstruct
public void init() {
System.out.println("In SecurityApplication.init");
}
}
EDIT: This is configured as a request-scoped managed bean in the adfc-config.xml file. This appears to be the problem since I have specified that the bean be managed by ADF, but used the JSF ManagedProperty annotation.
Request-scoped bean:
public class UserSecurityCompanies {
#ManagedProperty(value="#{securityApplication}")
private SecurityApplication securityApplication;
#PostConstruct
public void init() {
System.out.println("In UserSecurityCompanies.init");
System.out.println("SecurityApp.Test():" + getSecurityApplication().test());
}
public SecurityApplication getSecurityApplication() {
return securityApplication;
}
public void setSecurityApplication(SecurityApplication securityApplication) {
this.securityApplication = securityApplication;
}
}
The app-scoped bean is initialized during deployment of the app, but the NPE is thrown when getSecurityApplication().test() is called.
Steve
I'm currently evaluating Java EE 6 / JSF 2.1 with RichFaces.
A bean which is declared as
#ManagedBean
#ViewScoped
Gets an ID set (to prepare e.g. a delete operation).
Via JSF a confirmation popup is displayed.
If the user confirms, the delete method is invoked and removes the row for which the ID was stored in step 1.
Since CDI beans don't have a ViewScope I tried to declare the bean as:
#Named
#ConversationScoped
Now the processing fails in step 3. because the value that was set in step 1 (checked that) is no longer available.
Do I have to use Conversation.begin() and Conversation.end() methods?
If so, where would be good place to invoke them?
If you can upgrade to JSF 2.2, immediately do it. It offers a native #ViewScoped annotation for CDI.
import javax.faces.view.ViewScoped;
import javax.inject.Named;
#Named
#ViewScoped
public class Bean implements Serializable {
// ...
}
Alternatively, install OmniFaces which brings its own CDI compatible #ViewScoped, including a working #PreDestroy (which is broken on JSF #ViewScoped).
import javax.inject.Named;
import org.omnifaces.cdi.ViewScoped;
#Named
#ViewScoped
public class Bean implements Serializable {
// ...
}
Another alternative is to install MyFaces CODI which transparently bridges JSF 2.0/2.1 #ViewScoped to CDI. This only adds an autogenerated request parameter to the URL (like #ConversationScoped would do).
import javax.faces.bean.ViewScoped;
import javax.inject.Named;
#Named
#ViewScoped
public class Bean implements Serializable {
// ...
}
If you really need to use #ConversationScoped, then you indeed need to manually begin and end it. You need to #Inject a Conversation and invoke begin() in the #PostConstruct and end() in the latest step of the conversation, usually an action method which redirects to a new view.
import javax.enterprise.context.Conversation;
import javax.enterprise.context.ConversationScoped;
import javax.inject.Named;
#Named
#ConversationScoped
public class Bean implements Serializable {
#Inject
private Conversation conversation;
// ...
#PostConstruct
public void init() {
conversation.begin();
}
public String submit() {
// ...
conversation.end();
return "some.xhtml?faces-redirect=true";
}
}
See also:
How to choose the right bean scope?
I think you can benefit from CDI extension to create your own scope so you can implement the context and use the #NormalScope.
CDI fires an event AfterBeanDiscovery after each bean call
You can use CDI extension to #Observes this event and add your context implementation
In your scope implementation you can :
Use Contextual to get your bean by its name from FacesContext ViewRoot Map and return it after each ajax call back
Use CreationalContext if the bean name from first step is not found to create it in the FacesContext ViewRoot Map
For a more in-depth explanation, I recommend this link : http://www.verborgh.be/articles/2010/01/06/porting-the-viewscoped-jsf-annotation-to-cdi/
Inject the conversation into your bean and in the #PostConstructor method start the conversation if the conversation is transient.
And after deleting the record, end your conversation and navigate to your destination page. When beginning a conversation. Here is an example
public class BaseWebBean implements Serializable {
private final static Logger logger = LoggerFactory.getLogger(BaseWebBean.class);
#Inject
protected Conversation conversation;
#PostConstruct
protected void initBean(){
}
public void continueOrInitConversation() {
if (conversation.isTransient()) {
conversation.begin();
logger.trace("conversation with id {} has started by {}.", conversation.getId(), getClass().getName());
}
}
public void endConversationIfContinuing() {
if (!conversation.isTransient()) {
logger.trace("conversation with id {} has ended by {}.", conversation.getId(), getClass().getName());
conversation.end();
}
}
}
#ConversationScoped
#Named
public class yourBean extends BaseWebBean implements Serializable {
#PostConstruct
public void initBean() {
super.initBean();
continueOrInitConversation();
}
public String deleteRow(Row row)
{
/*delete your row here*/
endConversationIfContinuing();
return "yourDestinationPageAfter removal";
}
}
There is a project which holds an extentions to the Java EE stack features: DeltaSpike. It is a consolidation of Seam 3, Apache CODI. Above others, it includes the #ViewScoped into CDI. This is an old article and by now it has reached version 1.3.0
You can use:
import javax.annotation.PostConstruct;
import javax.faces.view.ViewScoped;
import javax.inject.Named;
#Named
#ViewScoped
public class PageController implements Serializable {
private String value;
public void setValue(String value) {
this.value = value;
}
public String getValue() {
return value;
}
public void execute() {
setValue("value");
}
#PostConstruct
public void init() {
System.out.println("postcontructor");
}
}
I am trying to get session scoped bean data in another Managed bean. When I am doing that value is coming as null and giving java.lang.NullPointerException error. I am new to JSF so keep in mind that I might be missing simple thing.
Here is the SessionScoped Bean
#ManagedBean
#SessionScoped
public class UserSessionBean {
private superProcessId;
//getter setter and other code
}
Here is the Managed Bean I am trying to get this data
#ManagedBean
public class AddProcessBean {
#ManagedProperty(value="#{UserSessionBean}")
private UserSessionBean sessionData;
//Getter Setter for sessionData
public UserSessionBean getSessionData() {
return sessionData;
}
public void setSessionData(UserSessionBean sessionData) {
this.sessionData = sessionData;
}
public void addAction() {
System.out.println(getSessionData().getSuperProcessId());
}
}
Your value is not good in #ManagedProperty. Use:
#ManagedProperty(value="#{userSessionBean}")
Default name for bean is same as class name with lower first letter. Also scope of your bean whose managed property is should be session or lower (view, request).
Every time I try to inject a session-scoped bean into my view-scoped beans, I get a NullPointerException when calling said bean. This problem is directly related to auto -instantiate a session bean?
Here is what I tried so far:
faces-config.xml
<managed-bean>
<managed-bean-name>sessionBean</managed-bean-name>
<managed-bean-class>com.example.SessionBean</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
<managed-bean>
<managed-bean-name>viewBean</managed-bean-name>
<managed-bean-class>com.example.ViewBean</managed-bean-class>
<managed-bean-scope>view</managed-bean-scope>
<managed-property>
<property-name>sessionBean</property-name>
<property-class>com.example.SessionBean</property-class>
<value>#{sessionMBean}</value>
</managed-property>
</managed-bean>
SessionBean.java:
package com.example;
public class SessionBean {
public SessionBean() {
System.out.println("Session is instantiated.");
}
public void sayHello() {
System.out.println("Hello from session");
}
}
ViewBean.java:
package com.example;
public class ViewBean {
private SessionBean sessionBean;
private String text = "Look at me!";
public ViewBean() {
System.out.println("View bean is instantiated.");
sessionBean.sayHello();
}
public SessionBean getSessionBean() {
return sessionBean;
}
public void setSessionBean(SessionBean sessionBean) {
this.sessionBean = sessionBean;
}
public String getText() {
return text;
}
}
and the relevant content of index.xhtml:
<f:view>
<h:outputText value="#{viewBean.text}"/>
</f:view>
And this is what I get:
com.sun.faces.mgbean.ManagedBeanCreationException: Cant instantiate class: com.example.ViewBean.
...
Caused by: java.lang.NullPointerException
at com.example.ViewBean.(ViewBean.java:12)
This runs (or rather doesn't run) on weblogic-10.3.6 with the shipped jsf-2-0.war deployed as a library.
What am I doing wrong here? I'm hoping this is not a container bug...
You cannot access the #SessionScoped bean in the #ViewScoped constructor. The #SessionScoped bean will be set AFTER the constructor of the #ViewScoped bean has been called.
Use the #PostConstruct annotation in some kind of init method to access the #SessionScoped bean.
public ViewBean() {
System.out.println("Constructor viewbean");
}
#PostConstruct
public void init() {
sessionBean.sayHello();
}
Further Links:
Why use #PostConstruct?
Spring Injection - access to the injected object within a constructor
I want to ask you if it is possible to inject a value to a sub-property of a bean.
This is what I want to do, but Eclipse IDE complains with:
Bean property beanB.subprop not found on parent class paq.paq.BeanA
<managed-bean>
<managed-bean-name>beanA</managed-bean-name>
<managed-bean-class>paq.paq.BeanA</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
<managed-property>
<property-name>beanB.subprop</property-name>
<value>Hello!</value>
</managed-property>
</managed-bean>
public class BeanA implements Serializable {
public BeanB beanB = null;
public Skin getBeanB() {
if (this.beanB == null) {
this.beanB = new BeanB();
}
return this.beanB;
}
public void setBeanB(BeanB beanB) {
this.beanB = beanB;
}
}
public class BeanB implements Serializable {
public String subprop = null;
public String getSubprop() {
return this.subprop;
}
public void setSubprop(String subprop) {
this.subprop = subprop;
}
}
Thanks!
Every nested bean needs to be declared as a managed bean as well. You need to inject it on the nested bean instead and then inject the nested bean itself in the main bean.
<managed-bean>
<managed-bean-name>beanA</managed-bean-name>
<managed-bean-class>paq.paq.BeanA</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
<managed-property>
<property-name>beanB</property-name>
<value>#{beanB}</value>
</managed-property>
</managed-bean>
<managed-bean>
<managed-bean-name>beanB</managed-bean-name>
<managed-bean-class>paq.paq.BeanB</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
<managed-property>
<property-name>subprop</property-name>
<value>Hello!</value>
</managed-property>
</managed-bean>