2011-05-02 42 views
0

我正在使用JSF 1.2框架。现在,我正在尝试执行文件上传过程,其中要上传的文件数由最终用户控制。请找到下面的快照和代码片段以供参考。多文件上传 - JSF

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"; 
} 

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

任何人都可以帮我吗?

回答

0

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

+0

升级到JSF2似乎是一个时间杀手的任务,因为我们面临很多的问题,配置:(。你肯定多文件上传是否可用于JSF 1.2? – 2011-05-02 08:01:58

+0

很确定,是的,虽然该组件使用Flash(在下一个Primefaces发行版中不会再有),但我不知道升级如何成为时间杀手任务,您不必更改配置(你可以使用注释,但不会强制) – ymajoros 2011-05-02 13:09:16

1

如果您创建了dinamically您的输入上传?在您的支持bean绑定属性

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

添加属性

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; 

} 
+0

“绑定”属性将你的控制器链接到jsf视图端代码,应该避免。基本思想相同:在托管bean中有一个值列表,从你的xhtml页面的ui:repeat中迭代。点击“添加”只是将一个项目添加到列表中。 – ymajoros 2011-05-03 07:53:36