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:param
和 f:attribute
.此外,重要的是要知道 f:param
你可以通过 String
值,并使用 f:attribute
你可以传递对象。
关于jsf-2 - JSF 核心标签 :setPropertyActionListener vs attribute vs param,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14474484/