The following sections show how to create HTML combobox from JSF.
The h:selectOneMenu tag renders an HTML input element of the type "select" with size not specified.
The following JSF tags
<h:selectOneMenu value="#{userData.data}"> <f:selectItem itemValue="1" itemLabel="Item 1" /> <f:selectItem itemValue="2" itemLabel="Item 2" /> </h:selectOneMenu>
are rendered into the following HTML code.
<select name="j_idt6:j_idt8"> <option value="1">Item 1</option> <option value="2">Item 2</option> </select>
Attribute | Description |
---|---|
id | id for the tag |
binding | Reference to the component used in a backing bean |
rendered | A boolean value; false would suppress rendering |
styleClass | Cascading stylesheet (CSS) class name |
value | value binding |
valueChangeListener | A method binding that responds to value changes |
converter | Converter class name |
validator | Class name of a validator attached to the component |
required | A boolean; if true, marks the tag as required |
accesskey | gives focus to an element |
accept | Comma-separated list of content types for a form |
accept-charset | Comma- or space-separated list of character encodings for a form. |
alt | Alternative text for nontextual elements such as images |
border | Pixel value for an element's border width |
charset | Character encoding for a linked resource |
coords | Coordinates for an element whose shape is a rectangle, circle, or polygon |
dir | Direction for text. Valid values are ltr (left to right) and rtl (right to left). |
disabled | Disabled state of an input element or button |
hreflang | Base language of a resource specified with the href attribute; |
lang | Base language of an element's attributes and text |
maxlength | Maximum number of characters for text fields |
readonly | Read-only state of an input field |
style | Inline style information |
tabindex | Numerical value specifying a tab index |
target | The name of a frame in which a document is opened |
title | A title used for accessibility. Browsers typically create tooltips for the title's value |
type | Type of a link; for example, stylesheet |
width | Width of an element |
onblur | Event handler for losing focus |
onchange | Event handler for value changes |
onclick | Event handler for Mouse button clicked over the element |
ondblclick | Event handler for Mouse button double-clicked |
onfocus | Event handler for element received focus |
onkeydown | Event handler for Key pressed |
onkeypress | Event handler for Key pressed and released |
onkeyup | Event handler for Key released |
onmousedown | Event handler for Mouse button pressed |
onmousemove | Event handler for mouse moved |
onmouseout | Event handler for mouse left |
onmouseover | Event handler for mouse moved onto |
onmouseup | Event handler for mouse button released |
onreset | Event handler for form reset |
onselect | Event handler for Text selected |
immediate | Process validation early in the life cycle |
The following code is from demo.xhtml.
<?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"> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core"> <h:body> <h:form> Hard-coded with "f:selectItem" : <h:selectOneMenu value="#{user.favItem}"> <f:selectItem itemValue="A" itemLabel="Coffee A" /> <f:selectItem itemValue="B" itemLabel="Coffee B" /> <f:selectItem itemValue="C" itemLabel="Coffee C" /> </h:selectOneMenu> <br /><br /> <h:commandButton value="Submit" action="result" /> <h:commandButton value="Reset" type="reset" /> </h:form> </h:body> </html>
The following code is from result.xhtml.
<?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"> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html"> <h:body> <p>user.favItem : #{user.favItem}</p> </h:body> </html>
The following code is from UserBean.java.
package com.java2s.common; import java.io.Serializable; import java.util.LinkedHashMap; import java.util.Map; import javax.faces.bean.ManagedBean; import javax.faces.bean.SessionScoped; @ManagedBean(name="user") @SessionScoped public class UserBean implements Serializable{ public String favItem = "Black"; public String getFavItem() { return favItem; } public void setFavItem(String f) { this.favItem = f; } }
The following code is from UserBean.java.
package com.java2s.common; import java.io.Serializable; import java.util.LinkedHashMap; import java.util.Map; import javax.faces.bean.ManagedBean; import javax.faces.bean.SessionScoped; @ManagedBean(name="user") @SessionScoped public class UserBean implements Serializable{ public String favItem; public String getFavItem() { return favItem; } public void setFavItem(String favItem) { this.favItem = favItem; } //Generated by Map private static Map<String,Object> value; static{ value = new LinkedHashMap<String,Object>(); value.put("Item A", "A"); //label, value value.put("Item B", "B"); value.put("Item C", "C"); } public Map<String,Object> getFavItemValue() { return value; } }
The following code is from demo.xhtml.
<?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"> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core"> <h:body> <h:form> Generated by Map : <h:selectOneMenu value="#{user.favItem}"> <f:selectItems value="#{user.favItemValue}" /> </h:selectOneMenu> <br /><br /> <h:commandButton value="Submit" action="result" /> <h:commandButton value="Reset" type="reset" /> </h:form> </h:body> </html>
The following code is from result.xhtml.
<?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"> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html"> <h:body> <p>User Selected : #{user.favItem}</p> </h:body> </html>
The following code is from demo.xhtml.
<?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"> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core"> <h:body> <h:form> Generated by Object array and iterate with var : <h:selectOneMenu value="#{user.favItem}"> <f:selectItems value="#{user.itemValue}" var="c" itemLabel="#{c.itemLabel}" itemValue="#{c.itemValue}" /> </h:selectOneMenu> <br /><br /> <h:commandButton value="Submit" action="result" /> <h:commandButton value="Reset" type="reset" /> </h:form> </h:body> </html>
The following code is from result.xhtml.
<?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"> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html"> <h:body> <p>Selected: #{user.favItem}</p> </h:body> </html>
The following code is from UserBean.java.
package com.java2s.common; import java.io.Serializable; import java.util.LinkedHashMap; import java.util.Map; import javax.faces.bean.ManagedBean; import javax.faces.bean.SessionScoped; @ManagedBean(name="user") @SessionScoped public class UserBean implements Serializable{ public String favItem; public String getFavItem() { return favItem; } public void setFavItem(String f) { this.favItem = f; } //Generated by Object array public static class Item{ public String itemLabel; public String itemValue; public Item(String l, String v){ this.itemLabel = l; this.itemValue = v; } public String getItemLabel(){ return itemLabel; } public String getItemValue(){ return itemValue; } } public Item[] itemList; public Item[] getItemValue() { itemList = new Item[3]; itemList[0] = new Item("Item A", "A"); itemList[1] = new Item("Item B", "B"); itemList[2] = new Item("Item C", "C"); return itemList; } }
Copy the generated WAR file from the target folder to Tomcat deployment folder and run Tomcat-Install-folder/bin/startup.bat.
After Tomcat finish starting, type the following URL in the browser address bar.
http://localhost:8080/simple-webapp/demo.xhtml