How to add custom VariableResolver to pure JSP

I would like to programmatically add a custom VariableResolver when JSF or something similar is not used, so before searching for beans in scope, as indicated in 1 , EL will first try to resolve the variables inside it.

The goal is that the objects in the database that are accessible in EL are right by their names, and there could be many to put them in scope.

Currently, I put a special bean in the session area under the name, say, "z", and that the bean extends the map interface and thus allows access to objects with an expression like $ {z.address} $ {z.fullName}. I am trying to exclude this "z".

It’s better if I manage to insert my resolver in the Filter (of course, I will check to not do this several times for each web request)

(Edit: maybe I'm talking about ELContext, how to put my own VariableMapper or ELResolver there or something like that)

+3
source share
2 answers

You can register your own ELResolveras follows.

@WebListener
public class Config implements ServletContextListener { 

    @Override 
    public void contextInitialized(ServletContextEvent event) {
        ServletContext servletContext = event.getServletContext(); 
        JspApplicationContext jspContext = JspFactory.getDefaultFactory().getJspApplicationContext(servletContext); 
        jspContext.addELResolver(new YourELResolver()); 
    } 

    // ... 
}

Note: Servlet 2.5 / JSP 2.1 or later is required for this approach.

+6
source

I am going to wrap a request object, override the getAttribute method, where I can return the desired value.

In normal situations, when the attribute is not found by name in the context of the request, the servlet container goes into the application and session context. Some useful options saved.

, , , ; , .

, ,

+1

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


All Articles