自动在 URL 中包含视图参数 [英] Automatically include view parameters in URL
问题描述
我有一个简单的 jsf 页面,带有视图参数和正在处理这些参数的加载方法:
I have simple jsf page with view params and load method which is processing those params:
<f:metadata>
<f:viewParam name="param1" value="#{bean.param1}"/>
<f:viewParam name="param2" value="#{bean.param2}"/>
<f:viewParam name="param3" value="#{bean.param3}"/>
<f:event type="preRenderView" listener="#{bean.load()}"/>
</f:metadata>
我还在@PostConstruct
中设置了一些初始值.如何将用户重定向到包含这些参数(不为空)的新位置.例如用户在浏览器中输入:
I also set some initial values in @PostConstruct
.
How to redirect user to new location that include those parameters (which are not null).
For example user enter in browser:
domain.com/page.jsf
并重定向到:
domain.com/page.jsf?param1=valueA
因为 param1 是在 @PostConstruct
中设置的.
because param1 was set in @PostConstruct
.
另一个问题 - 我在页面上有引用相同视图的链接:
Another question - I have links on page referencing same view:
<h:link value="clickme">
<f:param name="param3" value="otherValue"/>
</h:link>
当用户使用 ?param1=someValue
进入页面并点击链接时,被重定向到 ?param3=otherValue
但我想重定向到 ?param1=someValue¶m3=otherValue
.我知道我可以在
中添加更多参数,但是很难在每个
When user enters page with ?param1=someValue
and clicks link, got redirected to ?param3=otherValue
but I want to redirect to ?param1=someValue¶m3=otherValue
.
I know I can add more parameters in <h:link>
but it's diffucult to add every possible param in every <h:link>
附注.我使用本主题中的 BalusC 提示 JSF 2 和 Post/Redirect/Get?
PS. I use BalusC tip from this topic JSF 2 and Post/Redirect/Get?
推荐答案
至于第一个问题:您可以在导航案例结果中添加includeViewParams=true
.但是,当您在 preRenderView
方法中执行此操作时,您永远无法保证会成功重定向.那时可能已经太晚了.
As to the 1st question: you can add includeViewParams=true
to the navigation case outcome. But you can never guarantee that you'll sucessfully be redirected while you're doing that inside a preRenderView
method. It might be already too late then.
关于第二个问题:您可以将
的includeViewParams
属性设置为true
.
As to the 2nd question: you can set the includeViewParams
attribute of <h:link>
to true
.
<h:link value="clickme" includeViewParams="true">
<f:param name="param3" value="otherValue"/>
</h:link>
或者,您也可以将 includeViewParams=true
添加到结果中.
Alternatively, you can also add includeViewParams=true
to the outcome.
<h:link value="clickme" outcome="otherPage?includeViewParams=true">
<f:param name="param3" value="otherValue"/>
</h:link>
这篇关于自动在 URL 中包含视图参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!