Last updated Dec 8, 2017

Rendering a velocity template from your Fisheye servlet

Velocity allows your Servlet Plugins to render HTML pages from simple templates.This tutorial briefly shows the basics of using velocity. The Fisheye Twitter Integration Plugin Tutorial shows velocity being used to generate a form for maintaining user preferences as part of a larger plugin.

Velocity templates are placed in the resources part of your plugin project, i.e. myplugin/src/main/resources/mytemplate.vm.

Your servlet uses Velocity like this:

1
2
public class MyServlet extends HttpServlet {
    private final VelocityHelper velocityHelper;

    @Autowired
    public MyServlet(VelocityHelper velocityHelper) {
        this.velocityHelper = velocityHelper;
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Map<String,Object> params = new HashMap<String,Object>();

        params.put("first", "Hello");
        params.put("second", "World");
        velocityHelper.renderVelocityTemplate("mytemplate.vm", params, response.getWriter());
    }
}

where mytemplate.vm contains:

1
2
<html>
<head>
</head>
<body>
  ${first}, ${second}.
</body>
</html>

In order to render the text "Hello, World.".

Rate this page: