Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call a method with a parameter in JSF

Tags:

I' ve a JSF page that shows the content of a folder (really it's a dropbox's account content).

I'm using a dataTable to render the content of a ListArray object:

<h:dataTable style="text-align: left" width="600" var="dContent" value="#{backedBean.contents}">   <h:column>     <f:facet name="header">       <f:verbatim>NAME</f:verbatim>     </f:facet>     <h:commandButton value="#{dContent.fileName}" action="#{backedBean.updateContents(dContent)}"/>   </h:column>   <h:column>     <f:facet name="header">       <f:verbatim>SIZE</f:verbatim>     </f:facet>     <h:outputText value="#{dContent.size}"/>   </h:column> </h:dataTable> 

But when I run this page I obtain the following error:

/browse.xhtml @34,110 action="#{backedBean.updateContents(dContent)}" Error Parsing: #{backedBean.updateContents(dContent)}
...
...
Caused by: org.apache.el.parser.ParseException: Encountered " "(" "( "" at line 1, column 28. Was expecting one of:
"}" ...
"." ...
"[" ...
">" ...
"gt" ...
"<" ...
"lt" ...
">=" ...
"ge" ...
...
...

The funny thing is that Netbeans is able to autocomplete the method name so I image that my backend bean is ok. The problem occurs only when I call a method with a parameter.

Any ideas?

Many thanks

like image 341
Neos76 Avatar asked Mar 11 '11 13:03

Neos76


People also ask

How do you call a method in JSF?

As per the documentation of h:commandButton the action attribute should be: The action attribute accepts a method-binding expression for a backing bean action method to invoke when this component is activated by the user.

What is binding in JSF?

A JSF "binding" is a binding of the actual JSF UI component to a property. In over 99 cases out of 100, you would use the "value=" attribute, since it's only the control's backing property value you care about dealing with in the backing bean.


2 Answers

Passing method arguments was introduced in 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.

<?xml version="1.0" encoding="UTF-8"?> <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" >     <!-- Config here --> </web-app> 

If you aren't, then check this answer for alternatives with regard to obtaining current row in datatables, or this answer with regard to replacing the EL implementation by one which supports passing method arguments so that you can use it on Servlet 2.5 / EL 2.1 containers as well.

like image 177
BalusC Avatar answered Oct 10 '22 19:10

BalusC


Jboss Seam can also help to get the feature.

Seam uses JBoss EL which provides an extension to the standard Unified Expression Language (EL). JBoss EL provides a number of enhancements that increase the expressiveness and power of EL expressions.

Example:

pass literal strings using single quotes: <h:commandLink action="#{printer.println('Hello world!')}" value="Hello"/>

or for dynamic value <h:commandButton action="#{hotelBooking.bookHotel(hotel)}" value="Book Hotel"/>

Limitation:

JBoss EL can't currently be used with JSP 2.1 as the compiler rejects expressions with parameters in. So, if you want to use this extension with JSF 1.2, you will need to use Facelets. The extension works correctly with JSP 2.0.

like image 25
Nilesh J. Bhaumik Avatar answered Oct 10 '22 20:10

Nilesh J. Bhaumik