1/10/2011

How to pass Parameters and Attributes in JSF / ICEFaces and Liferay

Introduction


I had a couple of people visiting my blog just to find out how to pass parameters by commandButton, commandLink, menuItem etc. So I decided to write it down a little. I´m sure I will need it myself sooner or later ;)


The first thing you have to differentiate is whether you want to send parameters or attributes. While Parameters can be accessed by every component, attributes can only be read by the method that receives the enclosing ActionEvent, because the attribute is mapped to the component that fire the event. This means that attributes can only be used together with ActionListeners - not actions.


A parameter, in contrast is not bound to a specific component. It is an http fragment added to specific components. Those components generate an http URL like http://localhost:8080?id=123 where id is the parameter.
There are only two components that will work with parameters f:commandLink and f:outputLink. ICEFaces adds ice:menuItem to this list. For all the other components you will have to use attributes. 


Accessing Parameters (f:param)




Here are some examples on how to use parameters:


ice:menuItem


The ICEFaces menuItem allows to pass parameters either by a standard jsf f:param tag or by a link. But sadly - not both.



<ice:menuItem value="clickMe" actionListener="#{bean.listener}">
<f:param name="id" value="#{entry.id}" />
<f:param name="action" value="createNew" />
</ice:menuItem>


<ice:menuItem value="clickMe"
         link="localhost:8080/servlet?id=#{entry.id}">
</ice:menuItem>



f:commandLink

A commandLink renders an href element that behaves like a form submit button and is able to call actions or actionListeners:


<h:commandLink value="link" action="#{backingBean.myaction}">
        <f:param name="parameter1" value="value1" />                      
        <f:param name="parameter2" value="value2" />                      
</h:commandLink>



f:outputLink

The outputLink Tag renders a standard html href tag that fires a standard GET request to a web ressource. You could use it to call Java Servlets for example.

<h:outputLink value="#{bean.linkToServlet}">
    <h:outputText value="Click me"/>
    <f:param name="parameter" value="parameterValue" />    
</h:outputLink>




Parameter can be programatically accessed in the following way :





public static String getParameter(String parameterName) {


  String parameter = FacesContext.getCurrentInstance()
.getExternalContext().getRequestParameterMap()
.get(parameterName);
  return parameter;
}


Accessing Attributes (f:attribute)


Attributes can be used (for example) with commandLinks or commandButtons. Hint: commandButtons can´t use parameters ! If you have to use parameters (maybe in navigation), add them manually in an action of yours that you call by clicking your button.


The usage is pretty simple:


<ice:commandButton id="myButton" actionListener="#{bean.ac}"> 
    <f:attribute name="name" value="Click Me"></f:attribute> </h:commandButton>

Attributes can be accessed programatically in the following way:

public void ac(ActionEvent e) {


  Map<String, Object> attributes = e.getComponent().getAttributes();
  Object object = attributes.get("name");
  [...]



If you liked this tutorial it would be very nice, if you could click on some of the google ads you see on the right side. It helps me run this block and motivates me ;)

If you have any questions, feel free to leave a comment.