2013-01-23 19 views
13

Iam JSF中的新增功能。setPropertyActionListener vs屬性vs param之間的區別是什麼? ? 時候可以使用setPropertyActionListener .Thanks提前JSF Core標記:setPropertyActionListener vs屬性vs參數

+3

相關:http://balusc.blogspot.com/2006/06/communication-in-jsf.html請注意,由於EL 2.2可以將參數直接傳遞到操作方法而不需要任何這些標籤。 – BalusC

回答

28

1. F:setPropertyActionListener:

有了這個標籤,你可以直接在您的支持bean設置屬性。例如:

XHTML:

<h:commandButton action="page.xhtml" value="OK"> 
    <f:setPropertyActionListener target="#{myBean.name}" value="myname"/> 
</h:commandButton> 

支持bean:

@ManagedBean 
@SessionScoped 
public class MyBean{ 

    public String name; 

    public void setName(String name) { 
     this.name= name; 
    } 

} 

這將設置後臺bean的name屬性重視MYNAME

2. F:PARAM:

這個標籤簡單設定請求參數。例如:

XHTML:

<h:commandButton action="page.xhtml"> 
    <f:param name="myparam" value="myvalue" /> 
</h:commandButton> 

這樣你就可以得到這個參數在支持bean:

FacesContext.getExternalContext().getRequestParameterMap().get("myparam") 

3 F:屬性:

有了這個標籤,你可以通過屬性,以便您可以從支持bean的操作偵聽器方法中獲取該屬性。

XHTML:

<h:commandButton action="page.xhtml" actionListener="#{myBean.doSomething}"> 
    <f:attribute name="myattribute" value="myvalue" /> 
</h:commandButton> 

,讓你可以從動作監聽器方法這個屬性:

public void doSomething(ActionEvent event){ 
    String myattr = (String)event.getComponent().getAttributes().get("myattribute"); 
} 

你應該使用f:setPropertyActionListener,每當你想設置的支持bean的屬性。如果您想將參數傳遞給後備bean,請考慮f:paramf:attribute。此外,重要的是要知道,使用f:param您只能通過String值,而使用f:attribute則可以傳遞對象。