JBoss application server tutorials

  • Full Screen
  • Wide Screen
  • Narrow Screen
  • Increase font size
  • Default font size
  • Decrease font size

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="<a href="http://java.sun.com/xml/ns/javaee">http://java.sun.com/xml/ns/javaee</a>"      
xmlns:xsi="<a href="http://www.w3.org/2001/XMLSchema-instance">http://www.w3.org/2001/XMLSchema-instance</a>"      
xsi:schemaLocation="<a href="http://java.sun.com/xml/ns/javaee">http://java.sun.com/xml/ns/javaee</a> 
<a href="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd</a>" >    
</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>
 <

How to call a method with parameters in JSF
Francesco Google+
Top Programming Sites
You are here Java EE JSF How to call a method with parameters in JSF