本文整理匯總了Java中org.apache.velocity.app.Velocity.mergeTemplate方法的典型用法代碼示例。如果您正苦於以下問題:Java Velocity.mergeTemplate方法的具體用法?Java Velocity.mergeTemplate怎麽用?Java Velocity.mergeTemplate使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.apache.velocity.app.Velocity
的用法示例。
在下文中一共展示了Velocity.mergeTemplate方法的14個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: showTasks
import org.apache.velocity.app.Velocity; //導入方法依賴的package包/類
private void showTasks(HttpServletRequest request, HttpServletResponse response, Collection<TaskRecord> tasks, String title, boolean showDbStats)
throws IOException {
if ("json".equals(request.getParameter("format"))) {
response.setContentType("text/plain");
} else {
response.setContentType("text/html");
}
int offset = getOffset(request);
int length = getLength(request);
// Create Velocity context
VelocityContext context = new VelocityContext();
context.put("tasks", tasks);
context.put("title", title);
context.put("reportStore", metadata);
context.put("request", request);
context.put("offset", offset);
context.put("length", length);
context.put("lastResultNum", offset + length - 1);
context.put("prevOffset", Math.max(0, offset - length));
context.put("nextOffset", offset + length);
context.put("showStats", showDbStats);
context.put("JSON_DATE_FORMAT", JSON_DATE_FORMAT);
context.put("HTML_DATE_FORMAT", HTML_DATE_FORMAT);
context.put("PAGE_LENGTH", PAGE_LENGTH);
// Return Velocity results
try {
Velocity.mergeTemplate("tasks.vm", "UTF-8", context, response.getWriter());
response.setStatus(HttpServletResponse.SC_OK);
} catch (Exception e) {
LOG.warn("Failed to display tasks.vm", e);
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Failed to display tasks.vm");
}
}
示例2: renderMatrixTemplateTest
import org.apache.velocity.app.Velocity; //導入方法依賴的package包/類
@Test
public void renderMatrixTemplateTest() throws Exception {
//when:
String result = templateService.renderTemplate(pof, "Title", "default", riskIssues);
//then:
verify(pof, times(1)).isFilter();
verify(pof, times(1)).isProject();
verify(pof, times(1)).getName();
verify(webResourceUrlProvider, times(1)).getBaseUrl();
StringWriter sw = new StringWriter();
Writer writer = new BufferedWriter(sw);
VelocityContext context = new VelocityContext();
Velocity.mergeTemplate("src/main/resources/templates/matrixTemplate.vm", "UTF-8", context, writer);
writer.close();
sw.close();
String expectedResults = sw.getBuffer().toString();
assertEquals("Template service result is not what expected", expectedResults, result);
}
示例3: main
import org.apache.velocity.app.Velocity; //導入方法依賴的package包/類
public static void main(String args[])
throws Exception
{
if ( args.length == 0 || ! args[0].endsWith(".vm") )
{
System.err.println(
"Usage: java Driver x.vm");
return;
}
Velocity.init();
VelocityContext context = new VelocityContext();
PrintWriter writer = new PrintWriter(System.out, true);
Velocity.mergeTemplate(args[0], context, writer);
writer.flush();
writer.close();
}
示例4: mergeVelocityContext
import org.apache.velocity.app.Velocity; //導入方法依賴的package包/類
/**
* 根據Velocity模板,生成相應的文件
*
* @param context VelocityHost
* @param resultFilePath 生成的文件路徑
* @param templateFile Velocity模板文件
* @return
*/
public static boolean mergeVelocityContext(VelocityContext context, String resultFilePath, String templateFile)
throws Exception {
FileWriter daoWriter = null;
try {
daoWriter = new FileWriter(resultFilePath);
Velocity.mergeTemplate(templateFile, "UTF-8", context, daoWriter);
} catch (Throwable e) {
throw e;
} finally {
JavaIOUtils.closeWriter(daoWriter);
}
return true;
}
示例5: generateConfFile
import org.apache.velocity.app.Velocity; //導入方法依賴的package包/類
public String generateConfFile(String template, VelocityContext context) {
StringWriter sw = new StringWriter();
try {
Velocity.mergeTemplate(template + ".vm", "UTF-8", context, sw);
} catch (Exception ex) {
log.error("Failed to load velocity template '{0}'", ex, template);
return null;
}
return sw.toString();
}
示例6: generateConfFile
import org.apache.velocity.app.Velocity; //導入方法依賴的package包/類
public String generateConfFile(String template, VelocityContext context) {
StringWriter sw = new StringWriter();
try {
Velocity.mergeTemplate(template + ".vm", "UTF-8", context, sw);
} catch (Exception ex) {
log.error("Failed to load velocity template '{}'", template, ex);
return null;
}
return sw.toString();
}
示例7: mergeTemplate
import org.apache.velocity.app.Velocity; //導入方法依賴的package包/類
/**
* 渲染Velocity模板
* @param path
* @param map
*/
public static String mergeTemplate(String path, Map<String, Object> map) throws IOException {
VelocityContext vc = new VelocityContext();
if (null != map) {
for (String key : map.keySet()) {
vc.put(key, map.get(key));
}
}
StringWriter w = new StringWriter();
Velocity.mergeTemplate(path, "utf-8", vc, w);
String content = w.toString();
w.close();
return content;
}
示例8: generateFile
import org.apache.velocity.app.Velocity; //導入方法依賴的package包/類
/**
* Generate the specified output file by merging the specified
* Velocity template with the supplied context.
*/
protected void generateFile(File file,
String templateName,
VelocityContext context) throws IOException {
try (Writer writer = new BufferedWriter(new FileWriter(file))) {
Velocity.mergeTemplate(classpathPrefix + templateName,
ENCODING,
context,
writer);
writer.flush();
}
}
示例9: main
import org.apache.velocity.app.Velocity; //導入方法依賴的package包/類
public static void main(String args[])
throws Exception
{
Velocity.init();
VelocityContext context = new VelocityContext();
context.put("dictionary", dictionary);
context.put("names", names);
PrintWriter writer = new PrintWriter(System.out, true);
Velocity.mergeTemplate("TermsTable.vm", context, writer);
writer.flush();
writer.close();
}
示例10: main
import org.apache.velocity.app.Velocity; //導入方法依賴的package包/類
public static void main(String args[])
throws Exception
{
Calendar calendar = Calendar.getInstance();
int hour = calendar.get(Calendar.HOUR_OF_DAY);
Velocity.init();
VelocityContext context = new VelocityContext();
context.put("now", calendar.getTime());
context.put("hour", new Integer(hour));
PrintWriter writer = new PrintWriter(System.out, true);
Velocity.mergeTemplate("TheTime.vm", context, writer);
writer.flush();
writer.close();
}
示例11: main
import org.apache.velocity.app.Velocity; //導入方法依賴的package包/類
public static void main(String args[])
throws Exception
{
Velocity.init();
VelocityContext context = new VelocityContext();
context.put("dictionary", dictionary);
context.put("names", names);
PrintWriter writer = new PrintWriter(System.out, true);
Velocity.mergeTemplate("TermsDictionary.vm", context, writer);
writer.flush();
writer.close();
}
示例12: Write
import org.apache.velocity.app.Velocity; //導入方法依賴的package包/類
public String Write(Guide guide) {
Velocity.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
Velocity.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
Velocity.init();
VelocityContext context = new VelocityContext();
context.put("guide", guide);
StringWriter writer = new StringWriter();
Velocity.mergeTemplate("guide-html5.vm", "UTF-8", context, writer);
return writer.toString();
}
示例13: generateWeatherReport
import org.apache.velocity.app.Velocity; //導入方法依賴的package包/類
/**
* 把Weather信息生成圖文消息
*
* @param city
* @param msg
*/
public NewsMessage generateWeatherReport(String city, NewsMessage msg) {
WeatherInfo wi = weather.searchWeather(city);
if (null == wi) return null;
Date today = null;
try {
today = sdf.parse(wi.getDate_y());
} catch (ParseException e) {
e.printStackTrace();
}
if (null == today) return null;
String[] days = getDaysArray(today, REPORT_DAYS);
// 加載模板
initVelocity();
VelocityContext context = new VelocityContext();
context.put("wi", wi);
context.put("days", days);
context.put("msg", msg);
StringWriter writer = new StringWriter();
Velocity.mergeTemplate("lemon/weixin/toolkit/weather.xml", MmtCharset.LOCAL_CHARSET, context,
writer);
// TODO 天氣消息直接轉NewsMessage
return null;
}
示例14: createMessageClass
import org.apache.velocity.app.Velocity; //導入方法依賴的package包/類
/**
* This is the main method that generates the _Messages.java file.
* The class is generated by extracting the list of messges from the
* available LogMessages Resource.
*
* The extraction is done based on typeIdentifier which is a 3-digit prefix
* on the messages e.g. BRK for Broker.
*
* @throws InvalidTypeException when an unknown parameter type is used in the properties file
* @throws Exception thrown by velocity if there is an error
*/
private void createMessageClass(String file)
throws InvalidTypeException, Exception
{
VelocityContext context = new VelocityContext();
String bundle = file.replace(File.separator, ".");
int packageStartIndex = bundle.indexOf(_packageSource) + _packageSource.length() + 2;
bundle = bundle.substring(packageStartIndex, bundle.indexOf(".properties"));
System.out.println("Creating Class for bundle:" + bundle);
ResourceBundle fileBundle = ResourceBundle.getBundle(bundle, Locale.US);
// Pull the bit from /os/path/<className>.logMessages from the bundle name
String className = file.substring(file.lastIndexOf(File.separator) + 1, file.lastIndexOf("_"));
debug("Creating ClassName form file:" + className);
String packageString = bundle.substring(0, bundle.indexOf(className));
String packagePath = packageString.replace(".", File.separator);
debug("Package path:" + packagePath);
File outputDirectory = new File(_outputDir + File.separator + packagePath);
if (!outputDirectory.exists())
{
if (!outputDirectory.mkdirs())
{
throw new IllegalAccessException("Unable to create package structure:" + outputDirectory);
}
}
// Get the Data for this class and typeIdentifier
HashMap<String, Object> typeData = prepareType(className, fileBundle);
context.put("package", packageString.substring(0, packageString.lastIndexOf('.')));
//Store the resource Bundle name for the macro
context.put("resource", bundle);
// Store this data in the context for the macro to access
context.put("type", typeData);
// Create the file writer to put the finished file in
String outputFile = _outputDir + File.separator + packagePath + className + "Messages.java";
debug("Creating Java file:" + outputFile);
FileWriter output = new FileWriter(outputFile);
// Run Velocity to create the output file.
// Fix the default file encoding to 'ISO-8859-1' rather than let
// Velocity fix it. This is the encoding format for the macro.
Velocity.mergeTemplate("LogMessages.vm", "ISO-8859-1", context, output);
//Close our file.
output.flush();
output.close();
}