Access implicit objects from JSP EL

When defining a function for a JSP expression language, is there a way to indicate that a function requires one of the implicit objects (e.g. pageContext )?

I want to define the function ${my:href('a.jpg')} that will be implemented

 public static String href(String fileName, PageContext pageContext) 

but I don’t want me to explicitly pass pageContext to a function every time I call it.

+4
source share
3 answers

You can access "implicit" objects only if you provide their accumulation (through a filter or something) as local variables of the stream. You can then write some utility classes to get them out of any context. Some frames (like Stripes) (yaay Stripes!) Make this relatively easy.

+2
source

Consider using a tag instead of a function. Tags give you implicit access to PageContext inherited TagSupport#pageContext .

+2
source

It would be nice, but no, there is no way to do this directly. In addition, there is no workaround, as JSP EL functions must be public static . So the solution is to go back to the plain old JSP code:

 <% Helper my= new Helper (pageContext); %> 

Helper has a regular public method that takes a file name as an argument:

 public String href (String fileName) { ... } 

and later

 ... ${my.href(...)} 
+1
source

Source: https://habr.com/ru/post/1302130/


All Articles