How to call a method with parameters in JSF

The simplest (and most powerful) way to pass parameters is via JSF method expression which has been introduced in JSF 2.0 and EL 2.2. So this is only possible if you're running on a Servlet 3.0 / EL 2.2 capable container like Tomcat 7, Glassfish 3, JBoss AS 6, etc and your web.xml is been declared as per Servlet 3.0 specification.

<web-app version="3.0"  xmlns="http://java.sun.com/xml/ns/javaee"      
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"      
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" >    
</web-app> 

Here's an example which passes a static string to the confirm method:


<h:commandButton value="Confirm" action="#{imanager.confirm('name')}" />

 public void confirm(String s) {
  System.out.println("Called with parameter "+s);
 }
 
Of course you can pass also runtime attributes, for example you could add a button which identifies the row which has been clicked, by passing the "name" element of the model:
<h:dataTable value="#{imanager.cacheList}" var="item">
   <h:column>
    <f:facet name="header">Name</f:facet>
    <h:outputText value="#{item.name}" />
   </h:column>
   <h:column>
    <f:facet name="header">Show</f:facet>
    <h:outputText value="#{item.show}" />
   </h:column>
    <h:column>
    <f:facet name="header">Action</f:facet>
    <h:commandButton value="Confirm" action="#{imanager.confirm(item.name)}" />
   </h:column>       
</h:dataTable>
 


Advertisement
 

Search our tutorials