迭代JSP中的枚举常数 [英] iterating over Enum constants in JSP

查看:90
本文介绍了迭代JSP中的枚举常数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的枚举

  package com.example; 

public enum CoverageEnum {

COUNTRY,
REGIONAL,
COUNTY
}
/ pre>

我想在JSP中迭代这些常量,而不使用scriptlet代码。我知道我可以用这样的scriptlet代码:

 < c:forEach var =typeitems =< ;%= com.example.CoverageEnum.values()%>> 
$ {type}
< / c:forEach>

但是我可以在没有scriptlet的情况下实现同样的事情吗?



Cheers,
Don

解决方案

如果您使用Spring MVC,您可以通过以下语法祝福:

 < form:form method =postmodelAttribute =clustercssClass =formenctype = 多部分/格式数据 > 
< form:label path =clusterType>集群类型
< form:errors path =clusterTypecssClass =error/>
< / form:label>
< form:select items =$ {clusterTypes}var =typepath =clusterType/>
< / form:form>

您的模型属性(即,要填充的bean /数据实体)被命名为集群,您已经使用名为clusterTypes的枚举数组填充模型。 < form:error> 部分是非常可选的。



在Spring MVC版块中,您还可以将 clusterTypes 自动填充到您的模型中,如下

  @ModelAttribute(clusterTypes)
public MyClusterType [] populateClusterTypes(){
return MyClusterType.values();
}


I have an Enum like this

package com.example;

public enum CoverageEnum {

    COUNTRY,
    REGIONAL,
    COUNTY
}

I would like to iterate over these constants in JSP without using scriptlet code. I know I can do it with scriptlet code like this:

<c:forEach var="type" items="<%= com.example.CoverageEnum.values() %>">
    ${type}
</c:forEach>

But can I achieve the same thing without scriptlets?

Cheers, Don

解决方案

If you're using Spring MVC, you can accomplish your goal with the following syntactic blessing:

 <form:form method="post" modelAttribute="cluster" cssClass="form" enctype="multipart/form-data">
   <form:label path="clusterType">Cluster Type
      <form:errors path="clusterType" cssClass="error" />
   </form:label>
   <form:select items="${clusterTypes}" var="type" path="clusterType"/>
 </form:form>

where your model attribute (ie, bean/data entity to populate) is named cluster and you have already populated the model with an enum array of values named clusterTypes. The <form:error> part is very much optional.

In Spring MVC land, you can also auto-populate clusterTypes into your model like this

@ModelAttribute("clusterTypes")
public MyClusterType[] populateClusterTypes() {
    return MyClusterType.values();
}

这篇关于迭代JSP中的枚举常数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆