提交没有附件的表格
Submit form without attached file
我想创建带附件的 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 ...
?
此外,当我使用按钮上传按钮附加文件时,不会出现文件上传成功的消息。
您的 inputFile
符合 要求。这就是为什么您必须始终先上传的原因。
此外,如果您没有看到任何消息,那是因为您没有再次呈现 h:message
。尝试在您的托管 bean 中使用它:
FacesContext.getCurrentInstance().getPartialViewContext().getRenderIds()
.add("idMessage");
改变
<h:inputFile id="fileToUpload" value="#{contacts.file}" required="true" requiredMessage="No file selected ..."/>
到
<h:inputFile id="fileToUpload" value="#{contacts.file}" />
我想创建带附件的 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 ...
?
此外,当我使用按钮上传按钮附加文件时,不会出现文件上传成功的消息。
您的 inputFile
符合 要求。这就是为什么您必须始终先上传的原因。
此外,如果您没有看到任何消息,那是因为您没有再次呈现 h:message
。尝试在您的托管 bean 中使用它:
FacesContext.getCurrentInstance().getPartialViewContext().getRenderIds()
.add("idMessage");
改变
<h:inputFile id="fileToUpload" value="#{contacts.file}" required="true" requiredMessage="No file selected ..."/>
到
<h:inputFile id="fileToUpload" value="#{contacts.file}" />