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 比尔 · 詹姆斯.
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.
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 巨人 flaw in the EL.
我最终使用了“包装器”方法,下面是它的类;
public class CollectionWrapper {
Collection collection;
public CollectionWrapper(Collection collection) {
this.collection = collection;
}
public Collection getCollection() {
return collection;
}
public int getSize() {
return collection.size();
}
}