Note that there are some explanatory texts on larger screens.

plurals
  1. PORadio button is reset after immediate action (converter and ui:repeat mysteriously involved)
    primarykey
    data
    text
    <p>I came across a scenario were a set of radio buttons gets reset to a none-selected state whenever the view returns from an immediate action, although the actual value is processed correctly. Refreshing the view also displays the correct value. Weirdly enough, the behavior does only occur when the component uses an converter <em>and</em> is not inside a <code>ui:repeat</code> I narrowed it down to the following example:</p> <h3>JSF Page</h3> <pre><code>&lt;ui:repeat value="#{bean.stringList}" var="item" varStatus="iteration"&gt; #{item} &lt;h:selectOneRadio value="#{bean.boolValue}" rendered="#{iteration.last}"&gt; &lt;f:selectItems value="#{bean.boolItemList}" /&gt; &lt;f:converter converterId="testConverter" /&gt; &lt;/h:selectOneRadio&gt;&lt;br /&gt;&lt;br /&gt; &lt;/ui:repeat&gt; &lt;hr /&gt; &lt;h:selectOneRadio value="#{bean.stringValue}"&gt; &lt;f:selectItems value="#{bean.stringItemList}" /&gt; &lt;/h:selectOneRadio&gt; &lt;h:selectOneRadio value="#{bean.boolValue}"&gt; &lt;f:selectItems value="#{bean.boolItemList}" /&gt; &lt;f:converter converterId="testConverter" /&gt; &lt;/h:selectOneRadio&gt; &lt;p&gt; &lt;h:commandButton value="Submit" type="submit" /&gt; &lt;h:commandButton value="Do nothing, but immediate!" immediate="true" action="#{bean.doNothing}" /&gt; &lt;/p&gt; &lt;p&gt;boolValue is: #{bean.boolValue}&lt;/p&gt; </code></pre> <h3>Backing Bean</h3> <pre><code>@ManagedBean(name = "bean") @SessionScoped public class Bean { private List&lt;String&gt; stringList; private List&lt;SelectItem&gt; stringItemList; private List&lt;SelectItem&gt; boolItemList; private boolean boolValue; private String stringValue; private int removeParam; public Bean() { stringList = new ArrayList&lt;String&gt;(); stringList.add("Test Item One"); stringList.add("Test Item Two"); stringItemList = new ArrayList&lt;SelectItem&gt;(); stringItemList.add(new SelectItem("1", "Option One")); stringItemList.add(new SelectItem("2", "Option Two")); boolItemList = new ArrayList&lt;SelectItem&gt;(); boolItemList.add(new SelectItem(Boolean.TRUE, "This is real")); boolItemList.add(new SelectItem(Boolean.FALSE, "This is not real")); } // + getters and setters public void doNothing(){ } } </code></pre> <h3>Converter</h3> <pre><code>@FacesConverter("testConverter") public class TestConverter implements Converter { @Override public Object getAsObject(FacesContext context, UIComponent component, String value) { if (value!= null) { if (value.equals("real")) { return Boolean.TRUE; } } return Boolean.FALSE; } @Override public String getAsString(FacesContext context, UIComponent component, Object value) { boolean check = (Boolean) value; if (check) return "real"; else return "unreal"; } } </code></pre> <p>So we have:</p> <ul> <li>a group of radio buttons built from a backing bean list of <code>SelectItem(String, String)</code>, saved to a String. No converter needed.</li> <li>a group of radio buttons built from a backing bean list of <code>SelectItem(boolean, String)</code>, saved to a boolean. A converter is provided to map the boolean to some words ("real"/"unreal") - just to replace the standard converter ("true"/"false")</li> <li>A submit button + output to test if values get set correctly.</li> <li>A button with some immediate action, which does nothing else but change the JSF lifecycle.</li> </ul> <p>With this, one can play around and observe the following:</p> <ul> <li>Whenever the page returns from the immediate action, the radios with converter <em>outside</em> <code>ui:repeat</code> display no value, meaning: no radio is selected</li> <li>However, the value is actually set correctly during submit. Refreshing the page will results in correctly set radios.</li> <li>the <em>same</em> radio group <em>inside</em> <code>ui:repeat</code> works just fine, as does the one without the converter</li> </ul> <p>Testing around against better knowledge I checked that the converter-radios put inside a <code>&lt;c:foreach&gt;</code> instead of <code>ui:repeat</code> show the same error (which is as I would have expected, as it merely builds the tree and then vanishes) and that attaching some standard converter to the string-radios does not make them break. Regarding BalusC's comment, changing the bean to ViewScoped does not affect the problem - only then, refresh gives you a blank default-initialized page.</p> <p>Here I am at the end of my knowledge. I don't really see how to reduce this any further or why it behaves like this. So... well... ideas anybody? </p> <p>Specs I got from the server guy: Mojarra JSF API Implementation 2.0.5-FCS, JSF 2.0 and Facelets, Server is a Weblogic 11g 10.3</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload