The h:setPropertyActionListener tag adds an action listener to a component that sets a bean property to a given value.
The following code shows how to use f:setPropertyActionListener
.
<h:commandButton id="submit" action="result" value="Show Message"> <f:setPropertyActionListener target="#{userData.data}" value="JSF 2.0 User" /> </h:commandButton>
The following code is from UserBean.java.
package com.java2s.common; 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; } }
The following code is from 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>
The following code is from demo.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> <h:form id="form"> <h:commandButton action="#{user.outcome}" value="Click Me"> <f:setPropertyActionListener target="#{user.username}" value="java2s" /> </h:commandButton> </h:form> </h:body> </html>
Copy the generated WAR file from the target folder to Tomcat deployment folder and run Tomcat-Install-folder/bin/startup.bat.
After Tomcat finish starting, type the following URL in the browser address bar.
http://localhost:8080/simple-webapp/demo.xhtml