Skip to content
Share this..

Passing Parameters with h:commandLink in JSF

2010 July 13
by Eddie

I have been growing fond of JSF as of late, but was stumped by the simplest task.

How do I associate some value with a link? If I was using PHP I would just append a query parameter and grab the value. But with JSF it’s all done inside our xhtml files, so how do we set a value based on the link clicked?

Just use

<f:setPropertyActionListener
   target="#{bean.property}" value="#{VALUE}"/>

inside the commandAction.

This stores any expression into the specified bean’s property.

See below for a full faces example. Each Player link will set the backing Bean’s player object before calling the action.

<html xmlns="http://ww.w3.org/1999/xhtml" 
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
 
pageTitle
appTitle
content
 
 
<ui:composition template="/template.xhtml">
	<ui:define name="pagetitle">Team Management</ui:define>
 
 
	<ui:define name="apptitle">Affinity Management Plus (Basketball)</ui:define>
 
	<!-- team form -->
	<ui:define name="content">
		<h2> #{team.teamName}  Player Summary</h2>
		<ul>
		<ui:repeat value="#{processor.team.players}" var="player">
                 <li> 
                      <h:outputText value="#{player.number}" /> -  <h:outputText value="#{player.name}" />
 
                 </li> 
          </ui:repeat>	
		  </ul>	
 
		<h2> #{team.teamName} Game Summary</h2>
 
		<ui:repeat value="#{processor.team.games}" var="game">
 
                 <h3>Played #{game.date} against #{game.team2.teamName}</h3>
 
				 <ui:repeat value="#{game.statEntrySetList}" var="entry">
 
					<h:form>
 
                 	<h3>Stats for <h:commandLink id="playerAction" action="#{processor.preparePlayerAction}" >
                 	#{entry.key.name}
                 	<f:setPropertyActionListener
   target="#{processor.player}" value="#{entry.key}"/></h:commandLink>  </h3>
                 	</h:form>	
					<table width="100%" border="1">
					    <tr>
					        <th>1
					        </th>
					        <th>2
					        </th>
					        <th>3
					        </th>
					        <th>fouls	
					        </th>
					        <th>steals
					        </th>
					        <th>rebounds
					        </th>
					        <th>minutes
					        </th>
					    </tr>
					    <tr>
					        <td>#{entry.value.onePointers}
					        </td>
					        <td>#{entry.value.twoPointers}
					        </td>
					        <td>#{entry.value.threePointers}
					        </td>
					        <td>#{entry.value.fouls}
					        </td>
					        <td>#{entry.value.steals}
					        </td>
					        <td>#{entry.value.rebounds}
					        </td>
					        <td>#{entry.value.minutesPlayed}
					        </td>
					    </tr>
					</table>
 
 
 
       			 </ui:repeat>
 
 
        </ui:repeat>		
	</ui:define>
 
 
</ui:composition>
 
 
 
 
 
</html>
 
 
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
 
import javax.faces.event.ActionEvent;
import javax.faces.model.SelectItem;
 
import com.libertymutual.amp.basketball.DAO.GameDAO;
import com.libertymutual.amp.basketball.DAO.PlayerDAO;
import com.libertymutual.amp.basketball.DAO.TeamDAO;
 
public class TeamProcessor {
	//Business MOdels (used for temporary associations and datastore)
	private Team team= new Team();
	private Player player=new Player();
	private Game game=new Game();
	private Statistic statistic = new Statistic();
 
	private CumulativeStatistic cumaltiveStat= new CumulativeStatistic();
 
	private Team team2= new Team();
	private List<SelectItem> opponents = new LinkedList<SelectItem>();
	private TeamDAO teamDAO = new TeamDAO();
	private PlayerDAO playerDAO = new PlayerDAO();
	private GameDAO gameDAO = new GameDAO();
 
	//authorization
	private List<User> users= new LinkedList<User>();
	private String tmpName="";
	private String tmpPass="";
	private boolean isAuthorized=false;
 
 
	//expose properties to jsf
	public Game getGame() {
		return game;
	}
 
	public void setGame(Game game) {
		this.game = game;
	}
 
	public Statistic getStatistic() {
		return statistic;
	}
 
	public void setStatistic(Statistic statistic) {
		this.statistic = statistic;
	}
 
	public List<User> getUsers() {
		return users;
	}
 
	public void setUsers(List<User> users) {
		this.users = users;
	}
 
	public String getTmpName() {
		return tmpName;
	}
 
	public void setTmpName(String tmpName) {
		this.tmpName = tmpName;
	}
 
	public String getTmpPass() {
		return tmpPass;
	}
 
	public void setTmpPass(String tmpPass) {
		this.tmpPass = tmpPass;
	}
 
	public Player getPlayer() {
		return player;
	}
 
	public void setPlayer(Player player) {
		this.player = player;
	}
 
	public  Team getTeam() {
		return team;
	}
 
	public void setTeam(Team team) {
		this.team = team;
	}
 
	public void setTeam2(Team team2) {
		this.team2 = team2;
	}
 
	public Team getTeam2() {
		return team2;
	}
 
	public List<SelectItem> getOpponents() {
		return opponents;
	}
 
	public void setOpponents(List<SelectItem> opponents) {
		this.opponents = opponents;
	}
 
	public TeamProcessor(){
		// set valid users
		User eddie = new User();
		eddie.setName("eddie");
		eddie.setPassword("1234");
		users.add(eddie);
 
 
		opponents = new LinkedList<SelectItem>();
        for (Team team : teamDAO.list()) {
            opponents.add(new SelectItem(team,team.getTeamName()));
        }
 
 
	}
 
	/*
	 * 
	 * Action Methods
	 */
 
	public String processTeam(){
		if(!isAuthorized)return "invalid-login";
		String result= "success";
		return result ;
	}
 
	public String processPlayer(){
		System.out.println(">>> processPlayer");
		if(!isAuthorized)return "invalid-login";
		String result= "error";
		if( team.addPlayer(player)){
			result="success";
			playerDAO.addPlayer(player);
			System.out.println("Added: "+player.getName());
		}
		player=new Player();
		System.out.println("<<< processPlayer");
 
		return result;
	}
 
	public String processPlayerAndAddAnother(){	
		System.out.println(">>> processPlayerAndAddAnother");
		if(!isAuthorized)return "invalid-login";
		processPlayer() ;
		System.out.println("<<< processPlayerAndAddAnother");
 
		return null;
	}
	public String processPlayerDoner(){	
		return "success";
	}
 
 
	public String loginAction(){
		String result="invalid-login";
		for(User user : users){
			if(tmpName.equals(user.getName())
					&&
			tmpPass.equals(user.getPassword())){
				result = "success";
				isAuthorized=true;
			}
		}
		return result;
	}
 
	public String processGame(){
		System.out.println(">>> processGame");
		String result="error";
		game.setTeam1(this.team);
		game.setTeam2(team2);
		if(team.addGame(game)){
			gameDAO.addGame(game);
			result="success";
			game=new Game();
		}
 
		System.out.println("<<< processGame:"+result);
 
		return result;
	}
 
	public String processGameStats(){
 
		game.addGameStat(player, statistic);
		System.out.println(">>> processGameStats: " + game.getTeam2().getTeamName()+player.getName()+statistic.getFouls());
		player=new Player();
		statistic=new Statistic();
		game=new Game();
		return null;
 
	}
 
	public String finishGameStats(){
		processGameStats();
		return "success";
	}
 
	public String preparePlayerAction(){
		String result = "error";
		System.out.println("playerAction>>>");
 
		System.out.println("Player:"+player.toString());
		System.out.println("team:"+team.toString());
		for( Game game : team.getGames()){
			Statistic stat = game.getGameStatistic(player);
			if (stat != null){
				cumaltiveStat.addStatistic(stat);
			}
			result="success";
		}		
		System.out.println("CS:"+cumaltiveStat.getAverage(cumaltiveStat.getFouls()));
		System.out.println("playerAction<<<");
		return result;
	}
 
	public void preparePlayerListener(ActionEvent ae){
		System.out.println("playerListener>>>");
		for (Iterator iterator = ae.getComponent().getAttributes().entrySet().iterator(); iterator.hasNext();) {
			Object o = iterator.next();
			System.out.println(o.toString());
 
		}
		System.out.println("Player:"+player.toString());
		System.out.println("playerListener<<<");
	}
 
	public CumulativeStatistic getCumaltiveStat() {
		return cumaltiveStat;
	}
 
	public void setCumaltiveStat(CumulativeStatistic cumaltiveStat) {
		this.cumaltiveStat = cumaltiveStat;
	}
 
 
 
 
 
 
 
 
}
No comments yet

Leave a Reply

Note: You can use basic XHTML in your comments. Your email address will never be published.

Subscribe to this comment feed via RSS