使用form:options引入枚举类型数据构建下拉选

本文介绍了如何在JSP页面中利用枚举类型BusinessTypeEnum和TestTypeEnum来构建下拉选择框。关键在于在页面顶部导入枚举类,并在select标签中指定实体类属性,option标签中通过items遍历枚举数据,itemLabel映射枚举的name,itemValue可选枚举的index。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Enum类(name,index)

public enum BusinessTypeEnum {
	
	gnmy(0,"国内贸易"),
	dwmy(1,"对外贸易"),
	wstz(2,"外商投资"),
	jjhz(3,"经济合作"),
	zhyw(4,"综合业务"),
	qt(5,"其他");
	

	private BusinessTypeEnum(int index,String name) {
		this.index = index;
		this.name = name;
	}
	
	private int index;
	private String name;

	public int getIndex() {
		return index;
	}
	public String getName() {
		return name;
	}
}

重点:由于jsp页面中使用了  BusinessTypeEnum  枚举类,因此必须在该页面顶部引入  BusinessTypeEnum 类,否则下拉框中无法遍历枚举类!!!!!

<%@ page import="org.ciecc.xypt.datarecord.domain.BusinessTypeEnum " %>

JSP页面

<form:select path="businessType" cssClass="u-sel f-w200" >
    <option value="" selected="true">全部</option>
    <form:options items="${BusinessTypeEnum.values()}" itemLabel="name" itemValue="index"/>
</form:select>

说明:select   内  path中表示实体类对应的属性

           option   内  items中传入枚举类的所有数据

                              itemLabel-----对应    Enum中  name

                               itemValue----对应    Enum中  index  (枚举类中可不定义index,使用自带的,此处itemValue则可省略)

--------------------------------------------------------------------------------------------------------------------------------------------

                                                        枚举类中不设置index的写法

--------------------------------------------------------------------------------------------------------------------------------------------

public enum TestTypeEnum {
	testone("测试1"),
	testtwo("测试2"),
	testthree("测试3")
	;

	private TestTypeEnum (String name) {
		this.name = name;
	}
	
	private String name;

	public String getName() {
		return name;
	}

 

重点:同理该页面必引入

<%@ page import="org.ciecc.xypt.datarecord.domain.TestTypeEnum" %>

<form:select path="test" cssClass="u-sel f-w200" >
    <option value="" select="true">--请选择--</option>
    <form:options items="${TestTypeEnum.values()}" itemLabel="name"/>
</form:select>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值