45

How to call a Java method with arguments which is defined in Java class, from JSP using JSTL/EL. The method is returning arrays. Any return value can be used.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
sachin gk
  • 585
  • 3
  • 6
  • 11

4 Answers4

65

You can only invoke methods with arguments in EL if you're targeting and running at least a Servlet 3.0 compatible container (e.g. Tomcat 7 or newer, WildFly 8 or newer, GlassFish 3 or newer, etc) with a web.xml declared conform at least Servlet 3.0. This servlet version comes along with EL 2.2 which allows invoking arbitrary instance methods with arguments.

Assuming that you've a ${bean} in the scope which refers to an instance of a class which has a method something like public Object[] getArray(String key), then you should be able to do this:

<c:forEach items="${bean.getArray('foo')}" var="item">
    ${item} <br />
</c:forEach>

or even with another variable as argument

<c:forEach items="${bean.getArray(foo)}" var="item">
    ${item} <br />
</c:forEach>

But if you don't target a Servlet 3.0 container, then you cannot invoke methods with arguments in EL at all. Your best bet is to just do the job in the preprocessing servlet as suggested by Duffymo.

Object[] array = bean.getArray("foo");
request.setAttribute("array", array);
// ...

As a completely different alternative, you could create an EL function which delegates the method call. You can find a kickoff example as option 2 of this answer How to call a static method in JSP/EL? You'd like to end up something like as:

<c:forEach items="${util:getArray(bean, 'foo')}" var="item">
    ${item} <br />
</c:forEach>

with

public static Object[] getArray(Bean bean, String key) {
    return bean.getArray(key);
}

The web.xml file should absolutely not have a <!DOCTYPE> line in top as that would otherwise still force the Servlet 2.3 modus. You can find examples of proper web.xml declarations in the second half of this answer How to install JSTL? The absolute uri: http://java.sun.com/jstl/core cannot be resolved.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • 1
    I'm doing something similar with the request object, invoking `isUserInRole('role')` but eclipse think this is a get method and complains about the method `getIsUserInRole()` not existing. Do you know how this can be fixed? I'm using glassfish v3. Thanks, D. – Daniel Feb 14 '13 at 18:22
  • 2
    @Daniel: Eclipse is weak as to EL syntax checking and validation. Just disable EL validation in Eclipse, or install JBoss Tools. – BalusC Feb 14 '13 at 18:28
  • Just noticed that the link of Hidden features of JSP/Servlet has been deleted =\ – Luiggi Mendoza Jul 17 '13 at 05:51
  • @Luiggi: I have a copy in my blog, I fixed the link. Thanks! – BalusC Jul 17 '13 at 10:52
  • How would it be used with a model attribute as argument? I'm quite new at JSTL and trying to do something similar. Thanks! – Diana Amza Dec 02 '14 at 13:10
4

The above solution didnt work for me. I had a function getRemitanceProfileInformation(user) in my java class. I created a usebean of java class and then invoked

<c:set var="paymentValueCode" value='remittanceaddr.getRemitanceProfileInformation("${user}")'/>

and it worked.

Charles Menguy
  • 40,830
  • 17
  • 95
  • 117
2

Give the JSP a reference to an instance of the class that has the method and call it.

You're probably asking who gives the JSP that instance - it's a servlet in the model-2 MVC arrangement.

Here's how the flow will work:

  1. Submit a GET/POST request from a JSP to a servlet.
  2. Servlet acts on that request and does some work on the JSP's behalf. Puts all the necessary objects into request, session, or other appropriate scope.
  3. Servlet routes response to the next JSP, which might be the same as the requesting JSP.
  4. Rinse, repeat.
duffymo
  • 305,152
  • 44
  • 369
  • 561
0

If you're using JSF, you can use an bean act as a model in View Scope, and load from data source automatic. And if you're using JSP, how about using TLD Tag? And using JSTL tag <c:foreach> ? It's saves the memory from saving in the session, or save in session and remove it when load event done? Some how like this (JSTL+TLD)

<c:forEach items="${myTag:getProductByPage(page)}" var="p">
     Product name: ${p.productName}
</c:forEach>
BachT
  • 1,058
  • 9
  • 17
  • I am using struts tiles.could you please elebrote it yor explanation, more over we need to call a metod which accepts a parameters, here it will be ollean values set in JSP. – sachin gk Aug 19 '11 at 13:40