当前位置: 首页>>代码示例>>Java>>正文


Java VelocityEngine.getTemplate方法代码示例

本文整理汇总了Java中org.apache.velocity.app.VelocityEngine.getTemplate方法的典型用法代码示例。如果您正苦于以下问题:Java VelocityEngine.getTemplate方法的具体用法?Java VelocityEngine.getTemplate怎么用?Java VelocityEngine.getTemplate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.apache.velocity.app.VelocityEngine的用法示例。


在下文中一共展示了VelocityEngine.getTemplate方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: sendEmail

import org.apache.velocity.app.VelocityEngine; //导入方法依赖的package包/类
private void sendEmail(final String fromEmail, final IUser to, final String inSubject, final String inTemplate, final Map<String, Object> values) {
    final Properties props = new Properties();
    props.setProperty("resource.loader", "class");
    props.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");

    final VelocityEngine engine = new VelocityEngine(props);
    final VelocityContext context = new VelocityContext();

    engine.init();

    for(final String key : values.keySet()) {
        LOGGER.debug(() -> String.format("\t -- %s=%s", key, values.get(key)));
        context.put(key, values.get(key));
    }

    final StringWriter writer = new StringWriter();
    final Template template = engine.getTemplate("templates/" + inTemplate);

    template.merge(context, writer);

    final String inBody = writer.toString();

    sendEmail(fromEmail, to.getEmail(), inSubject, inBody);
}
 
开发者ID:howma03,项目名称:sporticus,代码行数:25,代码来源:ServiceMailAbstract.java

示例2: testTemplate1

import org.apache.velocity.app.VelocityEngine; //导入方法依赖的package包/类
public void testTemplate1() throws Exception {
    VelocityContext context = new VelocityContext();
    MyBean bean = createBean();
    context.put("bean", bean);
    Yaml yaml = new Yaml();
    context.put("list", yaml.dump(bean.getList()));
    VelocityEngine ve = new VelocityEngine();
    ve.setProperty("file.resource.loader.class", ClasspathResourceLoader.class.getName());
    ve.init();
    Template t = ve.getTemplate("template/mybean1.vm");
    StringWriter writer = new StringWriter();
    t.merge(context, writer);
    String output = writer.toString().trim().replaceAll("\\r\\n", "\n");
    // System.out.println(output);
    String etalon = Util.getLocalResource("template/etalon2-template.yaml").trim();
    assertEquals(etalon.length(), output.length());
    assertEquals(etalon, output);
    // parse the YAML document
    Yaml loader = new Yaml();
    MyBean parsedBean = loader.loadAs(output, MyBean.class);
    assertEquals(bean, parsedBean);
}
 
开发者ID:bmoliveira,项目名称:snake-yaml,代码行数:23,代码来源:VelocityTest.java

示例3: testApp

import org.apache.velocity.app.VelocityEngine; //导入方法依赖的package包/类
@Test
@Ignore
public void testApp() throws IOException {

    Properties props = new Properties();
    props.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("velocity.properties"));
    VelocityEngine ve = new VelocityEngine(props);
    ve.init();

    Template t = ve.getTemplate("/templates/registry.vm");
    VelocityContext context = new VelocityContext();

    context.put("username", "ricky");
    context.put("url", "http://www.thymeleaf.org");
    context.put("email", "[email protected]");

    StringWriter writer = new StringWriter(1024);
    t.merge(context, writer);

    String output = writer.toString();
    System.out.println(output);
}
 
开发者ID:TFdream,项目名称:okmail,代码行数:23,代码来源:VelocityTest.java

示例4: buildStringWriter

import org.apache.velocity.app.VelocityEngine; //导入方法依赖的package包/类
private static StringWriter buildStringWriter(List<Node> nodes, List<Edge> edges, Map<EVMEnvironment, SymExecutor> executions) throws ReportException {

        VelocityEngine velocityEngine = new VelocityEngine();
        Properties p = new Properties();
        p.setProperty("resource.loader", "class");
        p.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
        velocityEngine.init(p);
        Template template = velocityEngine.getTemplate(TEMPLATE_FILE);
        VelocityContext velocityContext = new VelocityContext();
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            velocityContext.put("nodes", objectMapper.writeValueAsString(nodes));
            velocityContext.put("edges", objectMapper.writeValueAsString(edges));
            velocityContext.put("executions", executions);
        } catch (IOException e) {
            logger.error("Error building the report: " + e);
            throw new ReportException("Failed creating ReportItem");
        }
        StringWriter stringWriter = new StringWriter();
        template.merge(velocityContext, stringWriter);
        return stringWriter;
    }
 
开发者ID:fergarrui,项目名称:ethereum-bytecode-analyzer,代码行数:23,代码来源:Report.java

示例5: generateHtmlChunks

import org.apache.velocity.app.VelocityEngine; //导入方法依赖的package包/类
private static List<String> generateHtmlChunks(List<ReportItem> reportItemList) {
  List<String> htmlChunks = new ArrayList<>();

  VelocityEngine velocityEngine = new VelocityEngine();
  Properties p = new Properties();
  p.setProperty("resource.loader", "class");
  p.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
  velocityEngine.init(p);
  Template template = velocityEngine.getTemplate("template/report_template.html");

  int maxItemsInReport = CliHelper.getMaxItemsInReport();
  List<List<ReportItem>> reportItemsChunks = Lists.partition(reportItemList, maxItemsInReport);

  for (List<ReportItem> reportItemsChunk : reportItemsChunks ) {
    VelocityContext velocityContext = new VelocityContext();
    velocityContext.put("jarPath", CliHelper.getPathToAnalyze());
    velocityContext.put("ruleName", reportItemsChunk.get(0).getRuleName());
    velocityContext.put("reportItems", reportItemsChunk);

    StringWriter stringWriter = new StringWriter();
    template.merge(velocityContext, stringWriter);
    htmlChunks.add(stringWriter.toString());
  }
  return htmlChunks;
}
 
开发者ID:fergarrui,项目名称:custom-bytecode-analyzer,代码行数:26,代码来源:ReportBuilder.java

示例6: renderNonWebAppTemplate

import org.apache.velocity.app.VelocityEngine; //导入方法依赖的package包/类
@Test
public void renderNonWebAppTemplate() throws Exception {
	AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
			VelocityAutoConfiguration.class);
	try {
		VelocityEngine velocity = context.getBean(VelocityEngine.class);
		StringWriter writer = new StringWriter();
		Template template = velocity.getTemplate("message.vm");
		template.process();
		VelocityContext velocityContext = new VelocityContext();
		velocityContext.put("greeting", "Hello World");
		template.merge(velocityContext, writer);
		assertThat(writer.toString()).contains("Hello World");
	}
	finally {
		context.close();
	}
}
 
开发者ID:philwebb,项目名称:spring-boot-concourse,代码行数:19,代码来源:VelocityAutoConfigurationTests.java

示例7: createIndexPage

import org.apache.velocity.app.VelocityEngine; //导入方法依赖的package包/类
/**
 * Creates and returns the website's index page.
 * 
 * <p>This is not private only so that it can be unit-tested.
 * 
 * @param redirectUrl the Url of the booking page for the current date.
 * @param showRedirectMessage whether to show a redirect error message to the user
 */
protected String createIndexPage(String redirectUrl, Boolean showRedirectMessage) {

  logger.log("About to create the index page");

  // Create the page by merging the data with the page template
  VelocityEngine engine = new VelocityEngine();
  // Use the classpath loader so Velocity finds our template
  Properties properties = new Properties();
  properties.setProperty("resource.loader", "class");
  properties.setProperty("class.resource.loader.class",
      "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
  engine.init(properties);

  VelocityContext context = new VelocityContext();
  context.put("redirectUrl", redirectUrl);
  context.put("showRedirectMessage", showRedirectMessage);

  // Render the page
  StringWriter writer = new StringWriter();
  Template template = engine.getTemplate("squash/booking/lambdas/IndexPage.vm", "utf-8");
  template.merge(context, writer);
  logger.log("Rendered index page: " + writer);
  return writer.toString();
}
 
开发者ID:robinsteel,项目名称:Sqawsh,代码行数:33,代码来源:PageManager.java

示例8: renderSchema

import org.apache.velocity.app.VelocityEngine; //导入方法依赖的package包/类
private void renderSchema(PrismSchema schema, PrismContext prismContext, VelocityEngine velocityEngine, PathGenerator pathGenerator) throws IOException {
    getLog().info("Processing schema: "+schema);
    VelocityContext velocityContext = new VelocityContext();
    populateVelocityContextBase(velocityContext, prismContext, pathGenerator, schema, "..");

    Template template = velocityEngine.getTemplate(TEMPLATE_SCHEMA_NAME);

    Writer writer = new FileWriter(pathGenerator.prepareSchemaOutputFile(schema));
    template.merge(velocityContext, writer);
    writer.close();

    // Object Definitions
    for (PrismObjectDefinition objectDefinition: schema.getObjectDefinitions()) {
        renderObjectDefinition(objectDefinition, schema, prismContext, velocityEngine, pathGenerator);
    }

    // Types
    for (ComplexTypeDefinition typeDefinition : schema.getComplexTypeDefinitions()) {
        renderComplexTypeDefinition(typeDefinition, schema, prismContext, velocityEngine, pathGenerator);
    }

}
 
开发者ID:Pardus-Engerek,项目名称:engerek,代码行数:23,代码来源:SchemaDocMojo.java

示例9: testVelocityTemplate

import org.apache.velocity.app.VelocityEngine; //导入方法依赖的package包/类
@Test
@Ignore
public void testVelocityTemplate() throws IOException, MessagingException {

    Properties props = new Properties();
    props.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("velocity.properties"));
    VelocityEngine ve = new VelocityEngine(props);
    ve.init();

    Template t = ve.getTemplate("/templates/registry.vm");
    VelocityContext context = new VelocityContext();

    context.put("username", "ricky");
    context.put("url", "http://www.thymeleaf.org");
    context.put("email", "[email protected]");

    StringWriter sw = new StringWriter(1024);
    t.merge(context, sw);

    String output = sw.toString();
    System.out.println(output);

    Mail mail = Mail.newBuilder().from(from,"ricky fung")
            .to(to)
            .subject("测试邮件[模板邮件-Velocity]")
            .html(output)
            .build();

    client.send(mail);
}
 
开发者ID:TFdream,项目名称:okmail,代码行数:31,代码来源:OkMailClientTest.java

示例10: parse

import org.apache.velocity.app.VelocityEngine; //导入方法依赖的package包/类
public static String parse(String templateFileName, AbstractFileWrapper data) {

        // 初始化模板引擎
        VelocityEngine engine = new VelocityEngine();
        engine.setProperty(RuntimeConstants.RESOURCE_LOADER, "file");
        engine.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());

        Properties props = new Properties();

        URL resource = VelocityUtil.class.getClassLoader().getResource(".");
        Objects.requireNonNull(resource);

        // 设置模板目录
        String basePath = resource.getPath() + Const.TEMPLATE_LOCATION_DIR;
        props.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH,  basePath);
        //设置velocity的编码
        props.setProperty(Velocity.ENCODING_DEFAULT, StandardCharsets.UTF_8.name());
        props.setProperty(Velocity.INPUT_ENCODING,   StandardCharsets.UTF_8.name());
        props.setProperty(Velocity.OUTPUT_ENCODING,  StandardCharsets.UTF_8.name());
        engine.init(props);

        Template template = engine.getTemplate(templateFileName);
        // 设置变量
        VelocityContext ctx = new VelocityContext();

        ctx.put("data", data);

        StringWriter writer = new StringWriter();
        template.merge(ctx, writer);

        return writer.toString();


    }
 
开发者ID:MrHunterZhao,项目名称:CoffeeMaker,代码行数:35,代码来源:VelocityUtil.java

示例11: getSMSBody

import org.apache.velocity.app.VelocityEngine; //导入方法依赖的package包/类
public static String getSMSBody(String orgName, String state, String userName, String webUrl,
    String appUrl,String instanceName) {
  try {
    Properties props = new Properties();
    props.put("resource.loader", "class");
    props.put("class.resource.loader.class",
        "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");

    VelocityEngine ve = new VelocityEngine();
    ve.init(props);

    Map<String,String> params = new HashMap<>();
    params.put("orgName", isStringNullOREmpty(orgName) ? "" : orgName);
    params.put("state", isStringNullOREmpty(state) ? "" : state);
    params.put("comma", isStringNullOREmpty(state) ? "" : ",");
    params.put("userName", isStringNullOREmpty(userName) ? "user_name" : userName);
    params.put("webUrl", isStringNullOREmpty(webUrl) ? "" : webUrl);
    params.put("appUrl", isStringNullOREmpty(appUrl) ? "" : appUrl);
    params.put("instanceName", isStringNullOREmpty(instanceName) ? "instance_name" : instanceName);
    Template t = ve.getTemplate("/welcomeSmsTemplate.vm");
    VelocityContext context = new VelocityContext(params);
    StringWriter writer = new StringWriter();
    t.merge(context, writer);
    String sms = writer.toString();
    if(isStringNullOREmpty(orgName)){
      sms = sms.replace(" for ", "");
    }
    if(isStringNullOREmpty(appUrl)){
      sms = sms.replace("APP download: ", "");
    }
    if(isStringNullOREmpty(webUrl)){
      sms = sms.replace("Web access URL: ", "");
    }
    return sms;
  } catch (Exception ex) {
    ProjectLogger.log("Exception occurred while formating and sending SMS " + ex);
  }
  return "";
}
 
开发者ID:project-sunbird,项目名称:sunbird-utils,代码行数:40,代码来源:ProjectUtil.java

示例12: generateHtmlfile

import org.apache.velocity.app.VelocityEngine; //导入方法依赖的package包/类
private void generateHtmlfile(Map<String, Object> input) {	   
 try{
 	VelocityEngine ve = new VelocityEngine();
 	ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
 	ve.setProperty("classpath.resource.loader.class",ClasspathResourceLoader.class.getName());
 	ve.init();
 	Template template = ve.getTemplate("templates/acmeair-report.vtl");
 	VelocityContext context = new VelocityContext();
 	 	    
 	 
 	for(Map.Entry<String, Object> entry: input.entrySet()){
 		context.put(entry.getKey(), entry.getValue());
 	}
 	context.put("math", new MathTool());
 	context.put("number", new NumberTool());
 	context.put("date", new ComparisonDateTool());
 	
 	Writer file = new FileWriter(new File(searchingLocation
	+ System.getProperty("file.separator") + RESULTS_FILE));	    
 	template.merge( context, file );
 	file.flush();
 	file.close();
   
 }catch(Exception e){
 	e.printStackTrace();
 }
}
 
开发者ID:WillemJiang,项目名称:acmeair,代码行数:28,代码来源:ReportGenerator.java

示例13: getTempate

import org.apache.velocity.app.VelocityEngine; //导入方法依赖的package包/类
public static Template getTempate(String path) {
	VelocityEngine ve = new VelocityEngine();
	ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
	ve.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
	ve.init();
	Template t = ve.getTemplate(path, "utf-8");
	return t;
}
 
开发者ID:wu191287278,项目名称:sc-generator,代码行数:9,代码来源:VelocityUtil.java

示例14: getTemplate

import org.apache.velocity.app.VelocityEngine; //导入方法依赖的package包/类
/**
 * Use Velocity Engine to get html template.
 *
 * @param request
 * @param tempName
 * @return String - template html
 */
protected String getTemplate(HttpServletRequest request, String tempName) {
    VelocityEngine ve = (VelocityEngine) request.getServletContext().getAttribute("templateEngine");
    VelocityContext context = new VelocityContext();
    Template template = ve.getTemplate("frontend" + File.separator + tempName + ".html");

    StringWriter writer = new StringWriter();
    template.merge(context, writer);
    return writer.toString();
}
 
开发者ID:brianisadog,项目名称:hotelApp,代码行数:17,代码来源:BaseServlet.java

示例15: getTemplate

import org.apache.velocity.app.VelocityEngine; //导入方法依赖的package包/类
public org.apache.velocity.Template getTemplate() {
    Velocity.addProperty("file.resource.loader.path", templateDir);
    VelocityEngine ve = new VelocityEngine();
    // ve.setProperty("file.resource.loader.path", templateDir);
    Properties p = new Properties();
    p.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, templateDir);
    p.setProperty(RuntimeConstants.INPUT_ENCODING, "UTF-8");
    p.setProperty(RuntimeConstants.OUTPUT_ENCODING, "UTF-8");
    ve.init(p);
    // template = Velocity.getTemplate(templateName, "UTF-8");
    template = ve.getTemplate(templateName, "UTF-8");
    return template;
}
 
开发者ID:zhaoxi1988,项目名称:sjk,代码行数:14,代码来源:VeTemplate.java


注:本文中的org.apache.velocity.app.VelocityEngine.getTemplate方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。