wood burning stoves*
The moose likes JSF and the fly likes JSF backingbean value is null always. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "JSF backingbean value is null always." Watch "JSF backingbean value is null always." New topic
Author

JSF backingbean value is null always.

Gopu Akraju
Ranch Hand

Joined: Jan 13, 2008
Posts: 242
In my JSF form, I have two radio buttons (#{testLane}). If the user selects 'yes', then a drop down list of numbers 1 till 8(#{testLaneList})
is displayed and the selected lane is being captured under #{UserBean.selectedLaneList}.

Everything works fine till the drop list displaying. When I select a number from a list of items, #{UserBean.selectedLaneList}is always NULL.
Why is it? I changed the topic b'cos of change in the focus of my trouble shooting. Thanks.


JSP file is as below:




face-config file
Gopu Akraju
Ranch Hand

Joined: Jan 13, 2008
Posts: 242
Why the value of backingbean is null when I activate through valuechangeLitener from the other JSF component?
Actually the valuechangeListener fo the radio button works and I could see the list of dropdown list, but when I select one item form the list and trying to set/get the value of backingbean, the value is null. Anything else needs to be done?


Thanks.
Gopu Akraju
Ranch Hand

Joined: Jan 13, 2008
Posts: 242
Can anyone suggest an alternate way of doing this? Or any bug in the changeValueListener of JSF component? Or do I have to go for AJAX? Thanks.
Bhaskar GR
Greenhorn

Joined: Jun 13, 2008
Posts: 28
Take binding value to select oneradio, In backing bean
private HtmlSelectOneRadio slectinput;


and set/get methods.
in value change listener write this point
radio this is your original value
radio=selectinput.getSubmittedValue().toString();
Gopu Akraju
Ranch Hand

Joined: Jan 13, 2008
Posts: 242
Hi Bhaskar,

I am bit confused. Initially I tried as below:




Could you please tell me how to do binding b'cos I tried as below and it is throwing me null pointer exception.




My get/set method are as below:



I get the exception when I try to print the value as below. I call this from a button action.

It would be really helpful if you please tell me how to bind the value of selected item from the list to radio button.

Thansks again.
Regards
Gopu


[ July 02, 2008: Message edited by: Gopu Akraju ]
[ July 02, 2008: Message edited by: Gopu Akraju ]
Bart Kummel
author
Ranch Hand

Joined: Nov 30, 2007
Posts: 81
Hi Gopu,

I think you don't need binding at all. If you change your code like this, it should work:

Note that I replaced the rendered value of the selectOneMenu. This already contains the value you need, so I don't see why you should process that in a backing bean. Hope this helps. Good luck!

Best regards,
Bart Kummel


SCJP 1.4 | SCJD 1.6 | Visit my website | Author of the book Apache MyFaces 1.2 Web Application Development
Gopu Akraju
Ranch Hand

Joined: Jan 13, 2008
Posts: 242
Thanks bart.
My requirement is different.

Let me explain bit more clearly again.

I have two radio button "yes" and "no". If the user selects "yes", then I need to display a list of items in a drop down menu (till this my code works!) and then when the user selects an item from the list, I need to get this value through a backing bean (which I haven't succeeded yet)
If the user selects "no", then no list is displayed.

I used rendered because I want the list testLaneList to be displayed only when the user selects the yes radio button.

I am setting the boolean value to true as per below. It works till then. If the user selects "No" radio button, then the list disappears.



Now my problem is getting the selected item from the list. I need to grab this value from the list through a backing bean.

Hope I have clearly explained about my problem now.
Andres Quinones
Ranch Hand

Joined: Oct 09, 2006
Posts: 57
Gopu,

I think that the problem comes from the rendered attribute from the list, in the JSF Life Cycle if the rendered attribute is false the value from the list will never be set in the Bean. In this case I recommend you to send as a request parameter the value from the radio button, and in the Bean Constructor method you manually set this to the property that renders it. Or probably easily you can set your Bean to Session Scope.

Hope this helps,
[ July 02, 2008: Message edited by: Andres Quinones ]
Gopu Akraju
Ranch Hand

Joined: Jan 13, 2008
Posts: 242
In this case I recommend you to send as a request parameter the value from the radio button, and in the Bean Constructor method you manually set this to the property that renders it.


Could you please explain bit more on how to do this?

And I tired you second option as you have mentioned as below: But it didn't work.
Or probably easily you can set your Bean to Session Scope.






This looks like a simple task but as a beginner, I am struck. It would be great if I get a break.
Cheers
Gopu
Gopu Akraju
Ranch Hand

Joined: Jan 13, 2008
Posts: 242
Can anyone please help me to solve this issue? I don't see many examples to follow up regarding this. Thanks.
[ July 03, 2008: Message edited by: Gopu Akraju ]
Andres Quinones
Ranch Hand

Joined: Oct 09, 2006
Posts: 57
Gopu is your UserBean in the session Scope??? I think it is not.

The value is null when you change the selected value of the list or when you submit the form???

If it is null when you submit the form in the constructor of UserBean and your bean is on the request scope int the class of yur JSF Bean

public UserBean(){

HttpServletRequest req = (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
String param = req.getParameter("visible");
if( param != null && "Yes".equals(param))
booleanValue = true;
else
booleanValue = false;

}

and in the jsp

<h:commandButton ......>
<f:param name="visible" value="#{UserBean.booleanValue}"/>
</h:commandButton>


This will do that the rendered property of your list be true and the value to be set in the bean and avoid it being null.

I hope you understand everything otherwise feel free to ask anything.
Gopu Akraju
Ranch Hand

Joined: Jan 13, 2008
Posts: 242
Hi Andres,

I am totally confused. I am attaching a sample code as below.

page1.jsp



UserBean.java


faces-config.xml


page2.jsp



Please tell me why UserBean.selectedLaneListTest is null? Thanks.
[ July 07, 2008: Message edited by: Gopu Akraju ]
Gopu Akraju
Ranch Hand

Joined: Jan 13, 2008
Posts: 242
Sorry to disturb all. Can anyone really see my mistake and tell me why UserBean.selectedLaneListTest is null. I have enclosed all my sample code jsut above this message like page1.jsp, page2.jsp, config file and UserBean.java. Any help would be truly appreciated. I am not able to resove this issue. Thanks.
Darryl Nortje
Ranch Hand

Joined: Jun 11, 2002
Posts: 140
Hi Gopu,

Andres asked the question "Gopu is your UserBean in the session Scope??? I think it is not." Just before you posted all your code. In your jsf config xml file you have this.

<managed-bean>
<managed-bean-name>UserBean</managed-bean-name>
<managed-bean-class>test.user.UserBean</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>

Have you tried Andres' suggestion?
Andres Quinones
Ranch Hand

Joined: Oct 09, 2006
Posts: 57
Gopu the UserBean.selectedLaneListTest is null when the page2.jsp is loaded??

If it is just try the following, remove from the selectOneMenu the rendered attribute just to test.

And tell me what happen.
Gopu Akraju
Ranch Hand

Joined: Jan 13, 2008
Posts: 242
Hi Darryl,
I tried Andre's suggestion of but still I couldn't get the value of


Hi Andres,

Just for testing purposes, I removed rendered from as below:


and then changing my bean value from to with corresponding get/set method. Then depending upon my selection from the drop down list (1,2..8) , I could grab the selected item from the list in the and it works fine.

Since my request is, I want the list (ie controlLaneList) to appear only when the user selects 'yes' radio button, and the page gets refreshes as I have used upon value change for radio button and as I am setting the boolean value in the method , the list appears but then I couldn't grab the value selected from a drop down list.

I tried private HtmlSelectOneMenu selectedLaneListTest;[/CODE], as the page gets refreshed, but failed to get the value.

Thanks and let me know your suggestions.
Gopu Akraju
Ranch Hand

Joined: Jan 13, 2008
Posts: 242
Hi Andres,

Any suggestion for my problem? Thanks.
Andres Quinones
Ranch Hand

Joined: Oct 09, 2006
Posts: 57
Hi Gopu,

I think that a library like Tomahawk can help you to solve your problem. This is from Apache and they created a tag named saveState, that will save the state the list within the selectOneMenu is created. So this will make the value is not null when you select something after the submit of the form. Just check it an tell me any comments you have about it. Also you can try setting both JSF Managed Beans in Session the User Bean and the other one in which you have the map to fill the selectOne Menu.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JSF backingbean value is null always.