• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

valueChangeListener not called.

 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Experts,
Please be patient in reading the problem as it is a long one.

Im working on a application wherein im using JavaServerFaces in the Web layer.I'm relatively new to the world of JSF and stil playing around with its intecasies.
I have the following UI components h:selectOneMenu,h:selectManyListbox,h:selectOneRadio & h:selectManyCheckbox in the jsp.
These display the various values as drop downs which are selected by the user.
I also have to display the default values as selected in these drop downs and check boxes.
One restriction is that i donot know how many of these components my jsp will have , so i cant create that number of attributes in the managed bean to capture the selected values from the user for each of these components.
Some time i may have two h:selectOneMenu and four h:selectManyListbox and sometime i may have five h:selectManyListbox and two h:selectOneRadio. So the bottomline is that, it can be of any permutaion /combinatins.
So in order to capture the selected values i've used valueChangeListener. All of these componets are binded to a listener i.e its valueChangeListener attribute is been used.
In the method which implement the valueChangeListener , i create a arraylist and stores the selected values in it.This way im storing the selected values.
So when the user submits the form the method implemeting the valuechangelistener is called and the corrosponding arraylist is populated.
I've to display the default value for these components.So in the value attribute of these components i have provided with default
selection.
But now i have ran in to a major bug.I dont know whether it is a discrepancy in the behaviour of valueChangeListener or i have implemented it wrongly.
If we don't change the selected values of the component then it's ValueChangeListener is not called i.e if the user performs a submit without changing the value of any of the component then the ValueChangeListener is not called.
I need couple of help
1)Whether my approach is correct or it is wrong.
2)If the approach is wrong then how do i acheive the following behaviour.
3)How do i capture the changed values as well as the defalut values in this case.

Thanks in advance for any kind of help provided.
Regards
 
Ranch Hand
Posts: 572
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you post the code you have implemented currently, it will help to better understand and help.
 
ved gunjan
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Thanks Ali for the reply.

Here's the jsp code:

<h:dataTable value="#{requirementBean.questionList}" var="questions" id="questionTable" columnClasses="none,tableRowOdd" rowClasses="tableRowEven,tableRowOdd"
cellpadding="1" cellspacing="1" width="100%">
<h:column >
<h:outputText value="#{questions.questionText}" rendered="#{questions.displayType ne 'String'}" />
<h:outputText value="#{questions.questionText}" rendered="#{questions.displayType eq 'String'}" styleClass="divheader" style="width: 100%;"/>
</h:column>
<h:column>
<h:graphicImage url="../../images/spacer.gif" width="1%" />
<h:inputText rendered="#{questions.displayType eq 'TextBox'}" id="inputTextid" value="#{questions.defaultAnswerString}"
validator="#{requirementBean.validateInput}" />

<h:selectOneMenu value="#{questions.defaultAnswerInteger}" rendered="#{questions.displayType eq 'DropDown'}"
id="selectOneMenuid" valueChangeListener="#{requirementBean.valueChangeSelectOneMenu}">
<f:selectItems value="#{questions.option}" />
</h:selectOneMenu>

<h:selectManyListbox size="5" value="#{questions.defaultAnswerArray}" rendered="#{questions.displayType eq 'MDropDown'}"
id="selectManyListboxid" valueChangeListener="#{requirementBean.valueChangeSelectManyListbox}" >
<f:selectItems value="#{questions.option}" />
</h:selectManyListbox>

<h:selectOneRadio value="#{questions.defaultAnswerInteger}" rendered="#{questions.displayType eq 'RadioButton'}" id="selectOneRadioid">
<f:selectItems value="#{questions.option}"/>
</h:selectOneRadio>

<h:selectManyCheckbox value="#{questions.defaultAnswerArray}" rendered="#{questions.displayType eq 'CheckBox'}"
id="selectManyCheckboxid" valueChangeListener="#{requirementBean.valueChangeSelectManyCheckbox}">
<f:selectItems value="#{questions.option}" />
</h:selectManyCheckbox>
</h:column>
</h:dataTable>

=========================================================
Here's the java code that is written in the managed bean.
/**
* valueChangeSelectOneMenu
*
* @param ValueChangeEvent evt
*
* puts the selected value of drop down into a List
*/
public void valueChangeSelectOneMenu(ValueChangeEvent evt) {
selectedValueOfDropDown.add((Integer) evt.getNewValue());
}

/**
* valueChangeSelectManyListbox
*
* @param ValueChangeEvent evt
*
* Puts the selected answer which is array of Integer into List
*/
public void valueChangeSelectManyListbox(ValueChangeEvent evt) {
// evt.getNewValue() This comes as array of integer
valueMultipleSelectList.add(evt.getNewValue());
}

/**
* valueChangeSelectManyCheckbox
*
* @param ValueChangeEvent evt
*
* Puts the selected answer which is array of Integer into List
*/
public void valueChangeSelectManyCheckbox(ValueChangeEvent evt) {
valueSelectManyCheckbox.add(evt.getNewValue());
}



Hope this make the things more clear.
Please let me know if things are stil not clear.


Thanks & Regards
Ved
 
Ali Gohar
Ranch Hand
Posts: 572
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't know your situation exactly but by looking at the code i can see that you are displaying questions and taking the answer from the user. Answers are taken using different controls like h:selectOneMenu and h:selectManyList etc

I am not sure why do you want to use valueChangeListener to capture the values? Just define different variables in Question bean to hold the answer accordingly for each control. specify them in the value attribute of that control and these will be updated accordingly for that corresponding Qestion Bean in the list of Questions when the form will be sumited.

Let me know if you are not clear?
 
ved gunjan
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ali for the reply...
The reason why i cannot create attribute/properties in the Backing beans for each corrosponding UIComponents(h:selectOneMenu,h:selectManyListbox,h:selectOneRadio & h:selectManyCheckbox in the jsp) is that, these components corrosponds to the questions, as you have understood rightly, but these questions are dynamic in nature... means some time there may be 30 quesitions, sometime there may be 20 questions, so now you understand why i cant create the properties/attributes in the backing bean before hand.

So what i have done is that , i have binded these components to valueChangeListiner, and whenever the value changes the implementing method gets called.

I think now things are bit more clear.
Thanks
Ved
 
Ali Gohar
Ranch Hand
Posts: 572
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No i am not saying to have those values in backing bean. Here is code snippet from your last post


<h:dataTable value="#{requirementBean.questionList}" var="questions" id="questionTable" columnClasses="none,tableRowOdd" rowClasses="tableRowEven,tableRowOdd"
cellpadding="1" cellspacing="1" width="100%">



Here what is "requirementBean.questionList" in value attribute? I guess it would be an Arraylist containing some beans representing each question. Means one question bean represent one row of datatable.

So i am saying to add those attributes in Question bean.

I guess i am clear now?
 
ved gunjan
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Ali,
Thanks for bearing with me...
See i have no problem in displaying the components for each questions. I have succeded in doing that.
The problem is how do i capture the values of these componets,submitted by the user.
The way you suggested is that to have that as many attributes/properties in backing beans as the number of components in jsp...but the problem is that these numbers are dynamic so i cant create these attributes in the backing beans.

so i used the way of valuechangelistener.
i binded these components with valuechangelistener and i've succeded in capturing the values, but now i ran into a major bug.
i had this requirement of displaying defalut values for dropdowns/multiselects/chakbox/radio buttons.
I'm still successful in getting the values for the components whose values are changed from the defalut selection. snice the user changes the default selection the valuechangelistener gets called. but if the user submits with defult selection the valuechangelistener method does not get called.
so i fail for such questions.

My query is:
1) Is my approach correct for my requirement.
2) and how to solve the present bug.

Hope this time it is more clear.
thanks
ved
 
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In your action method, you may check to see if the lists' (like valueSelectManyCheckbox, valueMultipleListbox...) sizes are 0. If 0 then add the corresponding default value.

But in your approach, what if the user selects something and later changes his/her selection? Will it get added two times to the list? So, you may even want to clear the list before adding the new value.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whenever you add valueChangeListener you have to use onchange="submit();"

Try like this. Your method will be invoked

<h:selectOneMenu id="dropdown2" value="#{Bean.layout}"
onchange="submit();" valueChangeListener="#{Bean.layoutChangeListener}">
<f:selectItems id="Items" value="#{Bean.layout}"/>
</h:selectOneMenu>
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic