我正在尝试上传一个带有参数的文件,该参数标识文件,例如,如果有一个参数角色,其中包含学生作为值,则上传文件应包含学生详细信息。
在单一表单中,我试图捕获角色id这里使用<p:fileUpload>
当我选择角色并点击文件上传器时选中的角色id被捕获为null
我正在使用JSF p:fileUpload
,以下是代码:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui"
xmlns:ui="http://java.sun.com/jsf/facelets">
<h:body>
<ui:composition template="/templates/layout.xhtml">
<ui:define name="content">
<h:form enctype="multipart/form-data">
<p:panel>
<h:panelGrid id="grid">
<h:outputLabel for="role" value="Role" style="font-weight:bold" />
<p:selectOneMenu id="role" value="#{userBean.selectedroleid}"
style="width:100%">
<f:selectItem itemLabel="Select User Type" itemValue="" />
<f:selectItems value="#{userBean.roles}" var="role"
itemLabel="#{role.role}" itemValue="#{role.id}" />
</p:selectOneMenu>
<h:outputText value="" />
</h:panelGrid>
<f:facet name="footer">
<p:fileUpload fileUploadListener="#{userBean.readCSVFile}"
mode="advanced" dragDropSupport="false" fileLimit="1"
allowTypes="/(.|/)(csv)$/" label="Select" immediate="true" />
<p:commandButton value="Save" action="#{userBean.readCSVFile1()}"
style="margin:0px" update="grid" icon="ui-icon-disk"
validateClient="true" />
</f:facet>
</p:panel>
</h:form>
</ui:define>
</ui:composition>
</h:body>
</html>
用户bean:捕获角色和文件
@ManagedBean
@ViewScoped
public class UserBean implements Serializable {
@Inject
private RoleService roleService;
private Integer selectedroleid;
private List<Role> roles;
public List<Role> getRoles() {
return roles;
}
public void setRoles(List<Role> roles) {
this.roles = roles;
}
@PostConstruct
public void initialize() {
this.roles = roleService.getRoles();
}
public void readCSVFile(FileUploadEvent event) throws IOException {
logger.debug("Reading data from csv and convert to java object:");
System.out.println("selected role Id " + selectedroleid);
System.out.println("selected file " + event.getFile().getFileName());
}
public void readCSVFile1() throws IOException {
logger.debug("Reading data from csv and convert to java object:");
System.out.println("selected role Id " + selectedroleid);
}
public Integer getSelectedroleid() {
return selectedroleid;
}
public void setSelectedroleid(Integer selectedroleid) {
System.out.println(" selected role id "+ selectedroleid);
this.selectedroleid = selectedroleid;
}
}
和我在Web.xml中也有Upload Filter映射,用于使用JSF上传文件。
<filter>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class>
<init-param>
<param-name>thresholdSize</param-name>
<param-value>2097152</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<servlet-name>Faces Servlet</servlet-name>
</filter-mapping>
但问题是,如果我选择角色并单击文件上传器选中的角色id被捕获为空,就像我单击保存按钮一样,角色的正确值在用户bean处被捕获。
我想获得角色id随着文件上传或一些我想在同一页面中捕获角色和文件。
您可以使用remoteCommand
传递值给后台bean。试试这样做:
<p:remoteCommand name="passValue">
<f:setPropertyActionListener for="role"
value="#{userBean.selectedroleid}"
target="#{userBean.selectedroleid}"/>
</p:remoteCommand>
然后添加onStart
选项到您的fileUploader,如:
onstart="passValue()"
这是一个老问题,但我遇到了这个问题的primefaces 6.2
问题的原因:文件上传应该是一个表单的单一输入,或者它可以是一个ajax请求(在mode="advanced"的情况下),而不需要张贴/处理其他输入。
我的解决方案:上传监听器只存储文件,业务逻辑(与其他输入)在remoteCommand中完成:
xhtml:<h:form>
<p:inputText value="#{helloWorld.msg}" />
...
<p:fileUpload mode="advanced" auto="true"
fileUploadListener="#{helloWorld.handleUpload}"
oncomplete="uploadBusinessLogic();"/>
<p:remoteCommand name="uploadBusinessLogic" actionListener="#{helloWorld.uploadBusinessLogic}"/>
</h:form>
豆:
private String msg;
private InputStream file;
public void handleUpload(FileUploadEvent event) {
try {
this.file = event.getFile().getInputstream();
} catch (IOException e) {
//...
}
}
public void uploadBusinessLogic() {
// msg and file is up to date
}