1

I am passing three feields but only the first one is being passed even though the built URL shows correct values:

TestBean1:

@ManagedBean
@RequestScoped
public class TestBean1 implements Serializable  {


private String field1;
private String field2;
private String field3;

// Constructor
public TestBean1() {

}

public void handleRequest() throws IOException {

    setField1("1 Julie");
    setField2("2 Kyle");
    setField3("3 Bob");

    String field1 = URLEncoder.encode(this.field1, "UTF-8");
    String field2 = URLEncoder.encode(this.field2, "UTF-8");
    String field3 = URLEncoder.encode(this.field3, "UTF-8");     

    String url = "Bean2.jsf?field1=" + field1 + "&field2=" + field2
                + "&field3=" + field3;

      FacesContext.getCurrentInstance().getExternalContext().redirect(url); 

}

// Getter and Setters
public String getField1() {
    return field1;
}

public void setField1(String field1) {
    this.field1 = field1;
}
public String getField2() {
    return field2;
}

public void setField2(String field2) {
    this.field2 = field2;
}

public String getField3() {
    return field3;
}

public void setField3(String field3) {
    this.field3 = field3;
}

  }

Bean1.jsf

  <ui:composition 

 xmlns="http://www.w3.org/1999/xhtml"
 xmlns:f="http://java.sun.com/jsf/core"
 xmlns:ui="http://java.sun.com/jsf/facelets"
 xmlns:h="http://java.sun.com/jsf/html"
 xmlns:p="http://primefaces.org/ui"
 template="/templates/template.xhtml">


<ui:define name="title">
    Bean 1
</ui:define>

  <ui:define name="content">

  <h:body>

<h:form>
  <table>
    <tr>        <td>Bean 1 </td>            </tr>
    <tr>        <td> </td>              </tr>
    <tr>        <td> </td>          </tr>
    <tr>        <td><h:commandLink value="Submit" action="#
                                    {testBean1.handleRequest}" /> </td>     </tr>
</table>

</h:form>   
  </h:body>
  </ui:define>  
  </ui:composition> 

Bean2.jsf

  <ui:composition 

 xmlns="http://www.w3.org/1999/xhtml"
 xmlns:f="http://java.sun.com/jsf/core"
 xmlns:ui="http://java.sun.com/jsf/facelets"
 xmlns:h="http://java.sun.com/jsf/html"
 xmlns:p="http://primefaces.org/ui"
 template="/templates/template.xhtml">

 <f:metadata>
 <f:viewParam name="field1" value="#{testBean2.field1}"/>
 <f:viewParam name="field2" value="#{testBean2.field2}"/>
 <f:viewParam name="field3" value="#{testBean2.field3}"/>
 </f:metadata>

<ui:define name="title">
    Bean 2
</ui:define>

 <ui:define name="content">

 <h:body>

<h:form>

<table>
    <tr>    <td>Bean 2 </td> </tr>
    <tr>    <td><h:inputText id="Input" value="#{testBean2.myInput}"/>  </td> /tr>      <tr>    <td> </td>  </tr>
    <tr>    <td> <h:outputText value="#{testBean2.field1}"    />  </td>     </tr>
    <tr>    <td> <h:outputText value="#{testBean2.field2}" />    </td>      </tr>
    <tr>    <td> <h:outputText value="#{testBean2.field3}" />    </td>      </tr>
    <tr>    <td> <h:commandLink value="Click" 
                       action="#{testBean2.handleRequest}" /> </td> </tr>

     </table>

</h:form>   
   </h:body>
   </ui:define> 
   </ui:composition>

TestBean2

     @ManagedBean
     @ViewScoped
     public class TestBean2  implements Serializable  {

private static final long serialVersionUID = -9L;

private String field1;
private String field2;
private String field3;

private String myInput = "Hello";


// Constructor
public TestBean2 () {
}

public void handleRequest() {

    System.out.println("field1 " + field1  + "  field2 " + field2 + "  field3 " + 
                    field3);

}


// Getter and Setters

    public String getField1() {
        return field1;
    }

    public void setField1(String field1) {
        this.field1 = field1;
    }
    public String getField2() {
        return field2;
    }

    public void setField2(String field2) {
        this.field2 = field2;
    }

    public String getField3() {
        return field3;
    }

    public void setField3(String field3) {
        this.field3 = field3;
    }

public String getMyInput() {
    return myInput;
}

public void setMyInput(String myInput) {
    this.myInput = myInput;
}

   }

When I run this I get only value for field1.

Thanks,

Peter
  • 131
  • 1
  • 13
  • How does the URL in browser address bar look like after the redirect? – BalusC Mar 20 '12 at 16:02
  • http://localhost:8080/TransactionViewerPF/Bean2.jsf?field1=1+Julie&field2=2+Kyle&field3=3+Bob – Peter Mar 20 '12 at 16:04
  • What if you change `&` to `&`? It should not matter, but you never know... If this works, please tell what browser you're using. – BalusC Mar 20 '12 at 19:20

1 Answers1

1

In template clients, the <f:metadata> needs to go inside an <ui:define>, otherwise the behaviour is undetermined. See also the documentation of the <f:metadata> tag for an example.

BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
  • BalusC Thanks! I am still not getting field2 and field3. Is there any other way to pass those values to Bean2? – Peter Mar 20 '12 at 19:18
  • You are great BalusC!Amazing. It works when I chnaged to String url = "Bean2.jsf?field1=" + field1 + "&field2=" + field2 + "&field3=" + field3; And I am using IE 8.0 Thanks you so much for your help!! – Peter Mar 20 '12 at 19:33
  • BalusC, not trying to bug you but just wanted to share with you that I am now applying the same in a real application, using metadata and & to build URL but I am not getting the values even though I see all those values in URL. Not complaining just feeling better after letting you know. I am also stuck with a question that I need to use these feilds to buld another list when constructor is called - I am not sure how to get hold of those values (say when f: viewparam starts working). regards and thanks. – Peter Mar 20 '12 at 22:46
  • I get the values only after page is rendered and submitbutton is pressed after key fields are entered on the form. The purpose was to get the key fields from the selected row and render the page details for that key. Since the key or say param values are not being available, the page is rendered with messgae saying no data found. Even if I do not provide any input and click the submit button, the setter methdos for those params are executed and values becomes available in bean. Baiscally setters methods get executed on each click on the submit button. – Peter Mar 20 '12 at 23:56
  • Please do not think that I am taking you for granted but definitely need some dirctions. THANK YOU! – Peter Mar 20 '12 at 23:57
  • The values are not available in the constructor simply because it's not possible to call the setters without having a constructed instance. Use ``. See also http://stackoverflow.com/questions/6377798/what-can-fmetadata-and-fviewparam-be-used-for – BalusC Mar 21 '12 at 00:08
  • Many thanks BalusC! I really appreciate your help. You are awesome. If you do not mind then can I address you as Guru? Sincere regards, Peter. – Peter Mar 21 '12 at 02:57