f: param nie działa z p: commandLink lub h: commandLink na łańcuchu zapytania
f:param
działa świetnie zh:link
, ale nie zp:commandLink
lubh:commandLink
.
Na przykład mam dwie stronytest_first.xhtml
itest_second.xhtml
i wspierająca fasola javaTestBean.java
.
Zaczynam biectest_first.xhtml
.
Jeśli kliknęlink1
, który jesth:link
, strona zostanie przekierowana dotest_second.xhtml
. Z pomocąf:param
, pojawi się pasek adresu przeglądarki.../test_second.xhtml?id=1
. Na tej stronietestBean.userId
zostaje wydrukowany.
Jeśli kliknęlink2
lublink3
, strona przekierowuje dotest_second.xhtml
. Jednak pasek adresu pokazuje tylko.../test_second.xhtml
, nie ma?id=#
! ItestBean.userId
nie drukuje się na tej stronie.
Jak mogę to zrobićcommandLink
pracować zf:param
? Czasami chcę, aby łącze nie przekierowywało do innej strony, ale żeby wywoływało pewne metody fasoli w zależności od danych.
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;
}
}