Search code examples
ajaxjsf-2radiobuttonlist

SelectOneRadio set time on selection


I've been raking my brain trying to figure this one out. I have a really simple requirement that's turning out to be a very interesting problem.

The requirement is to update a time stamp when the user chooses a selection in a radio button list.

So I have a radio button list:

...
<h:selectOneRadio value="#{mybean.myvalue}" >
    <f:selectItem itemLabel="A" itemValue="A"/>
    <f:selectItem itemLabel="B" itemValue="B"/>
    <f:selectItem itemLabel="C" itemValue="C"/>
    <f:selectItem itemLabel="D" itemValue="D"/>
    <f:ajax render="timeBox" listener="#{measurements.captureTime('timeBox')}"/>
</h:selectOneRadio>
...

And a text box a little further down the page:

...
<h:outputText id="timeBox" value='#{measurements.timeBox}'>
    <f:convertDateTime pattern="HH:mm:ss"/>
</h:outputText>
...

And then in the backing bean I have the listener defined:

    ...
public void captureTime(String id){
       if(id.equals("timeBox"){
           timeBox = new Date(System.currentTimeMillis());
       }
}
    ...

But when I change the radio button, the value is not updating. I'm pretty sure this is to do with the various phases of JSF (ie. it's not being updated because of the problem in this post: http://balusc.blogspot.ca/2012/03/reset-non-processed-input-components-on.html) but I'm not entirely sure how to fix it.. I'd really like to avoid including another library if possible.

As always let me know if you need more info and I will be happy to oblige.

Thanks!

Edit The structure of these components is:

<h:form>
<table>
  <tr>
    <td>
     <h:select.../>
    </td>
  </tr>
  <tr>
    <td>
     <h:output.../>
    </td>
  </tr>
</table>
</h:form>

Solution

  • The given construct suggests that you've duplicate components with the same ID. Make sure that you don't actually have duplicate components with the same ID.