简体   繁体   中英

el function with generic arraylist return type and with parameter

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

You can use complex return types in your tld, too. Eg this will work:

public static List<Map<String, String>> func(String arg1, String arg2,
        String arg3) {

    List<Map<String, String>> out = new ArrayList<HashMap<String, String>>();
    // code code code
    return out;
}

In your .tld file, you'll have to put this:

<function>
  <description>Blah blah blah</description>
  <name>func</name>
  <function-class>your.package.YourClassName</function-class>
  <function-signature>
    java.util.List&lt;java.util.Map&lt;java.lang.String,java.lang.String&gt;&gt; func(java.lang.String,java.lang.String,java.lang.String)
  </function-signature>
</function>

Caveats: As in this example, the angle brackets must be properly escaped in XML. The function signature must not be line wrapped . Non-unary generics, such as Map<String,String> , cannot be used as parameters . (Probably a tokenization bug.) You will have to go with the raw types there.

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")}'/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM