验证失败的回发后如何保留f:viewParam值 [英] How to retain f:viewParam values after postback with validation failed
问题描述
我有简单形式的xhtml页面.此页面用于几个数据表.为了指定数据表(依此类推),我使用GET请求参数. xhtml页面通过
I have xhtml page with simple form. This page is using for several data tables. To specify data table (and so on), I use GET request parameters. xhtml page receive it through
<f:metadata>
<f:viewParam id="page" name="page" value="#{itemsBean.page}"/>
</f:metadata>
并再次通过导航规则,例如
and pass again by the navigation rules like
<navigation-case>
<description>
Global rule for going to the items page from any page
</description>
<from-outcome>items</from-outcome>
<to-view-id>/items.xhtml</to-view-id>
<redirect>
<view-param>
<name>page</name>
<value>#{itemsBean.page}</value>
</view-param>
</redirect>
</navigation-case>
但是,如果我在xhtml文件中使用这样的输入
But if I use inputs in the xhtml file like this
<h:inputText id="itemName" value="#{itemsBean.name}"
required="true" requiredMessage="Value for this field required!"/>
在尝试接受输入中没有文本的表单后,我无法恢复视图参数. 我试图使用隐藏的输入来传递参数
I cannot restore view param after trying to accepting form with no text in input. I tried to use hidden input for passing parameters
<h:inputHidden id="page" value="#{itemsBean.page}" />
,但似乎之前运行过验证,而itemsBean.page仍然为空. itemsBean是RequestScoped.我做错了什么?我该如何传递参数?
, but seems like validation runs before and itemsBean.page is still empty. itemsBean is RequestScoped. What things I do wrong? How can I pass the param?
感谢您浪费时间.
推荐答案
您需要保持将请求参数传递给后续请求.在普通香草" HTML中,您确实已经为此使用了<input type="hidden">
,但是不幸的是,JSF <h:inputHidden>
不能那样工作.如果由于另一个输入字段而导致常规验证失败,则与<h:inputHidden>
关联的模型值将根本不会更新.
You need to keep passing request parameters to the subsequent request. In "plain vanilla" HTML you'd indeed have used <input type="hidden">
for this, but the JSF <h:inputHidden>
unfortunately doesn't work that way. In case of a general validation failure, caused by another input field, the model value associated with <h:inputHidden>
won't be updated at all.
您应该在UICommand
组件中使用<f:param>
来保留后续请求的请求参数.例如
You should be using a <f:param>
in the UICommand
component instead to retain request parameters for the subsequent request. E.g.
<h:commandButton ...>
<f:param name="page" value="#{param.page}" />
</h:commandButton>
或者,您可以使用<o:form>
: //omnifaces.org"rel =" nofollow> OmniFaces JSF实用程序库,它基本上扩展了<h:form>
并带有附加属性includeViewParams
,该属性使您可以保留表单中后续请求的视图参数操作网址.
Alternatively, you can use the <o:form>
of the OmniFaces JSF utility library, it basically extends the <h:form>
with an additional attribute includeViewParams
which enables you to retain the view parameters for the subsequent request in the form action URL.
<o:form includeViewParams="true">
...
</o:form>
如果您有多个命令按钮/链接和ajax操作,这样做可能最终会更容易,并且这是万一您希望在非ajax回发期间保留相同URL的唯一方法.
This may end up to be easier if you have multiple command buttons/links and ajax actions, and this would be the only way in case you'd like to retain the same URL during non-ajax postbacks.
这篇关于验证失败的回发后如何保留f:viewParam值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!