Speed ​​+ Spring

I am trying to configure webapp with the above components. I jumped over everything except the last hurdle that integrates Spring and Velocity Tools. This morning I saw this post and updated it with a slightly different answer than the one that was provided. However, as soon as I tried adding ParameterTool to one of my templates, for example:

#foreach( $key in $params.keySet() ) $key = $params.getValue($key) <br /> #end 

I get NPE java.lang.UnsupportedOperationException: Request is null. The Tool parameter must be initialized first! According to what I read, this means that the tool is configured correctly, it just does not have access to the request. Note. I also get an error message with the decision made.

Has anyone successfully been able to use these tools with Spring? This seems to be a known flaw, as there is an Open Jira for this for the Open Jira SPR-5514

+4
source share
1 answer

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 } } } 
+2
source

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


All Articles