Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSF Core Tag :setPropertyActionListener vs attribute vs param

Tags:

jsf-2

What is the difference between setPropertyActionListener vs attribute vs param?

When would use the setPropertyActionListener?

like image 940
tech2504 Avatar asked Jan 23 '13 07:01

tech2504


1 Answers

1. f:setPropertyActionListener:

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>

backing bean:

@ManagedBean
@SessionScoped
public class MyBean{

    public String name;

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

}

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>

so you can get this parameter in backing bean:

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

3. f:attribute:

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

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.

like image 150
partlov Avatar answered Nov 12 '22 00:11

partlov