在JSF中,“ f:setPropertyActionListener ”标记允许您直接在备用bean的属性中设置一个值。 例如,
<h:commandButton action="#{user.outcome}" value="Submit">
<f:setPropertyActionListener target="#{user.username}" value="mkyong" />
</h:commandButton>
在上述JSF代码段中,如果单击该按钮,它将通过setUsername()方法将“ mkyong ”值设置为“ username ”属性。
@ManagedBean(name="user")
@SessionScoped
public class UserBean{
public String username;
public void setUsername(String username) {
this.username = username;
}
}
JSF f:setPropertyActionListener示例
好的,让我们看一下JSF 2.0中的完整示例。
1.托管豆
一个超级简单的托管bean,名为“用户”。
package com.mkyong;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
@ManagedBean(name="user")
@SessionScoped
public class UserBean{
public String username;
public String outcome(){
return "result";
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
2. JSF页面
JSF页面显示了使用“ f:setPropertyActionListener ”将值“ mkyong ”直接设置到支持bean的属性“ username ”中。
default.xhtml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
>
<h:body>
<h1>JSF 2 setPropertyActionListener example</h1>
<h:form id="form">
<h:commandButton action="#{user.outcome}" value="Click Me">
<f:setPropertyActionListener target="#{user.username}" value="mkyong" />
</h:commandButton>
</h:form>
</h:body>
</html>
result.xhtml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
>
<h:body>
<h1>JSF 2 setPropertyActionListener example</h1>
#{user.username}
</h:body>
</html>
3.演示
这是单击按钮后的结果。
下载源代码
下载它– JSF-2-setPropertyActionListener-Example.zip (9KB)
参考
标签: jsf2
翻译自: https://mkyong.com/jsf2/jsf-2-setpropertyactionlistener-example/