0

I have a JSF web application with a view-scoped bean and a session-scoped bean. I'd like to modify the session bean's members from the view bean, and I followed this guide from a certain well-known JSF guy, but I can't seem to get it to work without a runtime exception. The reference to the managed session bean, "home" is null when referenced, similar to this question except I've already followed the advice of the accepted answer.

package brian.canadaShipping;

import java.io.Serializable;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;

@ManagedBean(name= "requestBean")
@ViewScoped
public class CpstcRequestBean implements Serializable {

@ManagedProperty(value="#{home}")
private CpstcHomeBean homeBean;


public CpstcHomeBean getHomeBean() {
    return homeBean;
}


public void setHomeBean(CpstcHomeBean homeBean) {
    this.homeBean = homeBean;
}


private static final long serialVersionUID = -5066913533772933899L;

public String testVar = "hello world";
private boolean displayOutput = false;

public boolean isDisplayOutput() {
    return displayOutput;
}


public void setDisplayOutput(boolean displayOutput) {
    this.displayOutput = displayOutput;
}


public String getTestVar() {
    return testVar;
}


public void setTestVar(String testVar) {
    this.testVar = testVar;
}


public CpstcRequestBean()
{
    System.out.println("TEST: " + homeBean.toString());
    System.out.println("Hello, ResuestBean!");
}

}

The first bit of my "home" bean is as follows:

@ManagedBean(name= "home")
@SessionScoped
public class CpstcHomeBean implements Serializable {
    ...

UPDATE: I've followed Jordan's suggestions and I have the following in my view-scoped bean:

@ManagedBean(name= "requestBean")
@ViewScoped
public class CpstcRequestBean implements Serializable {

@Inject @Named("home") CpstcHomeBean homeBean;

public CpstcHomeBean getHomeBean() {
    return homeBean;
}


public void setHomeBean(CpstcHomeBean homeBean) {
    this.homeBean = homeBean;
}

public CpstcRequestBean()
{
    System.out.println("TEST: " + homeBean.toString());
    System.out.println("Hello, ResuestBean!");
}
...

as well as this in my session-scoped bean:

@Named("home")
@SessionScoped
public class CpstcHomeBean implements Serializable {
...

yet my "home" bean reference is still null. Any ideas?

UPDATE 2: It turns out that you must use @Named in both classes, not just the injected class. My web app now loads but some elements are blank. In my console log, I see, "Target Unreachable, identifier 'home' resolved to null." I'm running on Tomcat 7, if that affects things. Any ideas?

Community
  • 1
  • 1
StockB
  • 755
  • 1
  • 13
  • 33

1 Answers1

0

You can either change your session bean's @ManagedBean to @Named and then just inject it into your view scoped bean OR you can reference the session bean as is like this:

FacesContext fc = FacesContext.getCurrentInstance()
private CpstcHomeBean homeBean = (CpstcHomeBean)  fc.getApplication().evaluateExpressionGet(fc, "#{home}", CpstcHomeBean.class);
Jordan Denison
  • 2,649
  • 14
  • 14
  • Just to clarify, do you mean, write: @Named("home") instead of: @ManagedBean(name="home") ? I know this seems like a basic "RTFM" question, but [this](http://docs.oracle.com/javaee/6/api/javax/inject/Named.html) API isn't so clear on its usage in JSF. [This example] just uses, "@Named" but I'm not sure where the name of the bean is defined then, if it's not a parameter of the annotation and if it's not defined in faces-config.xml. – StockB Oct 02 '12 at 00:32
  • Yes : ) Then use @Inject instead of ManagedProperty – Jordan Denison Oct 02 '12 at 00:40
  • I can't seem to import the "Named" class. Eclipse suggests javax.faces.event.NamedEvent instead... Trying the alternate method using FacesContext yields, "The type javax.el.ELException cannot be resolved. It is indirectly referenced from required .class files." – StockB Oct 02 '12 at 01:02
  • import javax.inject.Named and javax.inject.Inject – Jordan Denison Oct 02 '12 at 01:06
  • I'm still getting NullPointerException on the home bean reference. :-( – StockB Oct 02 '12 at 01:54
  • StackOverflow's comment system is choking on my @ references so please see the updated question. – StockB Oct 02 '12 at 01:56
  • I fixed the NullPointerException, but now I'm getting a different error at runtime when the page loads. The bean is now not visible from the xhtml file! (see OP) – StockB Oct 02 '12 at 02:14
  • Try removing Named after Inject – Jordan Denison Oct 02 '12 at 02:16
  • That fixed the console output, but I'm afraid that components are still failing to render, and HomeBean doesn't appear to be constructed. – StockB Oct 02 '12 at 02:28
  • BTW, I'm reading [the answer to this question](http://stackoverflow.com/questions/11986847/java-ee-6-javax-annotation-managedbean-vs-javax-inject-named-vs-javax-faces) and it seems like CDI annotations which you've recommended don't work natively with Tomcat. I tried running it on some modified Tomcat server which supposedly supported CDI (TomcatEE), but the project wouldn't even build. Should I migrate to a more robust backend? – StockB Oct 02 '12 at 02:32
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/17428/discussion-between-stockb-and-jordan-denison) – StockB Oct 02 '12 at 03:15
  • Did you create the "requestBean" in faces-config.xml too? – mdp Oct 20 '12 at 07:34