2013-01-23 97 views
0

在我page1.xhtml如何將param從一頁傳遞到其他頁面?

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

如何CA當我點擊page_two.onSubmit按鈕時,我在pageTwoBean中獲得pageOneBean.name?

+1

你需要在PageOneBean上使用另一個作用域或通過url傳遞參數,看看這個:http://balusc.blogspot.com/2006/06/communication-in-jsf.html –

+0

在這裏檢查它 - http://stackoverflow.com/questions/3010732/jsf-pass-parameter-to-another-page –

回答

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

試一下,看看是怎麼回事。

相關問題