jsf-2 - JSF 核心标签 :setPropertyActionListener vs attribute vs param

标签 jsf-2

setPropertyActionListener 和有什么区别对比 attribute对比 param ?

何时使用 setPropertyActionListener ?

最佳答案

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

}

这将设置 name支持 bean 的属性值 我的名字 .

2. f:参数:

这个标签简单地设置了请求参数。例子:

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

因此您可以在支持 bean 中获取此参数:
FacesContext.getExternalContext().getRequestParameterMap().get("myparam")

3. f:属性:

使用此标记,您可以传递属性,以便您可以从支持 bean 的 Action 监听器方法中获取该属性。

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

因此您可以从 Action 监听器方法中获取此属性:
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你可以传递对象。

关于jsf-2 - JSF 核心标签 :setPropertyActionListener vs attribute vs param,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14474484/

相关文章:

html - jsf页面中style和styleClass的区别

jsf - 在 JSF 2.0 中动态创建输入字段并将其链接到支持 bean

java - 在 JSF 2 中从同一命令调用不同的操作

jsf-2 - 如何将支持 bean 中的 FacesMessage 附加到 ui :repeat? 中的特定字段

maven - 将基于 Maven 的 JSF 项目部署到 Tomcat 导致 java.lang.ClassNotFoundException : javax. faces.webapp.FacesServlet

jsf-2 - Primefaces/JSF - 基于两个条件禁用按钮

jsf - 通过选择复选框启用和禁用组件

maven - 如何更改 ICEfaces 3 中的主题

java - 将复合组件迁移到自定义组件

jsf - 动态h :outputScript name attribute in ui:repeat