gpt4 book ai didi

java - 多个文件上传 - JSF

转载 作者:行者123 更新时间:2023-12-01 15:51:56 25 4
gpt4 key购买 nike

我正在使用 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";
}

每当我增加或减少编号时,此实现都会重置文件上传值。文件上传字段。另外,当我提交表单时,我无法获取 UploadedFile 对象(还包括文件上传先决条件,例如表单类型和 Web.xml 配置)。

谁能帮帮我吗?

最佳答案

如果您动态地创建您的输入上传?具有绑定(bind)属性

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

}

关于java - 多个文件上传 - JSF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5854263/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com