繁体   English   中英

<p:fileUpload>与表单中的其他输入元素(commandButton)

[英]<p:fileUpload> with other input elements (commandButton) in form

我正在尝试上传带有标识文件的参数的文件,例如,如果有参数角色,其中包含student作为值,则上传文件应包含学生详细信息。

所以在单个表单中我试图捕获roleid并使用<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中也有上传过滤器映射,用于使用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被捕获为null,就好像我单击“保存”按钮一样,角色的正确值正在获取用户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
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM