JSTL foreach 枚举

发布于 2024-09-28 10:55:30 字数 1689 浏览 3 评论 0原文

我有一个使用枚举类型在 java 中声明的常量列表,该列表必须出现在 jsp 中。 Java 枚举声明:

public class ConstanteADMD {


    public enum LIST_TYPE_AFFICHAGE {
        QSDF("qsmldfkj"), POUR("azeproui");

        private final String name;

        @Override
        public String toString() {
            return name;
        }

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

        public static List<String> getNames() {
            List<String> list = new ArrayList<String>();
            for (LIST_TYPE_AFFICHAGE test : LIST_TYPE_AFFICHAGE.values()) {
                list.add(test.toString());
            }
            return list;
        }
    }
}


<select name="typeAffichage" id="typeAffichage">
    <c:forEach var="type" items="${netcsss.outils.ConstanteADMD.LIST_TYPE_AFFICHAGE.names}">
        <option value="${type}">${type}</option>
    </c:forEach>
</select>

其中 as:

<select name="typeAffichage" id="typeAffichage">
    <c:choose>
        <c:when test="${catDecla ne null}">
            <option
                value="<%=catDecla.getCatDecla().getSTypeAffichage()%>"
                selected="selected"><%=catDecla.getCatDecla().getSTypeAffichage()%></option>
        </c:when>
    </c:choose> 
        <%List<String> list = ConstanteADMD.LIST_TYPE_AFFICHAGE.getNames();
                for(String test : list) {
            %>
        <option value="<%=test%>"><%=test%></option>
        <%}%>
</select>

工作正常。 foreach 循环中的枚举类型是否有限制?

I have a contant list declared in java using enum type, that must appears in a jsp.
Java enum declaration :

public class ConstanteADMD {


    public enum LIST_TYPE_AFFICHAGE {
        QSDF("qsmldfkj"), POUR("azeproui");

        private final String name;

        @Override
        public String toString() {
            return name;
        }

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

        public static List<String> getNames() {
            List<String> list = new ArrayList<String>();
            for (LIST_TYPE_AFFICHAGE test : LIST_TYPE_AFFICHAGE.values()) {
                list.add(test.toString());
            }
            return list;
        }
    }
}


<select name="typeAffichage" id="typeAffichage">
    <c:forEach var="type" items="${netcsss.outils.ConstanteADMD.LIST_TYPE_AFFICHAGE.names}">
        <option value="${type}">${type}</option>
    </c:forEach>
</select>

where as :

<select name="typeAffichage" id="typeAffichage">
    <c:choose>
        <c:when test="${catDecla ne null}">
            <option
                value="<%=catDecla.getCatDecla().getSTypeAffichage()%>"
                selected="selected"><%=catDecla.getCatDecla().getSTypeAffichage()%></option>
        </c:when>
    </c:choose> 
        <%List<String> list = ConstanteADMD.LIST_TYPE_AFFICHAGE.getNames();
                for(String test : list) {
            %>
        <option value="<%=test%>"><%=test%></option>
        <%}%>
</select>

Works fine.
Is there a limitation on enum types ni foreach loop?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

涙—继续流 2024-10-05 10:55:30

另一种选择是使用 标记,如下所示:

<c:set var="enumValues" value="<%=YourEnum.values()%>"/>

然后按如下方式迭代它:

<c:forEach items="${enumValues}" var="enumValue">
    ...
</c:forEach>

您的 IDE 应提示您导入 YourEnum 类。

Another option is to use a <c:set/> tag like such:

<c:set var="enumValues" value="<%=YourEnum.values()%>"/>

Then just iterate over it as such:

<c:forEach items="${enumValues}" var="enumValue">
    ...
</c:forEach>

Your IDE should prompt you to import the YourEnum class.

与风相奔跑 2024-10-05 10:55:30

另一种简单的方法可以是:

<c:forEach items="<%=LIST_TYPE_AFFICHAGE.values()%>" var="entry">
    <option>${entry.name }</option>
</c:forEach>

您需要导入这些:

<%@ taglib  uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@page import="packagename.LIST_TYPE_AFFICHAGE"%>

Another simple way can be:

<c:forEach items="<%=LIST_TYPE_AFFICHAGE.values()%>" var="entry">
    <option>${entry.name }</option>
</c:forEach>

You need to import these:

<%@ taglib  uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@page import="packagename.LIST_TYPE_AFFICHAGE"%>
瑾兮 2024-10-05 10:55:30

价值观方法工作正常,我的错误。
事实上,问题是我没有将列表放入 jsp 的页面范围中。

<%    pageContext.setAttribute("monEnum", ConstanteADMD.ListTypeAffichage.values()); %>

...
<c:forEach var="entry" items="${monEnum}">
    <option>${entry.type}</option>
</c:forEach>

不需要 getNames 方法

The values method works fine, my mistake.
Indeed the problem was that I didn't put my list in the page scope of my jsp.

<%    pageContext.setAttribute("monEnum", ConstanteADMD.ListTypeAffichage.values()); %>

...
<c:forEach var="entry" items="${monEnum}">
    <option>${entry.type}</option>
</c:forEach>

No need of the getNames method

梦屿孤独相伴 2024-10-05 10:55:30

如果您无法直接在 EL 表达式中使用值,您可以创建一个返回 Enum.values() 的方法。

从枚举中删除 getNames() 并在代码中的其他位置使用类似的方法。

public List<LIST_TYPE_AFFICHAGE> getNames() {
    return new ArrayList<LIST_TYPE_AFFICHAGE>(Arrays.asList(LIST_TYPE_AFFICHAGE.values()));
}

You can create a method that returns Enum.values() if you cannot use values directly in your EL expression.

Remove the getNames() from inside your Enum and use a method like this instead somewhere else in your code.

public List<LIST_TYPE_AFFICHAGE> getNames() {
    return new ArrayList<LIST_TYPE_AFFICHAGE>(Arrays.asList(LIST_TYPE_AFFICHAGE.values()));
}
下壹個目標 2024-10-05 10:55:30

您在 c:forEach 上的 items 属性中使用的 EL 尝试对您的枚举类型调用静态方法。我相信 EL 仅支持对实例方法的调用。

The EL you are using in the items attribute on c:forEach is trying to call a static method on your enum types. I believe that EL only supports calls on instance methods.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文