f:param
可以很好地与h:link
一起使用,但无法与p:commandLink
或h:commandLink
一起使用。
例如,我有两个页面test_first.xhtml
和test_second.xhtml
,以及一个后台java beanTestBean.java
。
我开始运行test_first.xhtml
。
如果我点击link1
,它是一个h:link
,页面将重定向到test_second.xhtml
。借助f:param
的帮助,浏览器的地址栏将显示.../test_second.xhtml?id=1
。在该页面上,将打印出testBean.userId
。
如果我点击link2
或link3
,页面将重定向到test_second.xhtml
。然而,地址栏仅显示.../test_second.xhtml
,没有?id=#
!并且testBean.userId
不会在该页面上打印。
如何使commandLink
与f:param
一起工作?有时我希望链接不是重定向到另一个页面,而是根据数据调用一些bean方法。
test_first.xhtml:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui">
<h:head/>
<h:body>
<h:form>
<h:link value="link1" outcome="test_second" >
<f:param name="id" value="1"/>
</h:link>
<br/><br/>
<h:commandLink value="link2" action="test_second?faces-redirect=true" >
<f:param name="id" value="2" />
</h:commandLink>
<br/><br/>
<p:commandLink value="link3" action="test_second?faces-redirect=true">
<f:param name="id" value="3" />
</p:commandLink>
<br/><br/>
</h:form>
</h:body>
</html>
test_second.xhtml:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui">
<f:metadata>
<f:viewParam name="id" value="#{testBean.userId}" />
</f:metadata>
<h:head/>
<h:body>
<h:form>
This is the second page.
<h:outputText value="Selected id is #{testBean.userId}" />
<h:commandButton value="Print page id" action="#{testBean.print()}" />
</h:form>
</h:body>
</html>
TestBean.java
@ManagedBean
@SessionScoped
public class TestBean implements Serializable{
private Integer userId;
public void print() {
System.out.println(userId);
}
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
}