I've managed to do what I was looking for, this time using some buttons with real actions to redirect to pages.
What I did was the following:
MyBackingBean
has now a method to determine whether or not the selection is empty;- It also has a method to determine if the selection is valid (this is: not empty and without any invalid item in it);
- There is an
<ice:commandLink>
rendered when the selection is invalid. This commandLink has the actual redirection to the error page. - The
<ice:outputResource>
will be rendered when the<ice:commandLink>
is not rendered (i.e.: when the selection is completely valid).
And now, the code:
First, the commandLink:
<ice:commandLink rendered="#{not myBackingBean.validSelection}"
disabled="#{myBackingBean.emptySelection}"
label="download excel report}"
action="redirect_to_error_page" />
Remember:
MyBackingBean.isEmptySelection()
returns true if there is no item selected.MyBackingBean.isValidSelection()
returns true if there is at least one item selected and also each and every selected item is a valid item (this is: an item that is valid for export)
Now, the outputResource:
<ice:outputResource
rendered="#{myBackingBean.validSelection}"
resource="#{myBackingBean.excelResource}"
label="download excel report}" shared="false" target="_self" />
Last but not least, you may have figured out the fact that the <ice:outputResource>
will now handle only valid selection (the actual redirection to the error page is being done by the <ice:commandLink>
). This means there has to be a way to filter the items before passing them to the resource for the actual export. Well, in my case I decided to create a filter(...)
method in the backing bean.
@ManagedBean(name = "myBackingBean")
@ViewScoped
@WindowDisposed
public class MyBackingBean implements Serializable
{
...
private ExcelResource resource;
...
public List<MyItems> getFilteredList(List<MyItems> allSelectedItems)
{
...
//do some selection here and return a list containing only valid items
return validItemsList;
}
public ExcelResource getExcelResource()
{
return new ExcelResource(getFilteredList(allSelectedItems));
}
public boolean isEmptySelection()
{
//return true if the selection is EMPTY, false otherwise.
}
public boolean isValidSelection()
{
//return true if the selection is NOT EMPTY and it has
//only VALID items in it, false otherwise.
}
}
This way you can generate the ExcelResource
"on the fly" with nothing but valid items in it. By the time the <ice:outputResource>
is being rendered in the XHTML, it will contain only valid and exportable items!.
I hope someone will find this useful :)