f:param
h:link
के साथ बहुत अच्छा काम करता है, लेकिन p:commandLink
या h:commandLink
के साथ नहीं।f: param p के साथ काम नहीं करता है: कमांड लिंक्स या एच: क्वेरी स्ट्रिंग पर कमांड लिंक्स
उदाहरण के लिए, मेरे पास दो पृष्ठ test_first.xhtml
और test_second.xhtml
हैं, और एक बैकिंग जावा बीन TestBean.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
के साथ कैसे काम कर सकता हूं? कभी-कभी मैं चाहता हूं कि लिंक किसी अन्य पृष्ठ पर रीडायरेक्ट न करें लेकिन डेटा के आधार पर बीन के कुछ तरीकों को कॉल करें।
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;
}
}
संबंधित: http://stackoverflow.com/questions/4317684/when-should-i-use-houtputlink-instead-of-hcommandlink/4317723#4317723 – BalusC