How can I access the JSP context inside a custom EL function.
Access JSP Context inside custom EL function
What do you mean by "custom EL function"? –
Succussion
@skaffman: see section "EL functions" of #2523930. @Viren: what exactly do you need the JSP context for? I.e. what methods/information would you like to call/obtain? In any case, the answer would be: just pass exactly that information as function argument. –
Miscellany
You have to explicitly include it as an argument to the method that implements your EL function.
Java method that implements EL function:
public static Object findAttribute(String name, PageContext context) {
return context.findAttribute(name);
}
TLD entry for EL function:
<function>
<name>findAttribute</name>
<function-class>kschneid.Functions</function-class>
<function-signature>java.lang.Object findAttribute(java.lang.String, javax.servlet.jsp.PageContext)</function-signature>
</function>
Usage in JSP:
<%@ taglib prefix="kfn" uri="http://kschneid.com/jsp/functions" %>
...
<c:if test="${empty kfn:findAttribute('userId', pageContext)}">...</c:if>
Thanks a lot. The example is great, exactly what I wanted. –
Swee
Or you can use a sophisticated trick. If you are OK with the ServletContext
rather than PageContext
it will be much easier
- In your EL function class, define a static
ThreadLocal<PageContext>
variable - From a custom filter, set that PageContext
- Access freely from your EL function
Code example:
public class MyFunctions {
private static final ThreadLocal<ServletContext> servletContext = new ThreadLocal<>();
public static void setServletContext(ServletContext servletContext) {
MyFunctions.servletContext.set(servletContext);
}
}
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException{
...
MyFunctions.setServletContext(servletRequest.getServletContext());
filterChain.doFilter(servletRequest, servletResponse);
}
If you really need PageContext
better do that setPageContext
in a JSP scritplet, possibly in an inclusion file. This has the drawback that EVERY JSP file must perform that inclusion
Idea is good, but approach has serious bugs. Please test/clarify/fix it. –
Miscellany
I was going to implement and test, but I rather found another strategy (via Spring beans). So if you could kindly illustrate a few of the bugs I may review my code (or delete it completely leaving the idea only). The basics is that I assume that the entire JSP request lifecycle happens in a single thread –
Dollar
Main oversight here is the fact that threads are pooled (and thus reused in different requests). –
Miscellany
Ok, good point. I am assuming that the filter is run every time a request is dispatched. So I expect the runtime to store the context in the correct threadlocal and then make it accessible to the EL function. The next request will pass by the filter again, thus overwriting the context. However no one else is caring about the current thread, because previous request ended. Thread pooling does not mean allowing a thread to serve multiple requests simultaneously. It means reusing old threads that will perform one task at once –
Dollar
© 2022 - 2024 — McMap. All rights reserved.