JSF列表框
精华
小牛编辑
152浏览
2023-03-14
以下部分介绍如何在JSF中创建ListBox(JSF表列表框)。<h:selectOneListbox>
标签呈现指定大小的“select
”类型的HTML输入元素。
以下JSF代码 -
<h:selectOneListbox value="#{userData.data}">
<f:selectItem itemValue="1" itemLabel="Item 1" />
<f:selectItem itemValue="2" itemLabel="Item 2" />
</h:selectOneListbox>
被渲染成以下HTML标签-
<select name="j_idt6:j_idt8" size="2">
<option value="1">Item 1</option>
<option value="2">Item 2</option>
</select>
硬编码实例表列表框
以下是文件:UserBean.java 中的代码 -
package com.yiibai.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 item;
public String getItem() {
return item;
}
public void setItem(String i) {
this.item = i;
}
}
以下是文件:index.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:selectOneListbox value="#{user.item}">
<f:selectItem itemValue="1" itemLabel="item 1" />
<f:selectItem itemValue="2" itemLabel="item 2" />
<f:selectItem itemValue="3" itemLabel="item 3" />
</h:selectOneListbox>
<br /><br />
<h:commandButton value="Submit" action="result" />
<h:commandButton value="Reset" type="reset" />
</h:form>
</h:body>
</html>
以下是文件: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.item}</p>
</h:body>
</html>
由映射生成的列表框
以下是文件: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.item}</p>
</h:body>
</html>
以下是文件:UserBean.java 中的代码 -
package com.yiibai;
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 item;
public String getItem() {
return item;
}
public void setItem(String i) {
this.item = i;
}
//Generated by Map
private static Map<String,Object> itemValue;
static{
itemValue = new LinkedHashMap<String,Object>();
itemValue.put("Item 1", "1"); //label, value
itemValue.put("Item 2", "2");
itemValue.put("Item 3", "3");
}
public Map<String,Object> getItemValue() {
return itemValue;
}
}
以下是文件:index.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:selectOneListbox value="#{user.item}">
<f:selectItems value="#{user.itemValue}" />
</h:selectOneListbox>
<br /><br />
<h:commandButton value="Submit" action="result" />
<h:commandButton value="Reset" type="reset" />
</h:form>
</h:body>
</html>
内部类istBox实现列表框
以下是文件:UserBean.java 中的代码 -
package com.yiibai;
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 item;
public String getItem() {
return item;
}
public void setItem(String i) {
this.item = i;
}
public static class Item{
public String label;
public String value;
public Item(String l, String v){
this.label = l;
this.value = v;
}
public String getLabel(){
return label;
}
public String getValue(){
return value;
}
}
public Item[] itemList;
public Item[] getItemValue() {
itemList = new Item[3];
itemList[0] = new Item("item - 1", "1");
itemList[1] = new Item("item - 2", "2");
itemList[2] = new Item("item - 3", "3");
return itemList;
}
}
以下是文件: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.item}</p>
</h:body>
</html>
以下是文件:index.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:selectOneListbox value="#{user.item}">
<f:selectItems value="#{user.itemValue}" var="y"
itemLabel="#{y.label}" itemValue="#{y.value}" />
</h:selectOneListbox>
<br /><br />
<h:commandButton value="Submit" action="result" />
<h:commandButton value="Reset" type="reset" />
</h:form>
</h:body>
</html>