How would I get the length of an ArrayList
using a JSF EL expression?
如何使用JSF EL表达式获取ArrayList的长度?
#{MyBean.somelist.length}
does not work.
不起作用。
7 个解决方案
#1
155
Yes, since some genius in the Java API creation committee decided that, even though certain classes have size()
members or length
attributes, they won't implement getSize()
or getLength()
which JSF and most other standards require, you can't do what you want.
是的,因为Java API创建委员会中的一些天才决定,即使某些类具有size()成员或长度属性,它们也不会实现JSF和大多数其他标准所要求的getSize()或getLength(),你可以'做你想做的。
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命名空间并使用长度函数
In JSF page: #{ fn:length(MyBean.somelist) }
#2
52
You mean size() don't you?
你的意思是尺寸()不是吗?
#{MyBean.somelist.size()}
works for me (using JBoss Seam which has the Jboss EL extensions)
适合我(使用JBoss Seam,它有Jboss EL扩展)
#3
14
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)。
This article has 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.
问题是我们正在尝试调用列表的大小方法(这是一个有效的LinkedList方法),但它不是符合JavaBeans的getter方法,因此无法计算表达式list.size-1。
There are two ways to address this dilemma. First, you can use the RT Core library, like this:
有两种方法可以解决这个难题。首先,您可以使用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方法提供对列表大小属性的访问。该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 GIANT flaw in the EL.
c_rt方法的问题在于您需要手动从请求中获取变量,否则它无法识别它。此时,您将为应该在功能中构建的内容添加大量代码。这是EL中的GIANT缺陷。
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”。这可能是解决此问题的最简单方法。
#4
6
<%@ 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
示例:表大小= 5
#5
0
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方法,即使使用参数...
#6
-2
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:下面的片段示例工作正常
<ui:fragment rendered="#{fn:length(myBean.someList) gt 0}">
<!-- Do something here-->
</ui:fragment>
#7
-4
You can get the length using the following EL:
您可以使用以下EL获取长度:
#{Bean.list.size()}
#{Bean.list.size()}
#1
155
Yes, since some genius in the Java API creation committee decided that, even though certain classes have size()
members or length
attributes, they won't implement getSize()
or getLength()
which JSF and most other standards require, you can't do what you want.
是的,因为Java API创建委员会中的一些天才决定,即使某些类具有size()成员或长度属性,它们也不会实现JSF和大多数其他标准所要求的getSize()或getLength(),你可以'做你想做的。
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命名空间并使用长度函数
In JSF page: #{ fn:length(MyBean.somelist) }
#2
52
You mean size() don't you?
你的意思是尺寸()不是吗?
#{MyBean.somelist.size()}
works for me (using JBoss Seam which has the Jboss EL extensions)
适合我(使用JBoss Seam,它有Jboss EL扩展)
#3
14
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)。
This article has 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.
问题是我们正在尝试调用列表的大小方法(这是一个有效的LinkedList方法),但它不是符合JavaBeans的getter方法,因此无法计算表达式list.size-1。
There are two ways to address this dilemma. First, you can use the RT Core library, like this:
有两种方法可以解决这个难题。首先,您可以使用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方法提供对列表大小属性的访问。该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 GIANT flaw in the EL.
c_rt方法的问题在于您需要手动从请求中获取变量,否则它无法识别它。此时,您将为应该在功能中构建的内容添加大量代码。这是EL中的GIANT缺陷。
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”。这可能是解决此问题的最简单方法。
#4
6
<%@ 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
示例:表大小= 5
#5
0
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方法,即使使用参数...
#6
-2
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:下面的片段示例工作正常
<ui:fragment rendered="#{fn:length(myBean.someList) gt 0}">
<!-- Do something here-->
</ui:fragment>
#7
-4
You can get the length using the following EL:
您可以使用以下EL获取长度:
#{Bean.list.size()}
#{Bean.list.size()}