当第一页使用与第二页相同的托管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}" >
支持bean
@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
}
results.xhtml
results.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.
查询字符串是否仍将使用两个不同的托管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>
,但是唯一的区别是,我的f:viewParam value
指向的results.xhtml中的支持者与search.xhtml中的支持者不同.当我这样做时,没有参数通过URL传递.当我将result.xhtml中的f:viewParam value
指向与search.xhtml中使用的相同的支持者时,参数可以很好地通过url传递,但是该值在我需要的结果支持者中不存在.如果我的results.xhtml页面中有重复的f:viewParam
-一个带有搜索支持者,另一个带有结果支持者,则一切正常.两个托管Bean拥有2个相同的f:viewParam
是正确的方法吗?
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-网址不传递任何参数
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
仅在源视图和目标视图均具有声明为<f:viewParam>
的所需视图参数时都有效.
includeViewParams
works only if both the source and target view have the desired view parameters declared as <f:viewParam>
.
因此,在您的parituclar案例中,您需要在search.xhtml
和results.xhtml
中都放入具有相同name
的<f:viewParam>
.在幕后,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屋!