Friday, September 16, 2011

JSF Trick of the Day: <h:selectOneRadio/> #{true} or "true"

There is a question about which method is more correct assigning the value of a <h:selectOneRadio/> to use #{true} which is an Expression Language (EL) boolean true value, or using "true" to represent to represent the boolean in String form.

Well it turns out either way is correct. JavaServerFaces has implicit conversion between boolean values and its String representation. It is my personal preference to use #{true} which I like better since it better represents the binding method used to connect the page to its backing bean.

I have attached a copy of the NetBeans project: SelectOneRadioBoolean.zip.

 The example below demonstrates using both methods.

index.xhtml


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core">
    <h:head>
        <title><SelectOneRadio/> Example</title>
    </h:head>
    <h:body>
        <h1>
 
<SelectOneRadio/> Example</h1>
<p>
We will use two examples to demonstrate how to pass boolean values back to the backing bean.
        </p>
<h:form>
            <h:messages id="messages1" showDetail="true"/>
            <h2>
 
Using Expression Language (EL)</h2>
<h:panelGrid columns="2">
                <h:outputLabel id="outputLabel1" value="Select Value:" for="selectOneRadio1"/>
                <h:selectOneRadio id="selectOneRadio1" value="#{indexBean.selection1}">
                    <f:selectItem itemLabel="true" itemValue="#{true}"/>
                    <f:selectItem itemLabel="false" itemValue="#{false}"/>
                </h:selectOneRadio>
                <h:commandButton id="commandButton1" value="Submit" action="#{indexBean.save()}"/>
                <h:commandButton id="commandButton2" type="reset" value="Reset"/>
            </h:panelGrid>           
            <h2>
 
Using "true" as a String</h2>
<h:panelGrid columns="2">
                <h:outputLabel id="outputLabel2" value="Select Value:" for="selectOneRadio2"/>
                <h:selectOneRadio id="selectOneRadio2" value="#{indexBean.selection2}">
                    <f:selectItem itemLabel="true" itemValue="true"/>
                    <f:selectItem itemLabel="false" itemValue="false"/>
                </h:selectOneRadio>
                <h:commandButton id="commandButton3" value="Submit" action="#{indexBean.save()}"/>
                <h:commandButton id="commandButton4" type="reset" value="Reset"/>
            </h:panelGrid>
        </h:form>
    </h:body>
</html>

IndexBean.java


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package com.bluelotussoftware.example.jsf.bean;
 
import javax.enterprise.context.RequestScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Named;
 
/*
 * Copyright 2011 John Yeary <jyeary@bluelotussoftware.com>.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/*
 * $Id: IndexBean.java 389 2011-09-16 23:10:21Z jyeary $
 */
/**
 *
 * @author John Yeary <jyeary@bluelotussoftware.com>
 * @version 1.0
 */
@Named
@RequestScoped
public class IndexBean {
 
    private boolean selection1;
    private boolean selection2;
 
    public IndexBean() {
    }
 
    public boolean isSelection1() {
        return selection1;
    }
 
    public void setSelection1(boolean selection1) {
        this.selection1 = selection1;
    }
 
    public boolean isSelection2() {
        return selection2;
    }
 
    public void setSelection2(boolean selection2) {
        this.selection2 = selection2;
    }
 
    public String save() {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        FacesMessage facesMessage = new FacesMessage();
        facesMessage.setSeverity(FacesMessage.SEVERITY_INFO);
        facesMessage.setSummary("Selections were set using radio buttons.");
        StringBuilder sb = new StringBuilder();
        sb.append("EL example was set to ").append(selection1);
        sb.append(", and String example was set to ").append(selection2);
        facesMessage.setDetail(sb.toString());
        facesContext.addMessage(null, facesMessage);
        return null;
    }
}

Popular Posts