开发者

el function with generic arraylist return type and with parameter

I want to return ArrayList<HashMap&l开发者_如何转开发t;String,String>> in EL function with three String argument. How to do that?


You can use complex return types in your tld, too. E.g. 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")}'/>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜