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>
Recommended Articles
JSF Data Validation Tutorial: Enhance Your Applications with JSF 2.0
Learn how to leverage JSF validation for cleaner, more efficient applications. #Java #Middleware #CloudNative
Create a Custom JSF Converter for User Object - Jakarta Server Faces (JSF) Tutorial
Learn how to create a custom JSF converter for User objects in Java applications using WildFly 31 and Java 21. #Java #Middleware #CloudNative
Create Java-Based JSF Custom Tags with Ease: A Step-by-Step Guide
Learn how to create Java-based JSF custom tags in just three simple steps, simplifying the process from early JSF 1.2 to modern specifications.
Comparing Object Storage Options for Web Applications with JSF 2 and CDI
Explore JSF 2 scopes and CDI extensions for storing object data in web applications. #JSF #CDI #WebDevelopment