开发者

How to set default value of h:selectOneRadio button

开发者 https://www.devze.com 2022-12-23 22:44 出处:网络
I am unable to set the default value of h:selectOneRadio as i need radio button to be pre selected <h:select开发者_运维技巧OneRadio id=\"myRadio\" value=\"#{Externalbean.addressFlag}\" >

I am unable to set the default value of h:selectOneRadio as i need radio button to be pre selected

<h:select开发者_运维技巧OneRadio id="myRadio" value="#{Externalbean.addressFlag}" >
<f:selectItem itemValue="1" itemLabel="Yes"/>
<f:selectItem itemValue="0" itemLabel="No"/>
</h:selectOneRadio>

and my backing bean is

private String addressFlag="0";

public String getAddressFlag() {
    return addressFlag;
}


public void setAddressFlag(String addressFlag) {
        this.addressFlag = addressFlag;
    }

but no luck


You need to set the default value in the init method of your backing bean:

@ManagedBean
public class YourBackingBean implements Serializable {

    private String addressFlag;

    @PostConstruct
    public void init() {
        addressFlag = "0";
    }

    public String getAddressFlag() {
        return addressFlag;
    }

    public void setAddressFlag(String addressFlag) {
        this.addressFlag = addressFlag;
    }
}


I did a little test, it works just fine as expected, but I also observed that when you leave out the <h:form> tag, the behaviour of radio buttons is unpredictable and dependent on the webbrowser. The JSF-generated HTML output looks correct, but the webbrowser would in the view only select the button which was actually selected by the user in the previous request on the same page. If the cache is empty, none of the buttons is selected. At least, that was the case in FF.

So, it look like that you have forgotten to put a <h:form> around it.


Only use that <h:selectOneRadio required="true"> :

<h:selectOneRadio required="true" id="myRadio" value="#{Externalbean.addressFlag}" >
  <f:selectItem itemValue="1" itemLabel="Yes"/>
  <f:selectItem itemValue="0" itemLabel="No"/>
</h:selectOneRadio>


Did you try to set the addressFlag as an Integer?

private Integer addressFlag = 0;

public Integer getAddressFlag() {
    return addressFlag;
}

public void setAddressFlag(Integer addressFlag) {
    this.addressFlag = addressFlag;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号