Domanda

I am using Apache Tomcat with Velocity and VelocityViewServlet. I have created a custom tool with refference to ViewContext. It all works well.

The question is: what is best way to locate/load template and procces it with suplied parameters?

I have already absolute path to the file obtained via

((ViewContext)context).getRequest().getSession().getServletContext().getRealPath("/")
  • Do I have to instantiate VelocityEngine? I suppose there is no global maintained by Velocity (VelocityViewServlet)
  • Which (and how) of Velocity loaders is best to use?
È stato utile?

Soluzione

Several points here:

  • The VelocityViewServlet will instantiate itself a VelocityEngine. It's not global, it's one engine per ServletContext.

  • The VelocityViewSerlet will locate itself the template that corresponds to the request URI using its default loader (WebappLoader), so you don't have to do it yourself either.

  • The Velocity context your template will be evaluated with, will already be populated with all standard tools (for tools 2.0), among which $params which allows you to inspect HTTP parameters.

  • I don't understand "a custom tool with refference to ViewContext": instead of using the ViewContext, you should add to your custom tool all the appropriate setters you need among the properties listed here (for instance, if you need access to the request, then you'll declare a "public setRequest(HttpServletRequest request)" method). Remember that from a bottom-up perspective, your tool must be only reachable via a key that you choose for it in your tools configuration file, and should not be aware of Velocity.

  • I advise you to use VelocityTools 2.0, which is a more mature library than the 1.x.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top