JSF Core Tag :setPropertyActionListener vs attribute vs param

前端 未结 1 455
孤独总比滥情好
孤独总比滥情好 2021-02-04 09:05

What is the difference between setPropertyActionListener vs attribute vs param?

When would use the setPropertyActionListener

相关标签:
1条回答
  • 2021-02-04 09:31

    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.

    0 讨论(0)
提交回复
热议问题