JSF 2 setPropertyActionListener Tag

JSF 2 setPropertyActionListener Tag


Posted in : Java Posted on : June 13, 2012 at 11:35 AM Comments : [ 0 ]

In this tutorial you will learn about the JSF 2 f:setPropertyActionListener.

JSF 2 setPropertyActionListener Tag

In this tutorial you will learn about the JSF 2 f:setPropertyActionListener.

This tag provides the facility to set the value of property of backing bean.

Attributes of f:setPropertyActionListener

  • value : This is a required attribute. This attribute is used to set the value of backing bean property.
  • target : This is a required attribute. This attribute is used to specify the destination of the value attribute.
  • for : This is not a required attribute. This attribute is used to refer the exposed objects of the composite component within which this tag is nested.

Example :

Here I am giving a simple example of JSF f:setPropertyActionListener tag. This example will demonstrate you about how to set the value of the backing bean property at run time. To do so at first I have created a Java Bean class with the 'name' property and its setter getter method. And the JSF pages to create a JSF view. In input.xhtml page I have used the <h:commandButton> and used the <f:setPropertyActionListener> tag inside it to where specified the destination of the value to be set using the 'target' attribute and set the value of backing bean property by the 'value' attribute. This process will occurred after clicking on button. And in the output.xhtml page I have simply used the <h:outputText> to show the value of backing bean property that is set by the <f:setPropertyActionListener> tag.

Directory Structure

NameBean.java

package devmanuals;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.AbortProcessingException;
import javax.faces.event.ActionEvent;
import javax.faces.event.ActionListener;

@ManagedBean(name="nameBean")
@RequestScoped

public class NameBean {

String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}

input.xhtml

<!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">

<head>
<title>Input page</title>
</head>
<body>
<f:view>
<h:form>
Click on submit button : <br/>
<h:commandButton action="output" value="submit">
<f:setPropertyActionListener target="#{nameBean.name}" value="deepak" />
</h:commandButton>
</h:form>
</f:view>
</body>
</html>

output.xhtml

<!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">

<head>
<title>Output Page</title>
</head>
<body>
<f:view>
Hello, <h:outputText value="#{nameBean.name}"/>!
</f:view>
</body>
</html>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>jsfSetPropertyActionListener</display-name>
<welcome-file-list>
<welcome-file>jsfPages/input.xhtml</welcome-file> 
</welcome-file-list>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
<url-pattern>*.jsf</url-pattern>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<context-param>
<description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>client</param-value>
</context-param>
<context-param>
<param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
<param-value>resources.application</param-value>
</context-param>
<listener>
<listener-class>com.sun.faces.config.ConfigureListener</listener-class>
</listener>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.faces</url-pattern>
</servlet-mapping>
</web-app>

How to Run

At first start your web server then use this link http://localhost:8080/jsfSetPropertyActionListener/jsfPages/input.jsf in the address bar of your web browser.

Output

When you will execute the above example you will get the output as follows :

When you will click on button named 'submit' then the output will be as follows :

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics