When defining a function for the JSP expression language, is there a way to specify that the functio开发者_开发问答n requires one of the implicit objects (such as pageContext
)?
I want to define a function ${my:href('a.jpg')}
to be implemented by
public static String href(String fileName, PageContext pageContext)
but I do not want to have to explicitly pass the pageContext to the function every time I call it.
You can access the "implicit" objects only if you provide for stashing them (via a filter or something) as thread local variables. Then you can write some utility classes to go get them from whatever context makes sense. Some frameworks (Stripes, for example) (yaay Stripes!) make that relatively easy.
Consider using a tag instead of a function. Tags provides you implicit access to the PageContext
by the inherited TagSupport#pageContext
.
It would be nice but no, there is no way to do that directly. Also, there is no workaround since JSP EL functions must be public static
. So the solution is to go back to plain old JSP code:
<% Helper my= new Helper (pageContext); %>
Helper
has a normal public method that takes a filename as argument:
public String href (String fileName) { ... }
and later
... ${my.href(...)}
精彩评论