Output the size of a list

I am trying to output the size of a list onto my page using JSF and EL. E.g., I have a managed bean, say myBean, which has a property, say myList. I would like to do the following:

<h:outputText value="#{myBean.myList.size}" />

Of course this does not work, since there is no getSize method on java.util.List. The EL interpreter tries to coerce size into an index and I get a NumberFormatException.

Any ideas?

FYI, I am using facelets but I do not think that matters in this case.

Thanks,

Ray

[567 byte] By [RaymondDeCampoa] at [2007-11-26 20:00:36]
# 1

I've subclassed an ArrayList into a DtoList which provides object-sorting functionality and which also implements a method "public int getSize() { return super.size(); }". Useful for JSF =)

I'll be so kind to share the source:

package net.balusc.datareflector.dto;

import java.util.ArrayList;

import java.util.Collection;

import java.util.Collections;

import java.util.LinkedHashSet;

import java.util.Set;

/**

* Extended ArrayList to hold data transfer objects (DTO's).

*

* @author balusc@xs4all.nl

*/

public class DtoList<Dto extends BaseDto> extends ArrayList<Dto> {

// Init

private String sortField;

private boolean sortAscending;

// Constructor --

/**

* Default constructor.

*/

public DtoList() {

// Keep it alive.

}

/**

* Create new DtoList based on the given Collection.

* @param collection The given Collection.

*/

public DtoList(Collection<Dto> collection) {

super(collection);

}

// Actions

/**

* Override ArrayList#add(), null DTO's are not allowed.

* @see java.util.ArrayList#add(java.lang.Object)

*/

public boolean add(Dto dto) {

if (dto != null) {

return super.add(dto);

}

return false;

}

/**

* Sort this list.

*/

public void sort() {

if (sortField != null) {

Collections.sort(this, new DtoComparator<BaseDto>(this));

} else {

Collections.sort(this);

}

}

/**

* Sort this list.

* @param sortField The The sort field..

*/

public void sort(String sortField) {

setSortField(sortField);

sort();

}

/**

* Sort this list.

* @param sortField The sort field.

* @param sortAscending Sort ascending?

*/

public void sort(String sortField, boolean sortAscending) {

setSortField(sortField, sortAscending);

sort();

}

/**

* Filter duplicates.

*/

public void unique() {

Set<Dto> set = new LinkedHashSet<Dto>(this);

super.clear();

super.addAll(set);

}

// Dto getters --

/**

* Shifts first DTO of this list.

* @return First DTO of list.

*/

public Dto getFirst() {

if (super.size() == 0) {

return null;

}

return super.remove(0);

}

/**

* Pops last DTO of this list.

* @return Last DTO of this list.

*/

public Dto getLast() {

if (super.size() == 0) {

return null;

}

return super.remove(size() - 1);

}

/**

* Useful for JSF. For example #{myBean.myDtoList.size} will be possible.

* @return The list size

*/

public int getSize() {

return super.size();

}

// Sorting getters -

/**

* @return The sort field.

*/

public String getSortField() {

return sortField;

}

/**

* @return Sort ascending?

*/

public boolean isSortAscending() {

return sortAscending;

}

/**

* @return Sort descending?

*/

public boolean isSortDescending() {

return !sortAscending;

}

// Sorting setters -

/**

* @param sortField The sort field.

*/

public void setSortField(String sortField) {

if (sortField != null && sortField.equals(this.sortField)) {

sortAscending = !sortAscending; // Reverse sort.

} else {

this.sortField = sortField; // New field.

sortAscending = true; // Set default order.

}

}

/**

* @param sortField The sort field.

* @param sortAscending Sort ascending?

*/

public void setSortField(String sortField, boolean sortAscending) {

this.sortField = sortField;

this.sortAscending = sortAscending;

}

}

BalusCa at 2007-7-9 22:58:19 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...