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