A slightly modified version of the accepted answer from this question solves this problem.
Instead of returning ViewContext, you need to return ViewToolContext. You also need to prepare toolbars and install them in the session / query, if applicable:
You will need to initialize the toolContext depending on what you need (look at my provided answer here on how to do this with updated APIs, as you will need access to ToolboxFactory.
Now, the modified createVelocityContext method should prepare the toolbars before creating the ViewToolContext as follows:
protected Context createVelocityContext(Map <String, Object> model, HttpServletRequest request, HttpServletRespsone response) throws Exception { initVelocityContext(); //Still keep toolContext static //will need to also add this to //the servletContext -- left as an exercise prepareToolboxes(request, response); Context context = new ViewToolContext(getVelocityEngine(), request, response, getServletContext()); //Set model attrs to context .... return context; } private void prepareToolboxes(final HttpServletRequest request, final HttpServletResponse response) { String key = Toolbox.class.getName(); if (factory.hasTools(Scope.REQUEST && request.getAttribute(key) == null) { Toolbox requestTools = factory.createToolbox(Scope.REQUEST); request.setAttribute(key, requestTools); } if (factory.hasTools(Scope.SESSION) { HttpSession session = request.getSession(); synchronized(factory) { //Follow pattern from above } } }
Scott source share