多个文件上传 - JSF

时间:2011-05-02 06:51:20

标签: java jsf file-upload jsf-1.2

我正在使用JSF 1.2框架。现在我正在尝试实现文件上载过程,其中要上载的文件数由最终用户控制。请查看以下快照和代码段以供参考。

Multiple File Upload

XHTML实施: -

<a4j:commandLink action="#{importWSDLBean.xsdLoopIncrementAction}" reRender="WSDLPanelGrid">
 <h:graphicImage value="/images/plus_icon.gif" />
</a4j:commandLink>

<a4j:commandLink action="#{importWSDLBean.xsdLoopDecrementAction}" reRender="WSDLPanelGrid">
 <h:graphicImage value="/images/minus_icon.gif" />
</a4j:commandLink>

<h:panelGrid id="WSDLPanelGrid">
 <c:forEach items="#{importWSDLBean.acscDataList}" var="inputFUpload">
  <t:inputFileUpload id="#{inputFUpload.id}" value="#{inputFUpload.value}" />
 </c:forEach>
</h:panelGrid>

Java Bean实施: -

public String xsdLoopIncrementAction() {
    if (acscDataList == null) {
        acscDataList = new ACSCDataList(new ArrayList());
        HtmlInputFileUpload htmlUpload = new HtmlInputFileUpload();
        htmlUpload.setId("upload" + (acscDataList.size() + 1));
        acscDataList.add(htmlUpload);
    } else {
        HtmlInputFileUpload htmlUpload = new HtmlInputFileUpload();
        htmlUpload.setId("upload" + (acscDataList.size() + 1));
        acscDataList.add(htmlUpload);
    }
    return "success";
}


public String xsdLoopDecrementAction() {
    if (acscDataList != null) {
        if (acscDataList.size() > 0) {
            acscDataList.remove(acscDataList.size() - 1);
        }
    }
    return "success";
}

每当我增加或减少no时,此实现将重置文件上载值。文件上传字段。此外,当我提交表单时,我无法获取UploadedFile对象(文件上载先决条件,如表单类型和Web.xml配置也包括在内)。

任何人都可以帮助我吗?

2 个答案:

答案 0 :(得分:1)

如果你动态创建你的输入上传?具有绑定属性

<h:panelGrid binding="#{importWSDLBean.myPanelGrid}"></h:panelGrid>

在你的支持bean添加属性

private javax.faces.component.html.HtmlPanelGrid myPanelGrid;
/**
 * @return the myPanelGrid
 */
public javax.faces.component.html.HtmlPanelGrid getMyPanelGrid() {
    return myPanelGrid;
}



/**
 * @param myPanelGrid the myPanelGrid to set
 */
public void setMyPanelGrid(javax.faces.component.html.HtmlPanelGrid myPanelGrid) {
    this.myPanelGrid = myPanelGrid;
}
/*change for your value upload type*/
Map<String,Object> values = new LinkedHashMap<String, Object>();
public void addInputAction() {



    String key = "key"+values.size();
            values.put(key,"newValue");

    HtmlInputText input = new HtmlInputText();
 /*add input property (converter,css,etc?)*/
    input.setId("id_"+key);

    input.setValueExpression("value", createValueExpression(
            "#{WSDLPanelGrid.values['"+key+"']}", new Class[0], String.class));
/*add to panel grid your input*/
    myPanelGrid.getChildren().add(input);
}

  public static ValueExpression createValueExpression(String value,
        Class[] params, Class returnType) {
    FacesContext fctx = FacesContext.getCurrentInstance();
    ELContext elctx = fctx.getELContext();
    Application jsfApp = fctx.getApplication();
    ExpressionFactory exprFactory = jsfApp.getExpressionFactory();

    ValueExpression valueExpr = exprFactory.createValueExpression(elctx,
            value, returnType);

    return valueExpr;

}

答案 1 :(得分:0)

是否阻止您使用JSF 2? Primefaces(www.primefaces.org)有一个多文件上传组件。它适用于JSF 1.2,但仅针对JSF 2进行开发。