My JSF Books/Videos My JSF Tutorials OmniFaces/JSF PPTs
JSF 2.3 Tutorial
JSF Caching Tutorial
JSF Navigation Tutorial
JSF Scopes Tutorial
JSF Page Author Beginner's Guide
OmniFaces 2.3 Tutorial Examples
OmniFaces 2.2 Tutorial Examples
JSF Events Tutorial
OmniFaces Callbacks Usages
JSF State Tutorial
JSF and Design Patterns
JSF 2.3 New Features (2.3-m04)
Introduction to OmniFaces
25+ Reasons to use OmniFaces in JSF
OmniFaces Validators
OmniFaces Converters
JSF Design Patterns
Mastering OmniFaces
Reusable and less-verbose JSF code

My JSF Resources ...

Java EE Guardian
Member of JCG Program
Member MVB DZone
Blog curated on ZEEF
OmniFaces is an utility library for JSF, including PrimeFaces, RichFaces, ICEfaces ...

[OmniFaces Utilities] - Find the right JSF OmniFaces 2 utilities methods/functions

Search on blog

Petition by Java EE Guardians

Twitter

luni, 8 februarie 2016

[JSF Page Author Beginner's Guide] JSF <f:setPropertyActionListener/>

The <f:setPropertyActionListener/> tag uses an action listener to directly set a value into a managed bean property
The <f:setPropertyActionListener/> tag uses an action listener (created by the framework) to directly set a value into a managed bean property; it is placed within a component derived from the ActionSource class. The target attribute indicates the managed bean property, while the value attribute indicates the value of the property.

Note Keep in mind that action listeners are executed in the order they are defined, which means that the presence of the <f:setPropertyActionListener/> tag can affect the order in which the listeners are fired.

Common/basic usage in JSF - set a static text to a managed bean property

Suppose we have the following managed bean:

@Named
@ViewScoped
public class PlayerBean implements Serializable {

 private static final long serialVersionUID = 1L;

 private String playerName = "Rafael Nadal";

 public PlayerBean() {
 }

 // Getters and setters

 // dummy action
 public void action(){
  System.out.println("Name set: " + playerName);
 }
}

In order to set a static text from a Facelets page to a managed bean property we can write:

<?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://xmlns.jcp.org/jsf/html"
 xmlns:f="http://xmlns.jcp.org/jsf/core">
 <h:head>
  <title>JSF setPropertyActionListener examples</title>
 </h:head>
 <h:body>
  <h:form>
   <h:commandButton value="Set name" action="#{playerBean.action()}">
    <f:setPropertyActionListener value="Roger Federer" target="#{playerBean.playerName}"/>
   </h:commandButton>
  </h:form>
  Player name: <h:outputText value="#{playerBean.playerName}"/>
 </h:body>
</html>

Data flow in image:
More examples:

Set a managed bean property value to another property value from the same bean

@Named
@ViewScoped
public class PlayerBean implements Serializable {

 private static final long serialVersionUID = 1L;

 private String playerName = "Rafael Nadal";
 private Player player;

 public PlayerBean() {
  player = new Player();
 }

 // Getters and setters
}

Where Player is the following object:

public class Player implements Serializable {

 private String name;

 public Player() {
 }

 public Player(String name) {
  this.name = name;
 }

 // Getter and setter

 @Override
 public String toString() {
  return "Player{" + name + '}';
 }
}

We can write:

<h:form>
 <h:commandButton value="Set name">
  <f:setPropertyActionListener value="#{playerBean.playerName}"
                               target="#{playerBean.player.name}"/>
 </h:commandButton>
</h:form>
Player name: <h:outputText value="#{playerBean.player.name}"/>

Set a property value from a managed bean to a property value in another managed bean

Let's suppose that we have two managed beans (PlayerBean and TournamentBean):

@Named
@ViewScoped
public class PlayerBean implements Serializable {

 private static final long serialVersionUID = 1L;

 private String playerName = "Rafael Nadal";

 public PlayerBean() {
 }

 // Getter and setter
}

@Named
@ViewScoped
public class TournamentBean implements Serializable {

 private static final long serialVersionUID = 1L;

 private Player registeredPlayer;

 public TournamentBean() {
  registeredPlayer = new Player();
 }

 // Getter and setter
}

And, we want to pass the playerName property value from PlayerBean bean to be the name value of the registeredPlayer property from the TournamentBean bean:

<h:form>
 <h:commandButton value="Set name">
  <f:setPropertyActionListener value="#{playerBean.playerName}"   
                               target="#{tournamentBean.registeredPlayer.name}"/>
 </h:commandButton>
</h:form>
Player name: <h:outputText value="#{tournamentBean.registeredPlayer.name}"/>

Remove an item from a list of items

Because <f:setPropertyActionListener/> allows you to set a value directly in your managed bean before executing an action method, it is commonly used in combination with <h:commandButton/> in order to set the user selection from a list of multiple options before further processing. So, having the following managed bean:

@Named
@ViewScoped
public class TournamentBean implements Serializable {

 private static final long serialVersionUID = 1L;

 private Player selectedPlayer;
 private List<Player> players;

 public TournamentBean() {
  selectedPlayer = new Player();
  players = new ArrayList<>(Arrays.asList(new Player("Rafael Nadal"), new Player("Novak Djokovic"),
  new Player("Roger Federer"), new Player("Andy Murray"), new Player("David Ferrer")));
 }

 // Getters and setters

 public void removePlayer() {
  players.remove(selectedPlayer);
  System.out.println("Removed " + selectedPlayer);
 }
}

Where Player is the following object:

public class Player implements Serializable {

 private String name;

 public Player() {
 }

 public Player(String name) {
  this.name = name;
 }

 // Getter and setter

 @Override
 public String toString() {
  return "Player{" + name + '}';
 }
}

If we want to display the above players in a list and allow the user to remove players one by one, we can write the following Facelets page:

<h:form>
 <ul style="list-style:none;">
  <ui:repeat value="#{tournamentBean.players}" var="player">
   <li>
    <h:commandLink action="#{tournamentBean.removePlayer()}">
     <h:graphicImage name="imgs/del.png" title="Remove"/>
      <f:setPropertyActionListener value="#{player}"
                                   target="#{tournamentBean.selectedPlayer}"/>
     </h:commandLink>
     #{player.name}
    </li>
   </ui:repeat>
   </ul>
  </h:form>
 </h:body>
</html>

Each time a user clicks on the delete icon, the <f:setPropertyActionListener/> tag sets the managed bean selectedPlayer property with the player value associated to the selected icon. This is happening before the action method is called. Afterwards, the <h:commandLink/> causes the execution of the removePlayer() action method. This method searches for selectedPlayer set via <f:setPropertyActionListener/> and remove it.  If we check the console output, we should see the item which was removed from list:

E.g.   Removed Player{Novak Djokovic}

Complete source code on GitHub.
See also Mkyong.com.
More resources on Constantin Alin, ZEEF page.

Niciun comentariu :

Trimiteți un comentariu

JSF BOOKS COLLECTION

Postări populare

Follow by Email

Visitors Starting 4 September 2015

Locations of Site Visitors