使用Get参数导航不获取所有参数

时间:2012-03-20 15:50:45

标签: jsp jsf

我正在传递三个主要内容,但即使构建的URL显示正确的值,也只会传递第一个:

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;
}

   }

当我运行它时,我只得到field1的值。

谢谢,

1 个答案:

答案 0 :(得分:1)

在模板客户端中,<f:metadata>需要进入<ui:define>,否则行为未确定。另请参阅the documentation of the <f:metadata> tag以获取示例。