h:inputFile值在其他输入提交或验证失败后变为空白,如何保留其值? [英] h:inputFile value becomes blank after submit or validation fail of other input, how to retain its value?

查看:126
本文介绍了h:inputFile值在其他输入提交或验证失败后变为空白,如何保留其值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于许多其他标准JSF组件,当提交表单并且服务器端验证失败时,将再次显示该页面,其中填充了先前提交的表单字段,供用户编辑和重新提交.我正在从新的h:inputFile寻找相同的行为,但找不到.

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.

作为一个简单的示例,我有一个带有h:inputText和h:inputFile的表单. inputText已启用服务器端验证.如果用户输入了无效文本并选择了文件并提交,则将上载文件,并使用文本字段中的无效文本和表示验证结果的h:message呈现表单.我要解决的问题是此时他们纠正了文本输入,但是不得不再次选择一个文件,然后再次上传.我缺少基本的东西吗,或者这是预期的行为?

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?

推荐答案

HTML不允许预填充<input type="file">,否则会打开巨大的安全漏洞.由于在这种情况下,JSF只是一个HTML代码生成器,因此它不能做很多事情.

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.

您最好的选择是ajax提交表单,并且ajax仅渲染 真正需要更新的部分,例如消息(即,不要使用render="@form").这样,初始输入值将保持不变.

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.

例如

<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>

另请参见:

  • 了解PrimeFaces流程/更新和JSF f: Ajax执行/渲染属性
  • See also:

    • Understanding PrimeFaces process/update and JSF f:ajax execute/render attributes
    • 这篇关于h:inputFile值在其他输入提交或验证失败后变为空白,如何保留其值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆