0

我正在尝试从一些项目中生成一个导出文件,这些项目应该符合一些“标准”以便能够被导出。问题是:用户应该选择一些项目(通过使用复选框),然后单击 ICEFaces 的OutputResource以导出(希望是全部)所选项目。

该过程涉及的部分如下:

XHTML 中的 OutputResource:

<ice:outputResource rendered="#{not myBackingBean.emptySelection}" resource="#{myBackingBean.excelResource}" label="export to Excel" shared="false" target="_self" />

持有资源的支持 bean:

@ManagedBean(name = "myBackingBean")
@ViewScoped
@WindowDisposed
public class MyBackingBean implements Serializable
{
 ...
 private ExcelResource resource;
 ...
}

最后,实际资源:

...
import com.icesoft.faces.context.Resource;
...

public class ExcelResource implements Resource
{
  ...

  @Override
  public InputStream open() throws IOException
  {
    //do some selection here. If there is no valid ticket to export then
    //this method will return null, otherwise it will return an InputStream
    //and everything will work properly

    if (everythingOk)
    {
        return new ByteArrayInputStream(...);
    }

    //hopefully, it won't get to this point
    return null;
  }

如您所见,我正在实现com.icesoft.faces.context.Resource接口并覆盖open()“即时”创建 Excel 导出的方法。

现在,再一次,我想要做的是过滤一些最初选择的项目,如果没有留下任何项目,请导航到某个错误页面。如果这是 anh:commandButton或 anice:commandLink那么我将使用action属性来执行此操作,但我不能在这里执行此操作,因为这是一个ice:outputResource. 有一些解决方法吗?请注意,仅使用render属性是不够的,因为用户可以选择某些内容(这将立即呈现ice:outputResource),但是在导出之前应该过滤选择。

最后但同样重要的是:我正在使用 Websphere 8 和 ICEFaces 3 来执行此操作。

提前致谢!

4

1 回答 1

0

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 :)

于 2013-10-15T06:31:56.447 回答