h:inputFile value becomes blank after submit or validation fail of other input, how to retain its value?

百般思念 提交于 2019-11-27 08:04:45

问题


With many other standard JSF components, when a form is submitted and server side validation fails, the page is rendered again with the previously submitted form fields filled in for the user to edit and resubmit. I am looking for that same behavior from the new h:inputFile and I'm not finding it.

As a simple example, I have a form with an h:inputText and an h:inputFile. The inputText has server side validation enabled. If the user enters invalid text and selects a file and submits, the file is uploaded and the form is rendered with the invalid text in the text field and a h:message indicating the validation result. The issue I'm trying to address is at this point they correct their text input, but have to select a file again, and upload it again. Is there something basic I am missing, or is this the intended behavior?


回答1:


HTML disallows prefilling an <input type="file"> as that otherwise opens a huge security hole. As JSF is in this context just a HTML code generator, it can't do much against that.

Your best bet is to ajax-submit the form and ajax-render only the parts which really need to be updated, such as messages (i.e. don't use render="@form"). This way the initial input value stays.

E.g.

<h:form enctype="multipart/form-data">
    <h:inputText id="text" />
    <h:message id="m_text" for="text" />

    <h:inputFile id="file" />
    <h:message id="m_file" for="file" />

    <h:commandButton value="submit">
        <f:ajax execute="@form" render="m_text m_file" />
    </h:commandButton>
</h:form>

See also:

  • Understanding PrimeFaces process/update and JSF f:ajax execute/render attributes


来源:https://stackoverflow.com/questions/30485870/hinputfile-value-becomes-blank-after-submit-or-validation-fail-of-other-input

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!