在开发中常常遇到可以列举的值:如订单状态,处理中(1)、已发货(2)、已完成(3)、已取消(4)。这种可列举的结果专业的做法是将其类型定义成枚举,下面我们就结合JSF的下拉组件将这种枚举类型加载到我们下位列表上。
定义订单状枚举类型 OrderStateEnum.java:
package com.isaiah.app.base;
/**
* @author HaijunLiu
* @emailw isaiah@sohu.com
*/
public enum OrderStateEnum {
READING(1, "已发货"),
SHIPPED(2, "已发货"),
RECEIVED(3, "已签收"),
CANCELLED(4, "已取消");
private int status;
private String label;
private OrderStateEnum(int status, String label) {
this.status = status;
this.label = label;
}
public int getStatus() {
return status;
}
public String getLabel() {
return label;
}
}
定义托管Bean OrderBean.java
package com.isaiah.app.web.bean;
import com.isaiah.app.base.OrderStateEnum;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
/**
* @author HaijunLiu
* @emailw isaiah@sohu.com
*/
@ManagedBean
@RequestScoped
public class OrderBean {
private String orderCode;
private OrderStateEnum orderState;
/**
* 导航处理方法
* @return
*/
public String addOrder(){
// 调用业务层添加订单
// ……
return "success";
}
/**
* 获取所有订单状态
* @return
*/
public OrderStateEnum[] getAllOrderStateEnum(){
return OrderStateEnum.values();
}
public String getOrderCode() {
return orderCode;
}
public void setOrderCode(String orderCode) {
this.orderCode = orderCode;
}
public OrderStateEnum getOrderState() {
return orderState;
}
public void setOrderState(OrderStateEnum orderState) {
this.orderState = orderState;
}
}
开发添加订单页面:addOrder.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://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<h:head>
<title>生成订单</title>
</h:head>
<h:body>
<h:form>
订单号:<h:inputText value="#{orderBean.orderCode}"/><br/>
订单状态:<h:selectOneMenu value="#{orderBean.orderState}">
<f:selectItems value="${orderBean.allOrderStateEnum}" var="state" itemLabel="#{state.label}" itemValue="#{s}"/>
</h:selectOneMenu><br/>
<h:commandButton value="添加" action="#{orderBean.addOrder}"/>
</h:form>
</h:body>
</html>
开发查看新增订单页面:listOrder.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://xmlns.jcp.org/jsf/html">
<h:head>
<title>查看新加订单</title>
</h:head>
<h:body>
订单号:#{orderBean.orderCode}<br/>
订单状态:#{orderBean.orderState.label}<br/>
</h:body>
</html>
在JSF配置文件中定义导航规则:faces-config.xhml
<?xml version='1.0' encoding='UTF-8'?>
<faces-config version="2.2"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd">
</converter>
<navigation-rule>
<from-view-id>/addOrder.xhtml</from-view-id>
<navigation-case>
<from-outcome>success</from-outcome>
<to-view-id>/listOrder.xhtml</to-view-id>
</navigation-case>
</navigation-rule>
</faces-config>
最终运行结果:
录入表单数据,点击【添加】:
以上例子参看:http://stackoverflow.com/questions/8229638/how-to-make-a-dropdown-menu-of-a-enum-in-jsf