File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSF and the fly likes Value Change Event is not working Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "Value Change Event is not working" Watch "Value Change Event is not working" New topic
Author

Value Change Event is not working

Russel Fleming
Greenhorn

Joined: Mar 18, 2013
Posts: 4
This problem is regarding valueChangeEvent.


This is the xhtml where I am setting the BooleanCheckBoxId=#{productItem.uniqueId}

<h:panelGrid columns="2" >
<h:panelGroup>
<h:selectBooleanCheckbox id="#{productItem.uniqueId}"
value="#{productItem.checkedValue}"
disabled="#{productItem.disabled}"
valueChangeListener="#{spcProductModel.productClicked}">
<a4j:support event="onclick"
oncomplete="retrieveActionText(this);"
reRender="paymentFormCard,actionText,includedForm,parametersCardForm,productsPanel,spcTemplateForm,productsPanel,#{paramPanelId}" />
</h:selectBooleanCheckbox>
</h:panelGroup>
<h:panelGroup>
<h:outputLabel>id="#{productItem.uniqueId}"</h:outputLabel>
<h:outputLabel
value=" #{productItem.productRelation.product.productName}"
for="#{productItem.uniqueId}"></h:outputLabel>
</h:panelGroup>
</h:panelGrid>

The unique ID has been generated using the following code:
String uniqueIds = prod.getUniqueId();

Thus the BooleanCheckbox is becoming equal to UniqueIds.
When I am printing the BooleanCheckBoxId in my front end for checking purpose, it reflects the changes made. But when I am fetching it as
String selectedId = (String) vce.getComponent().getId(); and printing the selectedId in logger, it remains same as it generated in the very first time.

SelectedId is remaining same as the old one, It does not change.
Is there anything I need to add?
Because it works fine for the first time. but when the value changes in the previous calling page, it does not work.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16228
    
  21

When you correct your display name, I'll look at your problem.


Customer surveys are for companies who didn't pay proper attention to begin with.
Vasundhara Sahi
Greenhorn

Joined: Mar 21, 2012
Posts: 17

Nitrate developer kindly do the needful as suggested by the forum moderator.

That way we all get to learn from Tim.

Thanks


Truth be told .....I am a Java noob....
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16228
    
  21

A cynic might suspect that we're still dealing with a false name here, but it's close enough.

Incidentally, the sample XML would be a lot easier to read if you used the Code button to add formatting tags.

The "id=" attribute in JSF cannot be an EL expression. It must be a literal value. See http://docs.oracle.com/javaee/5/javaserverfaces/1.2/docs/tlddocs/h/selectBooleanCheckbox.html where you'll note that the type must be java.lang.String, and not a ValueExpression (EL expression).

I am in doubt about using a valueChangeListener on the checkbox, but that's a secondary issue.
Russel Fleming
Greenhorn

Joined: Mar 18, 2013
Posts: 4
Thanks tim, for your response.
Could you please elaborate a bit more, so that I can implement that on my code?
I am a beginner in JSF.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16228
    
  21

You can't use a variable expression to generate id values, so you'll have to make other arrangements.

My suspicion is that you are either expecting the valueChangeListener to be called when the checkbox is checked. That's not how JSF listeners work. JSF listeners listen to a form submit or ajax event. The valueChangeListener only gets called when the form is submitted and a change has been detected between the original displayed value and the value coming back from the submit. If you are doing any sort of serious processing, it should NOT be done in a valueChangeListener, it should be done in an action method. Stuff done in a valueChangeListener method only gets done if an actual value change is detected, and the listeners act before the backing bean is fully prepared for general action, so often what you do in a listener gets undone by later processing. The data isn't fully vetted, updated, and stable until the action method is invoked.

Russel Fleming
Greenhorn

Joined: Mar 18, 2013
Posts: 4
So you are suggesting me to use ActionListener instead of ValueChangeListener?
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16228
    
  21

No. I discourage Listeners. They reduce reusability, testability and maintainability. They have their place, but 95 times out of 100, an action method will do the same job as an ActionListener and do it as POJO code.
Russel Fleming
Greenhorn

Joined: Mar 18, 2013
Posts: 4
Could you please tell me how to implement action in the code I have given?
that would be very helpful.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16228
    
  21

Something more like this:


Where productClicked is coded the same way you'd code a regular commandButton action method (public void productClicked(){....}). Except that unlike regular (non-AJAX) actions, the a4j action won't return a String, since you're staying on the same page. Hence it returns void.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Value Change Event is not working