【问题标题】:Submit form without attached file提交不带附件的表格
【发布时间】:2015-06-13 09:23:29
【问题描述】:

我想创建带有附件的 JSF 表单。而且还给用户一个选项形式来提交不带附件的文件。我不希望这是强制性的。

<h:form id="form" enctype="multipart/form-data">                               
    <div class="string">
        <label class="name">                                       
            <h:inputText id="name" value="#{contacts.name}" pt:placeholder="Name*:"/>
        </label>
    </div>
    <label class="message">                                    
        <h:inputTextarea value="#{contacts.comment}" pt:placeholder="Comment*:"/>
    </label>
    <h:inputFile id="fileToUpload" value="#{contacts.file}" required="true" requiredMessage="No file selected ..."/>
    <h:commandButton value="Upload" action="#{contacts.upload()}">
        <f:ajax execute="fileToUpload" />
    </h:commandButton>
    <h:message showDetail="false" showSummary="true" for="fileToUpload" style="color:red"/>
    <div class="btns">
        <h:commandLink id="submitlink" class="link" value="submit" action="#{contacts.sendEmail}" >
            <f:ajax render="@form" execute="@form" onevent="handleDisableButton" resetValues="true"/>
        </h:commandLink>
    </div>
    <h:outputText id="output" value="#{contacts.result}" />
</h:form>

有没有办法实现这一点,因为现在如果我尝试提交表单,我总是会收到消息No file selected ...? 此外,当我使用按钮上传按钮附加文件时,不会显示成功上传文件的消息。

【问题讨论】:

    标签: jsf jsf-2.2


    【解决方案1】:

    您的inputFile 必填。这就是为什么您必须始终先上传。

    另外,如果您没有看到任何消息,那是因为您没有再次呈现您的 h:message。尝试在您的托管 bean 中使用它:

    FacesContext.getCurrentInstance().getPartialViewContext().getRenderIds()
    .add("idMessage");
    

    【讨论】:

      【解决方案2】:

      改变

      <h:inputFile id="fileToUpload" value="#{contacts.file}" required="true" requiredMessage="No file selected ..."/>
      

      <h:inputFile id="fileToUpload" value="#{contacts.file}" />
      

      【讨论】:

        猜你喜欢
        • 2018-11-04
        • 1970-01-01
        • 2023-03-15
        • 1970-01-01
        • 1970-01-01
        • 2012-03-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多