且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

JSF核心标签:setPropertyActionListener vs属性vs param

更新时间:2023-12-04 17:40:16

1. f:setPropertyActionListener:

使用此标签,您可以直接在支持bean中设置属性.示例:

With this tag, you can directly set property in you backing bean. Example:

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 .

This will set name property of backing bean to value myname.

2. f:param:

此标记简单地设置了请求参数.示例:

This tag simple sets the request parameter. Example:

xhtml:

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

因此您可以在支持bean中获得此参数:

so you can get this parameter in backing bean:

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

3. f:属性:

使用此标记,您可以传递属性,以便可以从备用Bean的动作侦听器方法中获取该属性.

With this tag you can pass attribute so you can grab that attribute from action listener method of your backing bean.

xhtml:

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

因此您可以从操作侦听器方法获取此属性:

so you can get this attribute from action listener method:

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

无论何时要设置备用Bean的属性,都应使用f:setPropertyActionListener.如果要将参数传递给支持Bean,请考虑f:paramf:attribute.同样,重要的是要知道,使用f:param您可以传递String值,而使用f:attribute您可以传递对象.

You should use f:setPropertyActionListener whenever you want to set property of the backing bean. If you want to pass parameter to backing bean consider f:param and f:attribute. Also, it is important to know that with f:param you can just pass String values, and with f:attribute you can pass objects.