R K
R K

Reputation: 1313

Multiple File Uploads - JSF

I am using JSF 1.2 framework. Right now i am trying to implement a file upload process in which the number of files to be uploaded is controlled by the end user. Please find the below snapshot and code snippet for reference.

Multiple File Upload

XHTML Implementation:-

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

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

This implementation resets the file upload values whenever i increment or decrement the no. of file upload fields. Also when i submit the form i cant able to get the UploadedFile object (File Upload prerequisite such as Form type and Web.xml configuration is also included).

Can anyone help me out?

Upvotes: 0

Views: 3604

Answers (2)

jrey
jrey

Reputation: 2183

If you create dinamically yours input uploads? with binding property

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

in your backing bean add property

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;

}

Upvotes: 1

ymajoros
ymajoros

Reputation: 2738

Does something prevent you from using JSF 2? Primefaces ( www.primefaces.org ) has a multiple fileupload component. It is available for JSF 1.2, but development will go on for JSF 2 only.

Upvotes: 0

Related Questions