Managedbean in managedbean null in ctrl class generalInfo is null
Managedbean in managedbean null in ctrl class generalInfo is null
I'm a beginner in JSF and priemfaces.
I'm trying to inject a managed bean into an other one.
So i have a managed bean named generalInfo declared in face-config.xml
<managed-bean>
<managed-bean-name>generalInfo</managed-bean-name>
<managed-bean-class>com.pa.GeneralInfo</managed-bean-class>
<managed-bean-scope>application</managed-bean-scope>
</managed-bean>
<managed-bean>
<managed-bean-name>generalInfo</managed-bean-name>
<managed-bean-class>com.pa.GeneralInfo</managed-bean-class>
<managed-bean-scope>application</managed-bean-scope>
</managed-bean>
public class GeneralInfo implements Serializable {
private static final long serialVersionUID = 6549721328365328556L;
private MapModel emptyModel;
etc etc etc
public GeneralInfo() {
emptyModel = new DefaultMapModel();
}
etc etc etc
{
public class GeneralInfo implements Serializable {
private static final long serialVersionUID = 6549721328365328556L;
private MapModel emptyModel;
etc etc etc
public GeneralInfo() {
emptyModel = new DefaultMapModel();
}
etc etc etc
{
and the managedbean that will contain the generalInfo
public class ctrl implements Serializable {
@ManagedProperty(value="#{generalInfo}")
private GeneralInfo generalInfo ;
public GeneralInfo getGeneralInfo() {
System.out.println("get generalInfo");
return generalInfo;
}
public void setGeneralInfo(GeneralInfo generalInfo) {
System.out.println("set generalInfo");
this.generalInfo = generalInfo;
}
public void doSomething(){
syso(generalInfo.getName()); // generalInfo is null
}
}
public class ctrl implements Serializable {
@ManagedProperty(value="#{generalInfo}")
private GeneralInfo generalInfo ;
public GeneralInfo getGeneralInfo() {
System.out.println("get generalInfo");
return generalInfo;
}
public void setGeneralInfo(GeneralInfo generalInfo) {
System.out.println("set generalInfo");
this.generalInfo = generalInfo;
}
public void doSomething(){
syso(generalInfo.getName()); // generalInfo is null
}
}
declared in face-config.xml with view scope.
I can access generalInfo from xhtml pages but in ctrl class generalInfo is null
Where is the problem?
the setter and the getter of generalInfo are never called.
Well, for one thing "ctrl" is a very bad classname. There is a convention in Java that classnames start with an upper-case letter and that object instance and property names must start with a lower-case letter. Many tools will malfunction if you do not adhere to that standard.
Your sample code also does not include a package statement. If the actual code is also lacking the package statement, that places the class in question into the Java default package, which is also something that can cause you problems.
Finally, while your injected bean definition is OK, the injection target specifications are incomplete. So I cannot confirm that everything is properly done.
You are mixing XML (faces-config) and annotations. Be aware that XML will override annotations. It's best to use the same technique for everything to reduce confusion.
The following faces-config elements should function. Note that I have altered a few things to make them comply with JSF requirements. Actual details may vary.
<managed-bean>
<managed-bean-name>generalInfo</managed-bean-name>
<managed-bean-class>com.pa.GeneralInfo</managed-bean-class>
<managed-bean-scope>application</managed-bean-scope>
</managed-bean>
<managed-bean>
<managed-bean-name>ctrl</managed-bean-name>
<managed-bean-class>com.pa.Ctrl</managed-bean-class>
<managed-bean-scope>view</managed-bean-scope>
<managed-property>
<property-name>generalInfo</property-name>
<value>#{generalInfo}</value>
</managed-property>
</managed-bean>