コード例 #1
0
  private boolean render(
      FileInfo fileInfo,
      Map<String, Object> model,
      Request request,
      HttpServletRequest servletRequest,
      HttpServletResponse servletResponse,
      Writer writer)
      throws IOException {

    servletResponse.setContentType("text/html");

    String path = fileInfo.getPath();
    File localFile = fileInfo.getFile();
    if (model == null) {
      model = new HashMap<String, Object>();
    }

    model.put("request", request);
    model.put("servletRequest", servletRequest);

    Template template;
    try {
      template = templateConfig.getTemplate(path);
    } catch (IOException e) {
      String msg = "problems getting template from path '" + path + "'";
      logger.error(msg, e);
      throw new IOException(msg, e);
    }

    try {
      template.process(model, writer);
      return true;
    } catch (TemplateException e) {
      String msg = "could not render template in " + localFile;
      logger.error(msg, e);
      throw new IOException(msg, e);
    } finally {
      // don't close it here
      writer.flush();
    }
  }
コード例 #2
0
 @Override
 public boolean renderResult(
     Request request,
     HttpServletRequest servletRequest,
     HttpServletResponse response,
     Object result)
     throws IOException {
   ModelView modelView;
   FileInfo fileInfo;
   if (result instanceof ModelView) {
     modelView = (ModelView) result;
     String view = modelView.getView();
     if (view.startsWith("redirect:")) {
       String dest = view.substring(9);
       if (dest.startsWith("http:") || dest.startsWith("https:")) {
         ResponseUtils.sendRedirect(response, dest);
       } else {
         ResponseUtils.sendRelativeRedirect(servletRequest, response, dest);
       }
       return true;
     }
     fileInfo = fileLocator.findFile(view);
     if (fileInfo == null) {
       logger.error("template not found in path '{}'", view);
       return false;
     }
   } else if (result instanceof FileInfo) {
     fileInfo = (FileInfo) result;
     modelView = new ModelView(new HashMap<String, Object>(), fileInfo.getPath());
   } else {
     throw new IllegalArgumentException(
         "Cannot render the result of type: " + result.getClass().getSimpleName());
   }
   render(fileInfo, modelView.getModel(), request, servletRequest, response, response.getWriter());
   return true;
 }