我创建了一个简单的jsf
复制文件来上传文件,但是没有创建像这样的jsf
输入文件
<h:form id="fileToUploadForm" enctype="multipart/form-data">
<h:inputFile id="fileToUpload" required="true" f5:multiple="multiple"
requiredMessage="No file selected ..." onchange="fileSelected();"
render="FilesTable"/>
<h:commandButton type="button" onclick="uploadFile()" value="Upload" />
</h:form>
我想以编程方式创建它并将其添加到表单中。
例如:htmlform.getChildren().add(<File Component>)
答案 0 :(得分:-1)
Primefaces文档建议使用FileUpload组件:
p:fileUpload
与 multiple="true"
sizeLimit
,fileLimit
,allowTypes
和dragDropSupport
XHTML文件:
<h:form>
<p:fileUpload fileUploadListener="#{fileUploadView.handleFileUpload}" mode="advanced"
dragDropSupport="false" multiple="true" update="messages" fileLimit="3"
sizeLimit="100000 allowTypes="/(\.|\/)(gif|jpe?g|png)$/" />
<p:growl id="messages" showDetail="true" />
</h:form>
ManagedBean:
@ManagedBean
public class FileUploadView {
private UploadedFile file;
public UploadedFile getFile() {
return file;
}
public void setFile(UploadedFile file) {
this.file = file;
}
public void upload() {
if(file != null) {
FacesMessage message = new FacesMessage("Succesful", file.getFileName() +
" is uploaded.");
FacesContext.getCurrentInstance().addMessage(null, message);
}
}
public void handleFileUpload(FileUploadEvent event) {
FacesMessage msg = new FacesMessage("Succesful", event.getFile().getFileName() +
" is uploaded.");
FacesContext.getCurrentInstance().addMessage(null, msg);
}
}
答案 1 :(得分:-1)
由于我之前使用的是jsf 2.1,所以找不到该类,
javax.faces.component.html.HtmlInputFile
是jsf API 2.2及更高版本中存在的类,
org.omnifaces.component.input.InputFile
是我在杂技演员中找到的替代课程。