tags:

views:

328

answers:

1

I want to return ArrayList<HashMap<String,String>> in EL function with three String argument. How to do that?

+1  A: 

Actually I think it's perfectly reasonable to have an EL function return some complex object. Of course, there are issues of "architectural style" that might dictate what would and would not be appropriate situations for such a thing, but I'd say that a good example would be some facility for returning some kinds of configuration information that is not specific to any particular action, not really of interest to back-end business logic, and likely to be of use for presentation purposes on many pages.

To do that, what you want is an EL function that returns "Object", or perhaps "Object[]". You can't use the Java generics stuff in your EL declarations (in your .tld file, that is), but that doesn't really matter because the EL environment does type sniffing anyway. What you'd do is declare a public static function in a class somewhere:

public static Object yourFunction(String arg1, String arg2, String arg3) {
    // code code code
    return (ArrayList<HashMap<String, String>>) whatever;
}

In your .tld file, you'll have something like this:

<function>
  <description>Blah blah blah</description>
  <name>yourFunction</name>
  <function-class>your.package.YourClassName</function-class>
  <function-signature>
    java.lang.Object yourFunction(java.lang.String, java.lang.String, java.lang.String)
  </function-signature>
</function>

In your JSP, you'd access the function like this:

<c:set var='result' value='${prefix:yourFunction("Goodbye", "Mr.", "Chips")}'/>
Pointy
+1 for the introductory text. I'd however be interested what he would need it for. I can't imagine of any valid real world reason to do so. Either when the values are already hardcoded in JSP, why would you convert it to another Java object? Or when the values are dynamic, why would you not grab a servlet or bean to preprocess it?
BalusC
Well I don't know. Probably in most cases there'd be a bean to use, but who knows? The original question asked about how to set up an EL function. It sure is a lot quicker and cleaner to get hold of a bean via an EL function anyway, compared to `<jsp:useBean>', which pins down the class name in every page that uses it.
Pointy
Generics isn't supported, but your return type should be java.util.List in the function signature and List in the method signature.
BacMan