Servlet集成

只需要在Servlet代码里引用ServletGroupTemplate就能集成Beetl,他提供了一个render(String child, HttpServletRequest request, HttpServletResponse response)方法。例子如下:

  1. protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
  2. response.setContentType("text/html;charset=UTF-8");
  3. //模板直接访问users
  4. request.setAttribute("users",service.getUsers());
  5. ServletGroupTemplate.instance().render("/index.html", request, response);
  6. }

ServletGroupTemplate同其他web集成一样,将读取配置文件来配置,如果需要通过代码配置,可以在Servlet listener里 ServletGroupTemplate.instance().getGroupTemplate()方法获取GroupTemplate