What Seam (and its Extended EL) could do a long time ago standard EL can do now, too: calling methods with parameters. Assuming you have the following bean:
public class MyBean {
public String sayHello(final String name){
return "Hello " + name;
}
}
If you put <h:outputText value=”#{myBean.sayHello(‘Daniel’)}”/> somewhere in your JSF page the output ist not a TargetInvocationException or a MethodNotFoundException but indeed a “Hello Daniel”. But with one precondition: you have to use Facelets and not JSP (which should already do). Why is described by Ryan Lubke.
If you’re using Tomcat 6 be aware that it brings its own el-api.jar so you have to replace it in the lib folder.
UPDATE: As admitted in the comments I hadn’t tested the stuff I wrote above. The steps described here aren’t enough, so I’m writing it again so that it works.
- Download the two SNAPSHOT jars (el-api and el-ri) from the location posted in Ryan’s blog entry
- Put el-api-2.1.2-SNAPSHOT.jar into <tomcat root>/lib and remove the existing el-api.jar (please make an backup)
- Put el-impl-2.1.2-SNAPSHOT.jar into the WEB-INF/lib folder of your webapp (or let Maven do this for you
) - An now the most important part: Tell JSF to use the ExpressionFactory of the RI by adding the following lines to the web.xml:
<context-param> <param-name>com.sun.faces.expressionFactory</param-name> <param-value>com.sun.el.ExpressionFactoryImpl</param-value> </context-param>
I missed number 4 so JSF always uses org.apache.el.ExpressionFactoryImpl as the default ExpressionFactory. But if you follow these 4 steps everything should work. At least it works for me (Mojarra 2.0.1 and Tomcat 6.0.20).
#1 by Stefan on November 25, 2009 - 4:47 pm
Hi,
this looks great, but could you shed some light the tomcat el jar? Replace, with what from where ?
My Web-App uses JSF 2.0, I am running on Tomcat 6 but I keep getting a parsing Error. What exactly is to be done ?
Thanks,
Stefan
#2 by Daniel on November 26, 2009 - 8:26 am
Ok,
I have to admit that I haven’t really tested it but it looks like I should have.
The code mentioned above does not work with Tomcat since it always uses the jasper-el.jar which does not support parameters in methods.
Guess you have to wait for Tomcat 7 to use this feature. Or ask a question on the Tomcat users list.
#3 by Stefan on November 26, 2009 - 6:13 pm
Hi,
thanks for that hint with the ExpressionFactory contextParam, I can improve on your solution:
In a production system with several web-apps running in the tomcat and a sysadmin keeping the tomcat up-todate using os-tools, it is quite undesirable for a web app to require a change in the tomcat/lib dir.
If you include the mentionend jars with your webapp and set the context parameter, no change to the tomcat lib is required. Works for me on tomcat 6.0.18.
Thanks,
Stefan
#4 by Hendy Irawan on February 17, 2010 - 4:40 pm
Thanks Daniel!
This is the best tutorial there is to get JSF 2.0 (with Facelets and new EL) running properly under Tomcat!
Great job
#5 by Christian on July 28, 2010 - 9:34 am
Hi,
the four steps documented by Daniel worked very well for me (Mojarra 2.0.2, Tomcat 6.0.26) – Thanks a lot, Daniel!
Stefan: I would appreciate your improvement very much, because of the reasons you mentioned! Just how did you get it working?
I tried deploying both jars (el-api + el-impl) to the WEB-INF/lib folder of my webapp. The result:
java.lang.LinkageError: loader constraint violation: when resolving interface method “javax.servlet.jsp.JspApplicationContext.getExpressionFactory()Ljavax/el/ExpressionFactory;” the class loader (instance of org/apache/jasper/servlet/JasperLoader) of the current class, org/apache/jsp/index_jsp, and the class loader (instance of org/apache/catalina/loader/StandardClassLoader) for resolved class, javax/servlet/jsp/JspApplicationContext, have different Class objects for the type javax/el/ExpressionFactory used in the signature
Any hints would be very appreciated.
Regards,
Christian
#6 by Tomek on June 14, 2011 - 2:31 am
Thx a lot! In my case it was even not necessary to put any changes in web.xml file. Your solutions works great with EL Argument passing what was a huge problem in my project.