Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Display Different Sections while selecting different radio buttons

 
Mahendra Chowdary
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,

I am quite new to JSF. I have a query . I need to display two different panel when i select the different radio buttons . I am using the rich faces tags in my jsf page. I know how we can achieve this using javascript. But my interest is to achieve this functionality using Ajax. Is it possible . can somebody suggest.
 
Sumeet Singh Aryan
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please provide the sample code that you are trying.

I think you are not aware of reRender AJAX property. Do some google on it. this will surely help you.
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18276
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is very possible. Simply use the "rendered=" attribute on the two panels in question and use EL expressions such as "#{myBean.radioSelection == 1}"/"#{myBean.radioSelection == 2}"

Use of AJAX support such as RichFaces reRender can reduce the need for full-page refresh, but the end result is the same.
 
Mahendra Chowdary
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,

Could sombody provide me some sample code so that i can render component on the basis of ajax request

Thanks
Mahi
 
Mahendra Chowdary
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please see my code ,

In the page i have,

<h:selectOneRadio id="Internalradioselect">
<f:selectItem id="InternalPerson" itemLabel="Person" itemValue="11">
<a:support event="onselect" reRender="panel1" action="#{addcontact.firstRadioChecked}" ></a:support>
</f:selectItem>
<f:selectItem id="InternalDistributionList" itemLabel="DistributionList" itemValue="22">
<a:support event="onselect" reRender="panel2" action="#{addcontact.secondRadioChecked}" ></a:support>
</f:selectItem>
</h:selectOneRadio>

<rich:panel id="panel1" rendered="#{addcontact.firstRadioChecked}">
Welcome to Panel 1
</rich:panel>

<rich:panel id="panel2" rendered="#{addcontact.secondRadioChecked}">
Welcome to Panel 2
</rich:panel>


In the java file i have

private Boolean firstRadioChecked = false;
private Boolean secondRadioChecked = false;

public Boolean getFirstRadioChecked() {
firstRadioChecked = true;
return firstRadioChecked;
}

public void setFirstRadioChecked(Boolean firstRadioChecked) {
this.firstRadioChecked = firstRadioChecked;
}

public Boolean getSecondRadioChecked() {
secondRadioChecked = true;
return secondRadioChecked;
}

public void setSecondRadioChecked(Boolean secondRadioChecked) {
this.secondRadioChecked = secondRadioChecked;
}





But while viewing on the page i am getting the following exception ..

javax.el.PropertyNotFoundException: /addcontact.xhtml @101,73 rendered="#{addcontact.firstRadioChecked}": Property 'firstRadioChecked' not found on type org.javassist.tmp.java.lang.Object_$$_javassist_1


Please suggest


Thanks
Baziaz
 
Mahendra Chowdary
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When i try to run the page

the methods in the bean class are getting executed even before i perform an action like selecting a button on the page .

Can somebody please look into this .

Thanks
 
Ilari Moilanen
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I presume you solved your first problem since you now tell us that you have been able to view the page?

In you java code you do this
So you getter method always return true and always sets the property in question as selected. Why?!? What is the logic here? Shouldn't the getter only get the property and setter set it when needed?
 
Mahendra Chowdary
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I will call this function only when i select a particular radio button

Now , i am having another problem ,,

All the methods in my bean are getting executed without performing an action
 
Ilari Moilanen
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Go Green wrote:I will call this function only when i select a particular radio button

Now , i am having another problem ,,

All the methods in my bean are getting executed without performing an action
I see. So your real problem is that you do not understand basic concepts of JSF framework. Your form is sending the values to the bean when the forms is sent BUT the initial values of the inputs in the form ALSO come from the backing bean. That is why all the getters of the properties present in the form are being executed when the form is viewed! So if you think that the getters are only being executed when you call them yourself you are wrong.

So, as a simplification, the getters are being called when the form is viewed and setters are being called when the form is sent to server. Of course AJAX calls can make other getter and setter calls but the general rule still applies.
 
Mahendra Chowdary
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So ,

How should i modify my code so as to satisfy the following requirement.

I need to display different panels when i select different radio buttons in the page .
 
Mahendra Chowdary
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry ,

Double Post
 
Ilari Moilanen
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, first of all fix your java code so that the getters behave as they should be and only return the values.

And if you need to show one of the panels initially you can change the initialization for example to this
or do something similar.

But what comes to you ajax code I can not help you there. Sorry. I am not familiar with Rich Faces tags.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic