views:

441

answers:

5
+1  Q: 

JSF data binding

I am having trouble using JSF just wanted to run it by so if there is anything obvious someone can spot. I have a managed bean which is giving me trouble. In my faces-config.xml I have:

<managed-bean>
  <description>Info Bean</description>
  <managed-bean-name>InfoBean</managed-bean-name>
  <managed-bean-class>bean.InfoBean</managed-bean-class>
  <managed-bean-scope>request</managed-bean-scope>
</managed-bean>

In my JSF I have the following:

<h:outputText value="#{InfoBean.deviceModel}" rendered="true"></h:outputText>

I have a POJO for InfoBean as follows:

public class InfoBean {

String deviceModel;
String userEmail;
String active;

public InfoBean() {
 // TODO Auto-generated constructor stub
}

public String getDeviceModel() {
 return deviceModel;
}
public void setDeviceModel(String deviceModel) {
 this.deviceModel = deviceModel;
}
public String getUserEmail() {
 return userEmail;
}
public void setUserEmail(String userEmail) {
 this.userEmail = userEmail;
}
public String getActive() {
 return active;
}
public void setActive(String active) {
 this.active = active;
}
}

There is a no arg constructor in POJO too, but for some reason the deviceModel value does not get displayed to the screen and I cannot figure out why! Any help much appreciated. I have a handler which is also in the faces-config as a separate managed bean, when the user clicks a button, control goes to handler class which calls a service that populates fields in the POJO InfoBean, so as I can see it should appear but it does not!

Any help much appreciated.

A: 

How your deviceModel property of the bean is populated?

Are you sure that it is not null? You can eventually check that by putting a log in the getter method:

public String getDeviceModel() {
    System.out.println("Getter called: " + deviceModel + ".");
    return deviceModel;
}

Eventually, you can modify the scope of the bean to set it as session.

romaintaz
A: 

HI All,

Thank you for your comments I think I am getting one step closer. I followed the above suggestion and added a sysout to the getter for deviceModel. I can see it being printed in the standard output as :

10:30:34,932 INFO  [STDOUT] Getter called: G50000.

However I noticed other output below this which is showing:

10:30:38,885 INFO  [renderkit] component.getValue() returned null

10:30:38,885 INFO [renderkit] Value to be rendered null 10:30:38,900 INFO [renderkit] component.getValue() returned 10:30:38,900 INFO [renderkit] Value to be rendered

So appears that the deviceModel is null later on when it is being rendered, any ideas how to troubleshoot this. Where and why it is being set to null. My thinking is that since it is defined in the faces-config as havign scope session, so where ever the InfoBean gets set it should hold its values and be rendered to the jsp as it is in the config file?

msharma
+1  A: 

I have sorted out the issue and the solution is that since I had a model like this: JSP button is clicked->call goes to Handler->handler calls method in service->Service populates the managed bean InfoBean and returns it to handler

The managed bean even though declared in the config file with scope as session was NOT actually part of the session. In my handler after returning the InfoBean I added:

HttpSession session = (HttpSession)FacesContext.getCurrentInstance().getExternalContext().getSession(false);
 session.setAttribute("InfoBean", InfoBean);

This placed it in the session and immediately and values started appearing! :-))

I have read several articles about this and never seen this mentioned, so I am wondering how it is done otherwise. One other suggestion I got was make InfoBean a private instance of the Handler with getters and setters, this way it will get created with the handler and will also be olk. I have not tried this approach though. Thanks to all who helped.

msharma
A: 

Your post shows it being defined in request scope not session scope. If you change it to session, you won't need put it in using setAttribute(). Or maybe I'm missing something.

Wayne Young
A: 

Despite changign the scope to session, it was not working, the above code where I add it to the HttpSession is necessary in order for this to work, or so I have found.

Thanks.

msharma