Thursday, November 16, 2017

JSF 2.3 Tip of the Day: Single Select Menu Item Example

One of my new colleagues is new to JSF development, and asked for some assistance in creating a JSF <h:selectOneMenu/> which had a blank option. I came up with a quick example using JSF 2.3 and CDI on GlassFish 5.0.

The web page (index.xhtml) looks like the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?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://xmlns.jcp.org/jsf/html"
      xmlns:f="http://xmlns.jcp.org/jsf/core">
    <h:head>
        <title>h:selectOneMenu Example</title>
    </h:head>
    <h:body>
        <h:form>
            <h:selectOneMenu value="#{indexBean.selected}" >
                <f:selectItems value="#{indexBean.items}"/>
                <f:ajax event="change" render="@form"/>
            </h:selectOneMenu>
             
 
 
            <h:outputText value="#{indexBean.selected ne null ? indexBean.selected : 'None'}"/>
        </h:form>
    </h:body>
</html>
The backing bean (IndexBean.java) contains the list of items for the menu and the logic to hold the selected value:

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
package com.bluelotussoftware.jsf;
 
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.faces.model.SelectItem;
import javax.faces.view.ViewScoped;
import javax.inject.Named;
 
/**
 *
 * @author John Yeary <jyeary@bluelotussoftware.com>
 * @version 1.0.0
 */
@Named
@ViewScoped
public class IndexBean implements Serializable {
 
    private static final long serialVersionUID = -8409333287484097050L;
 
    private String selected;
 
    public IndexBean() {
    }
 
    public List<selectitem> getItems() {
        List<selectitem> items = new ArrayList<>();
        items.add(new SelectItem("", ""));
        items.add(new SelectItem("Tuna", "Tuna"));
        items.add(new SelectItem("Catfish", "Catfish"));
        items.add(new SelectItem("Gold Fish", "Gold Fish"));
        items.add(new SelectItem("Piranha", "Piranha"));
        return items;
    }
 
    public String getSelected() {
        return selected;
    }
 
    public void setSelected(String selected) {
        this.selected = selected;
    }
 
}

All in all a very simple example. I was asked what my dependencies looked like so here is the one last piece.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<dependencies>
    <dependency>
        <groupid>javax</groupId>
        <artifactid>javaee-web-api</artifactId>
        <version>8.0</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupid>javax.faces</groupId>
        <artifactid>javax.faces-api</artifactId>
        <version>2.3</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupid>org.glassfish</groupId>
        <artifactid>javax.faces</artifactId>
        <version>2.3.0</version>
        <scope>provided</scope>
    </dependency>
</dependencies>

The NetBeans Maven project can be downloaded here: jsf23-cdi-selectonemenu-example

0 comments :

Popular Posts