本文整理匯總了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);
}
示例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);
}
示例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);
}
示例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;
}
示例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;
}
示例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();
}
}
示例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();
}
示例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);
}
}
示例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);
}
示例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();
}
示例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 "";
}
示例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();
}
}
示例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;
}
示例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();
}
示例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;
}