当第一页使用与第二页相同的托管 bean 时,f:viewParam 是否仅在 url 中传递查询字符串? [英] Does f:viewParam only pass query string in url when first page uses the same managed bean as the second page?
问题描述
让我们以搜索页面和结果页面为例.如果我有一个 ViewScoped bean 来处理我的搜索页面和我的结果页面,我可以使用这样的东西通过 url 传递参数:
Let's use a search page and a results page for example. If i have a ViewScoped bean that handles my search page and my results page, i'm able to pass parameters through the url using something like this:
search.xhtml
search.xhtml
<p:commandButton value="Search" type="submit" action="#{searchBacker.search}" >
支持豆
@ManagedBean(name="search")
@ViewScoped
public class searchBacker extends AbstractBacking {
private String firstName = null;
private String lastName = null;
private String results = null;
public String search() {
return "results?faces-redirect=true&includeViewParams=true";
}
public void getResults() {
MyDAO dao = new MyDAO();
results = dao.getResults(firstName, lastName);
}
//getters and setters
}
结果.xhtml
<f:metadata>
<f:event type="preRenderView" listener="#{searchBacker.getResults}" />
<f:viewParam name="firstName" value="#{searchBacker.firstName}"/>
<f:viewParam name="lastName" value="#{searchBacker.lastName}"/>
</f:metadata>
现在假设我有两个托管 bean - 一个用于搜索页面,一个用于结果页面.
Now lets say i have two managed beans - one for the search page and one for the results page.
查询字符串仍会使用 2 个不同的托管 bean 构建在 url 中,还是仅在两个页面使用相同的托管 bean 时才有效?
Will the query string still be built in the url with 2 different managed beans or does this only work when using the same managed bean for both pages?
更新
我在 search.xhtml 和 results.xhtml 页面上有相同的
,但唯一的区别是我的 f:viewParam 值
指向我的 results.xhtml 中的支持者与我的 search.xhtml 中的支持者不同.当我这样做时,没有参数通过 url 传递.当我将 results.xhtml 中的 f:viewParam value
指向我在 search.xhtml 中使用的同一个支持者时,参数通过 url 传递就好了,但该值不存在于结果支持者在我需要的地方.如果我的 results.xhtml 页面中有重复的 f:viewParam
s - 一个带有搜索支持者,一个带有结果支持者,一切正常.有 2 个相同的 f:viewParam
s 和两个托管 bean 是正确的方法吗?
I have the same <f:viewParam>
on my search.xhtml and results.xhtml page, but the only difference is that my f:viewParam value
points to a different backer in my results.xhtml than in my search.xhtml. When i do this, no params get passed through the url. When I point my f:viewParam value
in results.xhtml to the same backer that i use in search.xhtml, the param gets passed through the url just fine, but the value doesn't exist in the results backer where i need it. If i have duplicate f:viewParam
s in my results.xhtml page - one with the search backer and one with the results backer, everything works fine. Is having 2 of the same f:viewParam
s with both managed beans the correct way to do this?
示例:
results.xhtml - 参数通过 url 传递,但在我的 resultsBacker 中不可用
results.xhtml - params get passed through url, but are not available in my resultsBacker
<f:metadata>
<f:viewParam name="firstName" value="#{searchBacker.firstName}"/>
<f:viewParam name="lastName" value="#{searchBacker.lastName}"/>
</f:metadata>
results.xhtml - 没有参数通过 url 传递
results.xhtml - no params get passed through url
<f:metadata>
<f:viewParam name="firstName" value="#{resultsBacker.firstName}"/>
<f:viewParam name="lastName" value="#{resultsBacker.lastName}"/>
</f:metadata>
results.xhtml - 参数通过 url 传递并在我的 resultsBacker 中可用,但看起来很笨重.这是执行此操作的正确方法还是我仍然遗漏了什么?
results.xhtml - params get passed through url and are available in my resultsBacker, but seems clunky. Is this the correct way to do this or am i still missing something?
<f:metadata>
<f:viewParam name="firstName" value="#{searchBacker.firstName}"/>
<f:viewParam name="firstName" value="#{resultsBacker.firstName}"/>
<f:viewParam name="lastName" value="#{searchBacker.lastName}"/>
<f:viewParam name="lastName" value="#{resultsBacker.lastName}"/>
</f:metadata>
推荐答案
includeViewParams
仅在两者源视图和目标视图都将所需的视图参数声明为
.
includeViewParams
works only if both the source and target view have the desired view parameters declared as <f:viewParam>
.
因此,在您的 parituclar 情况下,您需要在 search.xhtml
和 results.xhtml
.在幕后,includeViewParams
即只抓取当前视图的视图参数,然后只应用在目标视图中也声明的那些.
So, in your parituclar case, you need to put the <f:viewParam>
s with very same name
in both the search.xhtml
and results.xhtml
. Under the covers, the includeViewParams
namely only grabs the view params of the current view and then only applies the ones which are also declared in the target view.
与具体问题无关,您似乎实际上想要一个 GET 表单.在这种情况下,有比使用参数执行 POST-Redirect-GET 更好的方法.看看上面另见"的底部链接.
Unrelated to the concrete problem, you seem to effectively want a GET form. In that case, there is a better way for that than performing a POST-Redirect-GET with parameters. Look at the bottom of the above "See also" link.
这篇关于当第一页使用与第二页相同的托管 bean 时,f:viewParam 是否仅在 url 中传递查询字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!