JSF核心标签:setPropertyActionListener vs属性vs param [英] JSF Core Tag :setPropertyActionListener vs attribute vs param

查看:120
本文介绍了JSF核心标签:setPropertyActionListener vs属性vs param的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

setPropertyActionListener vs attribute vs param有什么区别?

What is the difference between setPropertyActionListener vs attribute vs param?

何时使用setPropertyActionListener?

推荐答案

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.

这篇关于JSF核心标签:setPropertyActionListener vs属性vs param的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆