Java 在JSF表达式语言中如何获取列表的长度?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/206161/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 11:21:55  来源:igfitidea点击:

How do you get the length of a list in the JSF expression language?

javajspjsfjstlel

提问by GBa

How would I get the length of an ArrayListusing a JSF EL expression?

我将如何ArrayList使用 JSF EL 表达式获得长度?

#{MyBean.somelist.length}

does not work.

不起作用。

采纳答案by billjamesdev

Yes, since some genius in the Java API creation committee decided that, even though certain classes have size()members or lengthattributes, they won't implement getSize()or getLength()which JSF and most other standards require, you can't do what you want.

是的,因为 Java API 创建委员会中的一些天才决定,即使某些类具有size()成员或length属性,它们也不会实现getSize()getLength()JSF 和大多数其他标准要求,您不能做您想做的事。

There's a couple ways to do this.

有几种方法可以做到这一点。

One: add a function to your Bean that returns the length:

一:向您的 Bean 添加一个返回长度的函数:

In class MyBean:
public int getSomelistLength() { return this.somelist.length; }

In your JSF page:
#{MyBean.somelistLength}

Two: If you're using Facelets (Oh, God, why aren't you using Facelets!), you can add the fn namespace and use the length function

二:如果你使用的是 Facelets(哦,上帝,你为什么不使用 Facelets!),你可以添加 fn 命名空间并使用 length 函数

In JSF page:
#{ fn:length(MyBean.somelist) }

回答by Damo

You mean size() don't you?

你的意思是 size() 不是吗?

#{MyBean.somelist.size()}

works for me (using JBoss Seam which has the Jboss EL extensions)

对我有用(使用具有 Jboss EL 扩展的 JBoss Seam)

回答by Romain Linsolas

You can eventually extend the EL language by using the EL Functor, which will allow you to call any Java beans methods, even with parameters...

您最终可以通过使用EL Functor来扩展 EL 语言,这将允许您调用任何 Java bean 方法,即使使用参数...

回答by James McMahon

Note:This solution is better for older versions of JSTL. For versions greater then 1.1 I recommend using fn:length(MyBean.somelist)as suggested by Bill James.

注意:此解决方案更适合旧版本的 JSTL。对于大于 1.1 的版本,我建议fn:length(MyBean.somelist)按照Bill James 的建议使用。



This articlehas some more detailed information, including another possible solution;

这篇文章有一些更详细的信息,包括另一种可能的解决方案;

The problem is that we are trying to invoke the list's size method (which is a valid LinkedList method), but it's not a JavaBeans-compliant getter method, so the expression list.size-1 cannot be evaluated.

There are two ways to address this dilemma. First, you can use the RT Core library, like this:

问题是我们试图调用列表的 size 方法(这是一个有效的 LinkedList 方法),但它不是 JavaBeans 兼容的 getter 方法,因此无法计算表达式 list.size-1。

有两种方法可以解决这个难题。首先,您可以使用 RT Core 库,如下所示:

<c_rt:out value='<%= list[list.size()-1] %>'/>

Second, if you want to avoid Java code in your JSP pages, you can implement a simple wrapper class that contains a list and provides access to the list's size property with a JavaBeans-compliant getter method. That bean is listed in Listing 2.25.

其次,如果您想避免在 JSP 页面中使用 Java 代码,您可以实现一个简单的包装类,该类包含一个列表,并使用符合 JavaBeans 的 getter 方法提供对列表 size 属性的访问。该 bean 列在清单 2.25 中。

The problem with c_rt method is that you need to get the variable from request manually, because it doesn't recognize it otherwise. At this point you are putting in a lot of code for what should be built in functionality. This is a GIANTflaw in the EL.

c_rt 方法的问题在于您需要手动从请求中获取变量,否则它无法识别它。在这一点上,您正在为应该内置的功能输入大量代码。这是EL中的一个巨大缺陷。

I ended up using the "wrapper" method, here is the class for it;

我最终使用了“包装器”方法,这是它的类;

public class CollectionWrapper {

    Collection collection;

    public CollectionWrapper(Collection collection) {
        this.collection = collection;
    }

    public Collection getCollection() {
        return collection;
    }

    public int getSize() {
        return collection.size();
    }
}

A third option that no one has mentioned yet is to put your list size into the model (assuming you are using MVC) as a separate attribute. So in your model you would have "someList" and then "someListSize". That may be simplest way to solve this issue.

没有人提到的第三个选项是将您的列表大小作为单独的属性放入模型中(假设您使用的是 MVC)。所以在你的模型中你会有“someList”然后是“someListSize”。这可能是解决此问题的最简单方法。

回答by UdayKiran Pulipati

<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>

<h:outputText value="Table Size = #{fn:length(SystemBean.list)}"/>

On screen it displays the Table size

在屏幕上显示表格大小

Example: Table Size = 5

例子: Table Size = 5

回答by Soujanya

You can get the length using the following EL:

您可以使用以下 EL 获得长度:

#{Bean.list.size()}

#{Bean.list.size()}

回答by Arry

After 7 years... the facelets solution still works fine for me as a jsf user

7 年后……作为 jsf 用户,facelets 解决方案仍然适用于我

include the namespace as xmlns:fn="http://java.sun.com/jsp/jstl/functions"

将命名空间包含为 xmlns:fn="http://java.sun.com/jsp/jstl/functions"

and use the EL as #{fn:length(myBean.someList)}for example if using in jsf ui:fragment below example works fine

并使用 EL #{fn:length(myBean.someList)}例如,如果在 jsf ui:fragment 下面的示例中使用工作正常

<ui:fragment rendered="#{fn:length(myBean.someList) gt 0}">
    <!-- Do something here-->
</ui:fragment>