【问题标题】:<p:fileUpload> with other input elements (commandButton) in form<p:fileUpload> 与表单中的其他输入元素 (commandButton)
【发布时间】:2014-05-06 12:46:17
【问题描述】:

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

因此,我试图以单一形式捕获 roleid 并使用 &lt;p:fileUpload&gt;,在这里,当我选择角色并单击文件上传器时,所选角色 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 以及文件上传,或者我想如何在同一页面中同时捕获角色和文件。

【问题讨论】:

    标签: jsf file-upload primefaces


    【解决方案1】:

    您可以使用remoteCommand 将值传递给支持bean。尝试这样的事情:

    <p:remoteCommand name="passValue">
        <f:setPropertyActionListener for="role"
                                     value="#{userBean.selectedroleid}"
                                     target="#{userBean.selectedroleid}"/>
    </p:remoteCommand>
    

    然后将 onStart 选项添加到您的 fileUploader 中,例如:

    onstart="passValue()"
    

    【讨论】:

      【解决方案2】:

      这是一个老问题,但我在 primefaces 6.2 中遇到了这个问题

      问题的原因:文件上传应该是表单的单个输入,也可以是 ajax 请求(在 mode="advanced" 的情况下)而不发布/处理其他输入。

      我的解决方法:上传监听器只存储文件,业务逻辑(与其他输入)在远程命令中完成:

      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
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-03-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-08-29
        • 2018-10-20
        相关资源
        最近更新 更多