Package org.apache.velocity

Examples of org.apache.velocity.Template


    Boolean inChain = result.isInChain();
    if((inChain==null || !inChain) &&(result.getErrorType() != null || result.getInvalidValues() != null)){
      url = annotation.error().value();
    }
    //获取模板
    Template template = Velocity.getTemplate(url, "utf-8");
    //往上下文中填入数据
    context.put("ctxPath", this.request.getContextPath());
    context.put("ctx", result);
    context.put("xctx",request.getSession().getAttribute(HtmlResponseWriter.OPTION_KEY));
    //输出到用户端
    StringWriter writer = new StringWriter();
    template.merge(context, writer);
    String content = writer.toString();
    out.write(content.getBytes());
  }
View Full Code Here


        response.setContentType("text/html");

        try {
            String location = (String) ActionContext.getContext().get("template");
            Template template = velocityManager.getVelocityEngine().getTemplate(location);

            Writer writer = pageContext.getOut();
            template.merge(resultContext, writer);
            writer.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // perform cleanup
View Full Code Here

            }

            //Rendering the content
            VelocityEngine velocityEngine = VelocityManager.getInstance().getVelocityEngine();

            Template template = velocityEngine.getTemplate(decorator.getPage());
            StringWriter tempWriter = new StringWriter();
            template.merge(context, tempWriter);
            String renderResult = tempWriter.toString();

            response.getWriter().print(renderResult);

            while (decorator.getInitParameterNames().hasNext()) {
View Full Code Here

        outputFileName = file.getName();
        // 获取模板
        String tPath = file.getAbsolutePath().replace(
            this.templatePath, "");

        Template template = Velocity.getTemplate(tPath, "UTF-8");

        if (file.getName().equalsIgnoreCase("ContextListener.java")) {
          packagePath = target.getPath() + File.separator
              + this.packageNamePath + File.separator
              + "listener" + File.separator;
          checkDirExist(packagePath);
        } else if (file.getName().equalsIgnoreCase("Simple.java")
            || file.getName().equalsIgnoreCase("WithVelocity.java")) {
          if (this.useJPA)
            return;
          packagePath = target.getPath() + File.separator
              + this.packageNamePath + File.separator
              + "resource" + File.separator;
          checkDirExist(packagePath);

          outputFileName = entityName + outputFileName;

        } else if (file.getName().equalsIgnoreCase("Entity.java")) {
          if (!this.useJPA)
            return;
          packagePath = target.getPath() + File.separator
              + this.packageNamePath + File.separator + "entity"
              + File.separator;
          checkDirExist(packagePath);
          outputFileName = entityName + ".java";
        } else if (file.getName().equalsIgnoreCase("Resource.java")) {
          if (!this.useJPA)
            return;
          packagePath = target.getPath() + File.separator
              + this.packageNamePath + File.separator
              + "service" + File.separator;
          checkDirExist(packagePath);
          outputFileName = entityName + outputFileName;
        } else if (file.getName().equalsIgnoreCase("Service.java")
            || file.getName().equalsIgnoreCase("ServiceTest.java")) {
          if (!this.useJPA)
            return;
          packagePath = target.getPath() + File.separator
              + this.packageNamePath + File.separator + "service"
              + File.separator;
          checkDirExist(packagePath);
          outputFileName = entityName + outputFileName;
        } else
          packagePath = target.getPath() + File.separator;

        if (file.getName().equalsIgnoreCase("velocity.vm")) {
          outputFileName = entityName + ".vm";
        }

        // 输出到用户端
        OutputStreamWriter fileWriter = new OutputStreamWriter(
            new FileOutputStream(packagePath + outputFileName),
            "UTF-8");
        template.merge(context, fileWriter);
        fileWriter.flush();
        fileWriter.close();
      } catch (Exception e) {
      }
    }
View Full Code Here

            return "";
        }
    }

    public static String getRenderedTemplateWithoutSwallowingErrors(String templateName, Context context) throws Exception {
        Template template = getTemplate(templateName);
        StringWriter tempWriter = new StringWriter();
        template.merge(context, tempWriter);
        return tempWriter.toString();
    }
View Full Code Here

         * com.opensymphony.webwork.portlet.sitemesh.VelocityUtils.class));
         * props.list(System.out); velocityEngine.init(props); }
         */
        String encoding = "UTF-8";

        Template template = velocityEngine.getTemplate(templateName, encoding);
        return template;
    }
View Full Code Here

        try
        {
            Context  ctx = createContext( request, response, context );

            Template template = _engine.getTemplate( path );
            template.merge( ctx, response.getWriter(  ) );
        }
        catch ( ParseErrorException p )
        {
            _log.error( "Unexpected error", p );
            throw new PortletServiceException( "Error in the template", p );
View Full Code Here

  }

  @Override
  public void out(String viewFileName) {
    try {
      Template template = Velocity.getTemplate(viewFileName, defaultEncoding);

      if (template != null) {
        Writer writer = douyuContext.getHttpServletResponse().getWriter();
        template.merge(velocityContext, writer);

        //writer.flush();
        //writer.close();
      }
    } catch (ResourceNotFoundException rnfe) {
View Full Code Here

        ExceptionUtils.checkEmpty("templateFile", templateFile);
        ExceptionUtils.checkNull("output", output);
        this.velocityContext = new VelocityContext();
        this.loadVelocityContext(templateObjects);

        Template template = (Template)this.discoveredTemplates.get(templateFile);
        if (template == null)
        {
            template = this.velocityEngine.getTemplate(templateFile);

            // We check to see if the namespace requires a merge, and if so
            final Merger merger = Merger.instance();
            if (merger.requiresMerge(this.namespace))
            {
                final String mergedTemplateLocation = this.getMergedTemplateLocation(templateFile);
                final InputStream resource = template.getResourceLoader().getResourceStream(templateFile);
                ResourceWriter.instance().writeStringToFile(
                    merger.getMergedString(resource, this.namespace),
                    mergedTemplateLocation);
                template = this.velocityEngine.getTemplate(templateFile);
                this.mergedTemplateFiles.add(new File(mergedTemplateLocation));
            }
            this.discoveredTemplates.put(templateFile, template);
        }
        template.merge(velocityContext, output);
    }
View Full Code Here

    public abstract boolean passthrough();

    public abstract void generate() throws ToolException;

    protected void doWrite(String templateName, Writer outputs) throws ToolException {
        Template tmpl = null;
        try {
            tmpl = Velocity.getTemplate(templateName);
        } catch (Exception e) {
            Message msg = new Message("TEMPLATE_MISSING", LOG, templateName);
            throw new ToolException(msg, e);
        }

        VelocityContext ctx = new VelocityContext();

        for (Iterator iter = attributes.keySet().iterator(); iter.hasNext();) {
            String key = (String)iter.next();
            ctx.put(key, attributes.get(key));
        }

        VelocityWriter writer = new VelocityWriter(outputs);
        try {
            tmpl.merge(ctx, writer);
            writer.close();
        } catch (Exception e) {
            Message msg = new Message("VELOCITY_ENGINE_WRITE_ERRORS", LOG);
            throw new ToolException(msg, e);
        }
View Full Code Here

TOP

Related Classes of org.apache.velocity.Template

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.