开发者

Accessing the implicit objects from a JSP EL function

开发者 https://www.devze.com 2022-12-21 04:15 出处:网络
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)?

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(...)}
0

精彩评论

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

关注公众号