简体   繁体   中英

How to unchecked all choices as default of select one radio button in JSF?

I have the following JSF radio button:

<h:selectOneRadio id="journeyDir"
  value="#{refParam.journeyDir}">
  <f:selectItems value="#{newMessage.journeyDirectionSelectItemList}" />    
</h:selectOneRadio>

The values of the radio button, which can be selected by the user was defined in this Java method:

public List<SelectItem> getJourneyDirectionSelectItemList() {
    final List<SelectItem> journeyDirectionSelectItemList = new ArrayList<SelectItem>();

    journeyDirectionSelectItemList.add(new SelectItem(Journey.BIDIRECTION,
            YES));
    journeyDirectionSelectItemList.add(new SelectItem(Journey.ONEWAY, NO));

    return journeyDirectionSelectItemList;
}

Now I have the problem, that the first value of the radio button, which was declared in the Java method above, will be selected automatically by load the JSF webpage. I have the requirement, that no values must be selected automatically by the system.

How can I solve this problem? Any ideas? I use JSF 1.2.

只要确保将您的journeyDir变量设置为null

Just try that code in yourController.java

@PostConstruct
public void postConstruct() {    
// ....    

// initialize journeyDir by setting its default value
// Note you must write ONLY one of next lines
refParam.journeyDir = null; // if you need to unchecked all choices
refParam.journeyDir = YES; // if you need to checked first choice
refParam.journeyDir = NO; // if you need to checked second choice  

// ....
}  

Note : you must take care that
There is no other code in your controller change its default value later
Until the screen page loaded for the first time

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM