我怎样才能将param从一页传递到另一页?

时间:2013-01-23 16:09:08

标签: jsf jsf-2

在我的page1.xhtml

<h:form>
    <h:inputText value="#{page_one.name}" />
    <h:commandButton action="#{page_one.onSubmit}" value="Submit" />
</h:form>

PageOneBean.class

@ManagedBean(name="page_one")
@RequestScoped
public class PageOneBean {
    private String name;
    public String onSubmit(){
    return "/page2.xhtml";
    }
}

page2.xhtml

<h:form>
    <h:commandButton action="#{page_two.onSubmit}" value="Submit" />
</h:form>

PageTwoBean.class

@ManagedBean(name = "page_two")
@RequestScoped
public class PageTwoBean {
private String name;
public String onSubmit() {
    System.out.println("name" + name);
    return null;
}
}

当我点击page_two.onSubmit按钮时,如何在pageTwoBean中获取pageOneBean.name?

1 个答案:

答案 0 :(得分:3)

Page2.xhtml

<h:form>
    <f:metadata>
         <f:viewParam name="name" value="#{page_two.name}"/>            
    </f:metadata>

    <h:commandButton action="#{page_two.onSubmit}" value="Submit" />
</h:form>

Bean1:

public class PageOneBean {
private String name;
public String onSubmit(){
    return "/page2.xhtml?name=something";
}
public void setName(String name){ this.name = name;}
public String getName(){ return name; }
}

试一试,看看它是怎么回事。