本文整理汇总了Java中org.apache.velocity.app.VelocityEngine.mergeTemplate方法的典型用法代码示例。如果您正苦于以下问题:Java VelocityEngine.mergeTemplate方法的具体用法?Java VelocityEngine.mergeTemplate怎么用?Java VelocityEngine.mergeTemplate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.velocity.app.VelocityEngine
的用法示例。
在下文中一共展示了VelocityEngine.mergeTemplate方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: writeTo
import org.apache.velocity.app.VelocityEngine; //导入方法依赖的package包/类
@Override
public void writeTo(String templateReference, Viewable viewable, MediaType mediaType,
MultivaluedMap<String, Object> httpHeaders, OutputStream out) throws IOException {
// 获取 模板引擎
VelocityEngine velocityEngine = getVelocityEngine();
// 实例化一个VelocityContext
VelocityContext context = (VelocityContext) viewable.getModel();
Enumeration<String> enums = request.getParameterNames();
while (enums.hasMoreElements()) {
String key = enums.nextElement();
context.put(key, request.getParameter(key));
}
// 把request放进模板上下文里
context.put("request", request);
// 渲染并输出
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(out);
velocityEngine.mergeTemplate(templateReference, "utf8", context, outputStreamWriter);
outputStreamWriter.flush();
outputStreamWriter.close(); // 有必要关闭吗? 关闭了是否对jax-rs拦截器,servlet有影响,需要继续学习,参考jsp模板实现
}
示例2: renderTemplate
import org.apache.velocity.app.VelocityEngine; //导入方法依赖的package包/类
/**
* Return the String representation of the template rendered using Velocity.
*
* @param context context use to render the template
* @param templateFileName file name of the template in the classpath
* @throws TemplateRenderException if there is an error with the template
*/
public static String renderTemplate(String templateFileName,
VelocityContext context)
throws TemplateRenderException {
VelocityEngine ve = new VelocityEngine();
ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
ve.setProperty("classpath.resource.loader.class",
ClasspathResourceLoader.class.getName());
StringWriter sw = new StringWriter();
try {
ve.mergeTemplate(templateFileName, "UTF-8", context, sw);
} catch (ResourceNotFoundException
| ParseErrorException
| MethodInvocationException e) {
throw new TemplateRenderException("Error rendering template file: " + templateFileName, e);
}
return sw.toString();
}
示例3: renderFile
import org.apache.velocity.app.VelocityEngine; //导入方法依赖的package包/类
/**
* 渲染模板文件.
*
* @param velocityEngine velocityEngine, 需经过VelocityEngineFactory处理, 绑定Spring的ResourceLoader.
* @param templateContent 模板文件名, loader会自动在前面加上velocityEngine的resourceLoaderPath.
* @param context 变量Map.
*/
public static String renderFile(String templateFilePName, VelocityEngine velocityEngine, String encoding,
Map<String, ?> context) {
VelocityContext velocityContext = new VelocityContext(context);
StringWriter result = new StringWriter();
velocityEngine.mergeTemplate(templateFilePName, encoding, velocityContext, result);
return result.toString();
}
示例4: doHandleRequest
import org.apache.velocity.app.VelocityEngine; //导入方法依赖的package包/类
@Override
protected ModelAndView doHandleRequest(HttpServletRequest request,
HttpServletResponse response) throws Exception {
Properties properties=new Properties();
properties.setProperty("resource.loader", "file");
properties.setProperty("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
String contextPath=request.getContextPath();
if(!contextPath.endsWith("/")){
contextPath+="/";
}
VelocityEngine velocityEngine=new VelocityEngine(properties);
VelocityContext context=new VelocityContext();
StringBuffer sb=new StringBuffer();
sb.append("\r");
sb.append("<link rel=\"stylesheet\" type=\"text/css\" href=\""+contextPath+"dorado/res/dorado/resources/jquery.contextMenu.css\" />");
sb.append("\r");
sb.append("<link rel=\"stylesheet\" type=\"text/css\" href=\""+contextPath+"dorado/res/dorado/resources/jquery-ui-1.8.19.custom.css\" />");
sb.append("\r");
sb.append("<script type=\"text/javascript\" src=\""+contextPath+"dorado/res/dorado/scripts/jbpm4-designer-all-in-one.js\"></script>");
sb.append("\r");
String serverUrl=this.buildServerUrl(request.getScheme(),request.getServerName(),request.getServerPort());
if(contextPath.endsWith("/")){
serverUrl+=contextPath.substring(0,contextPath.length()-1);
}
context.put("cssandscript", sb.toString());
context.put("baseIconsDir", contextPath+"dorado/res/dorado/resources");
context.put("serverUrl", serverUrl);
StringWriter writer=new StringWriter();
velocityEngine.mergeTemplate("dorado/resources/jbpm4-designer.html","utf-8", context, writer);
response.setContentType("text/html; charset=utf-8");
PrintWriter out=response.getWriter();
out.write(writer.toString());
out.flush();
out.close();
return null;
}
示例5: renderTemplate
import org.apache.velocity.app.VelocityEngine; //导入方法依赖的package包/类
/**
* Render a template and return the result
*
* @param templateName the template name only without the .vm extension
* @param context the context
* @return string
*/
public static String renderTemplate(String templateName, VelocityContext context) {
StringWriter stringWriter = new StringWriter();
VelocityEngine engine = new VelocityEngine();
engine.init("src/test/resources/velocity.properties");
engine.mergeTemplate(TEMPLATE_BASE_DIR + templateName + ".vm", "UTF-8", context, stringWriter);
return stringWriter.getBuffer().toString();
}
示例6: mergeTemplate
import org.apache.velocity.app.VelocityEngine; //导入方法依赖的package包/类
/**
* Merge the specified Velocity template with the given model and write
* the result to the given Writer.
* @param velocityEngine VelocityEngine to work with
* @param templateLocation the location of template, relative to Velocity's resource loader path
* @param model the Map that contains model names as keys and model objects as values
* @param writer the Writer to write the result to
* @throws VelocityException if the template wasn't found or rendering failed
* @deprecated Use {@link #mergeTemplate(VelocityEngine, String, String, Map, Writer)}
* instead, following Velocity 1.6's corresponding deprecation in its own API.
*/
@Deprecated
public static void mergeTemplate(
VelocityEngine velocityEngine, String templateLocation, Map<String, Object> model, Writer writer)
throws VelocityException {
VelocityContext velocityContext = new VelocityContext(model);
velocityEngine.mergeTemplate(templateLocation, velocityContext, writer);
}
示例7: mergeTemplate
import org.apache.velocity.app.VelocityEngine; //导入方法依赖的package包/类
/**
* Merge the specified Velocity template with the given model and write
* the result to the given Writer.
* @param velocityEngine VelocityEngine to work with
* @param templateLocation the location of template, relative to Velocity's resource loader path
* @param model the Map that contains model names as keys and model objects as values
* @param writer the Writer to write the result to
* @throws org.apache.velocity.exception.VelocityException if the template wasn't found or rendering failed
* @deprecated Use {@link #mergeTemplate(org.apache.velocity.app.VelocityEngine, String, String, java.util.Map, java.io.Writer)}
* instead, following Velocity 1.6's corresponding deprecation in its own API.
*/
@Deprecated
public static void mergeTemplate(
VelocityEngine velocityEngine, String templateLocation, Map<String, Object> model, Writer writer)
throws VelocityException {
VelocityContext velocityContext = new VelocityContext(model);
velocityEngine.mergeTemplate(templateLocation, velocityContext, writer);
}