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

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

问题描述

对于许多其他标准的JSF组件,当表单被提交并且服务器端验证失败时,页面被再次呈现,其中先前提交的表单域被填充以供用户编辑并重新提交。我正在从新的h:inputFile寻找相同的行为,我没有找到它。



作为一个简单的例子,我有一个形式与h:inputText和一个h:inputFile。 inputText启用了服务器端验证。如果用户输入无效文本并选择文件并提交,则文件将被上传,表单将在文本字段中显示为无效文本,并显示一个表示验证结果的h:消息。我试图解决的问题是在这一点上,他们纠正他们的文字输入,但不得不再次选择一个文件,并再次上传。有什么基本的我想念,或者这是预期的行为? HTML不允许预先填充< input type =file> ,否则会打开一个巨大的安全漏洞。由于JSF在这个上下文中只是一个HTML代码生成器,所以它不能做太多的事情。



最好的办法是将ajax提交表单和ajax呈现只有真正需要更新的部分,如消息(即不要使用 render =@ form)。例如

 < / p> h:form enctype =multipart / form-data> 
< h:inputText id =text/>
< h:message id =m_textfor =text/>

< h:message id =m_filefor =file/>

< h:commandButton value =submit>
< / h:commandButton>
< / h:表格>


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?

解决方案

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>

这篇关于h:inputFile的值在其他输入验证失败后变为空白,如何保留其值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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