本文整理汇总了Java中freemarker.template.Configuration.setSharedVariable方法的典型用法代码示例。如果您正苦于以下问题:Java Configuration.setSharedVariable方法的具体用法?Java Configuration.setSharedVariable怎么用?Java Configuration.setSharedVariable使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类freemarker.template.Configuration
的用法示例。
在下文中一共展示了Configuration.setSharedVariable方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: configuration
import freemarker.template.Configuration; //导入方法依赖的package包/类
@Singleton
@Provides
public static Configuration configuration(LinkHelper linkHelper, MultiTemplateLoader templateLoader) {
try {
freemarker.log.Logger.selectLoggerLibrary(Logger.LIBRARY_SLF4J);
Configuration cfg = new freemarker.template.Configuration(Configuration.VERSION_2_3_25);
cfg.setTagSyntax(freemarker.template.Configuration.SQUARE_BRACKET_TAG_SYNTAX);
cfg.setLazyAutoImports(true);
cfg.setLocale(Locale.GERMANY); // todo make configurable
cfg.addAutoImport("saito", "saito.ftl");
cfg.setSharedVariable("saitoLinkHelper", linkHelper);
cfg.setDefaultEncoding("UTF-8");
cfg.setLogTemplateExceptions(false);
cfg.setTemplateLoader(templateLoader);
return cfg;
} catch (TemplateModelException | ClassNotFoundException e) {
log.error("Error creating config", e);
throw new IllegalStateException(e);
}
}
示例2: afterPropertiesSet
import freemarker.template.Configuration; //导入方法依赖的package包/类
@Override
public void afterPropertiesSet() throws IOException, TemplateException {
super.afterPropertiesSet();
Configuration cfg = this.getConfiguration();
cfg.setSharedVariable("shiro", new ShiroTags());// shiro标签
cfg.setNumberFormat("#");// 防止页面输出数字,变成2,000
// 可以添加很多自己的要传输到页面的[方法、对象、值]
}
示例3: loadTransforms
import freemarker.template.Configuration; //导入方法依赖的package包/类
/**
* Protected helper method.
* <p>
* SCIPIO: TODO: delegate to FreeMarkerWorker and remove license notice
*/
protected static void loadTransforms(ClassLoader loader, Properties props, Configuration config) {
for (Iterator<Object> i = props.keySet().iterator(); i.hasNext();) {
String key = (String) i.next();
String className = props.getProperty(key);
if (Debug.verboseOn()) {
Debug.logVerbose("Adding FTL Transform " + key + " with class " + className, module);
}
try {
config.setSharedVariable(key, loader.loadClass(className).newInstance());
} catch (Exception e) {
Debug.logError(e, "Could not pre-initialize dynamically loaded class: " + className + ": " + e, module);
}
}
}
示例4: loadTransforms
import freemarker.template.Configuration; //导入方法依赖的package包/类
/**
* Protected helper method.
*/
protected static void loadTransforms(ClassLoader loader, Properties props, Configuration config) {
for (Iterator<Object> i = props.keySet().iterator(); i.hasNext();) {
String key = (String) i.next();
String className = props.getProperty(key);
if (Debug.verboseOn()) {
Debug.logVerbose("Adding FTL Transform " + key + " with class " + className, module);
}
try {
config.setSharedVariable(key, loader.loadClass(className).newInstance());
} catch (Exception e) {
Debug.logError(e, "Could not pre-initialize dynamically loaded class: " + className + ": " + e, module);
}
}
}
示例5: loadSharedVars
import freemarker.template.Configuration; //导入方法依赖的package包/类
/**
* SCIPIO: Loads shared vars.
*/
protected static void loadSharedVars(Properties props, Configuration config) {
for (Iterator<Object> i = props.keySet().iterator(); i.hasNext();) {
String key = (String) i.next();
String value = props.getProperty(key);
if (Debug.verboseOn()) {
Debug.logVerbose("Adding FTL shared var " + key + " with value " + value, module);
}
try {
config.setSharedVariable(key, value);
} catch (Exception e) {
Debug.logError(e, "Could not pre-initialize dynamically loaded shared var: " + key + ": " + e, module);
}
}
}
示例6: registerDirectives
import freemarker.template.Configuration; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
protected static void registerDirectives(Configuration conf, ServletContext servletContext)
throws ServletException, IOException {
// ---------------------------------------------------------------
// Locate directives and add them to the freemarker conf.
// ---------------------------------------------------------------
ModuleLoader loader = App.get().moduleLoader();
Class<WidgetController>[] foundClasses = (Class<WidgetController>[]) loader
.findAllTypesAnnotatedWith(Directive.class, false);
if (foundClasses != null && foundClasses.length > 0) {
for (Class<WidgetController> foundClass : foundClasses) {
try {
Annotation declaredAnnotation = Annotations.declaredAnnotation(foundClass, Directive.class);
if (declaredAnnotation != null) {
// get directive name from annotation (this is the tag
// name that will be used in the template)
String directiveName = ((Directive) declaredAnnotation).value();
if (Str.isEmpty(directiveName))
directiveName = ((Directive) declaredAnnotation).name();
if (!Str.isEmpty(directiveName)) {
// Add it to the configuration as a shared variable
conf.setSharedVariable(directiveName, ModuleInjector.get().getInstance(foundClass));
}
}
} catch (Throwable t) {
RuntimeException re = new RuntimeException(t);
re.setStackTrace(t.getStackTrace());
throw re;
}
}
}
}
示例7: render
import freemarker.template.Configuration; //导入方法依赖的package包/类
public static String render(String templateContent, Map<String, Object> params)
throws IOException, TemplateException {
StringWriter sw = new StringWriter();
App app = App.get();
ApplicationContext appCtx = app.context();
RequestContext reqCtx = appCtx.getRequestContext();
// TODO: replace quick hack with configured value!
Configuration conf = new Configuration();
conf.setDefaultEncoding("UTF-8");
if (reqCtx != null && reqCtx.getLocale() != null)
conf.setLocale(reqCtx.getLocale());
conf.setSharedVariable("set", new SetDirective());
conf.setSharedVariable("url", app.inject(URLDirective.class));
conf.setSharedVariable("attribute", new AttributeDirective());
conf.setSharedVariable("attribute_exists", new AttributeExistsDirective());
conf.setSharedVariable("attribute_equals", new AttributeEqualsDirective());
conf.setSharedVariable("attribute_has_value", new AttributeHasValueDirective());
conf.setSharedVariable("message", new MessageDirective());
conf.setSharedVariable("print", new PrintDirective());
conf.setSharedVariable("json", new JsonDirective());
conf.setSharedVariable("skin", new SkinDirective());
conf.setSharedVariable("truncate", new TruncateDirective());
Template t = new Template("name", new StringReader(templateContent), conf);
t.process(params, sw);
return sw.toString();
}
示例8: getInternalEngine
import freemarker.template.Configuration; //导入方法依赖的package包/类
protected Configuration getInternalEngine() throws IOException, TemplateException{
try {
BeansWrapper beansWrapper = new BeansWrapper(Configuration.VERSION_2_3_23);
this.templateModel = beansWrapper.getStaticModels().get(String.class.getName());
} catch (TemplateModelException e) {
throw new IOException(e.getMessage(),e.getCause());
}
// 创建 Configuration 实例
Configuration config = new Configuration(Configuration.VERSION_2_3_23);
Properties props = ConfigUtils.filterWithPrefix("docx4j.freemarker.", "docx4j.freemarker.", Docx4jProperties.getProperties(), false);
// FreeMarker will only accept known keys in its setSettings and
// setAllSharedVariables methods.
if (!props.isEmpty()) {
config.setSettings(props);
}
if (this.freemarkerVariables != null && !this.freemarkerVariables.isEmpty()) {
config.setAllSharedVariables(new SimpleHash(this.freemarkerVariables, config.getObjectWrapper()));
}
if (this.defaultEncoding != null) {
config.setDefaultEncoding(this.defaultEncoding);
}
List<TemplateLoader> templateLoaders = new LinkedList<TemplateLoader>(this.templateLoaders);
// Register template loaders that are supposed to kick in early.
if (this.preTemplateLoaders != null) {
templateLoaders.addAll(this.preTemplateLoaders);
}
postProcessTemplateLoaders(templateLoaders);
// Register template loaders that are supposed to kick in late.
if (this.postTemplateLoaders != null) {
templateLoaders.addAll(this.postTemplateLoaders);
}
TemplateLoader loader = getAggregateTemplateLoader(templateLoaders);
if (loader != null) {
config.setTemplateLoader(loader);
}
//config.setClassLoaderForTemplateLoading(classLoader, basePackagePath);
//config.setCustomAttribute(name, value);
//config.setDirectoryForTemplateLoading(dir);
//config.setServletContextForTemplateLoading(servletContext, path);
//config.setSharedVariable(name, value);
//config.setSharedVariable(name, tm);
config.setSharedVariable("fmXmlEscape", new XmlEscape());
config.setSharedVariable("fmHtmlEscape", new HtmlEscape());
//config.setSharedVaribles(map);
return config;
}
示例9: main
import freemarker.template.Configuration; //导入方法依赖的package包/类
public static void main(String[] args) {
Configuration config = new Configuration();
try {
config.setClassForTemplateLoading(HelloFreeMarker.class, "");
// 去掉int型输出时的逗号, 例如: 123,456
// config.setNumberFormat("#"); // config.setNumberFormat("0"); 也可以
config.setNumberFormat("#0.#####");
config.setDateFormat("yyyy-MM-dd");
config.setTimeFormat("HH:mm:ss");
config.setDateTimeFormat("yyyy-MM-dd HH:mm:ss");
config.setDateTimeFormat("yyyy-MM-dd HH");
config.setSharedVariable("sharedChen", "sharedChen");
config.setSharedVariable("name", "HelloFreeMarker----------------SharedVariable");
Template myTemplate = config.getTemplate("hellofreemarker.ftl");
Map<String,Object> dataModel = new HashMap<String, Object>();
dataModel.put("name", "HelloFreeMarker");
dataModel.put("date1", (new Date()).toString());
dataModel.put("dateO", new Date());
dataModel.put("staticUser", User.class);
dataModel.put("chen","");
User user = User.getUser();
dataModel.put("user",user);
List temp = new ArrayList();
temp.add("1");
temp.add("2");
temp.add("322");
dataModel.put("list", temp);
StringWriter sw = new StringWriter();
myTemplate.process(dataModel, sw);
System.out.println(sw.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
示例10: registerWidgets
import freemarker.template.Configuration; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
protected static void registerWidgets(Configuration conf, ServletContext servletContext)
throws ServletException, IOException {
// ---------------------------------------------------------------
// Locate widgets, wrap them in a freemarker directive
// and add them to the freemarker conf as a shared variable.
// ---------------------------------------------------------------
ModuleLoader loader = App.get().moduleLoader();
Class<WidgetController>[] foundClasses = (Class<WidgetController>[]) loader
.findAllTypesAnnotatedWith(Widget.class, false);
if (foundClasses != null && foundClasses.length > 0) {
for (Class<WidgetController> foundClass : foundClasses) {
try {
Annotation declaredAnnotation = Annotations.declaredAnnotation(foundClass, Widget.class);
if (declaredAnnotation != null) {
// get directive name from annotation (this is the tag
// name that will be used in the template)
String directiveName = ((Widget) declaredAnnotation).value();
if (Str.isEmpty(directiveName))
directiveName = ((Widget) declaredAnnotation).name();
// Wrap the widget in a freemarker compatible directive
WidgetWrapperDirective widgetDirective = new WidgetWrapperDirective(foundClass);
if (!Str.isEmpty(directiveName)) {
// Add it to the configuration as a shared variable
conf.setSharedVariable(directiveName, widgetDirective);
}
}
} catch (Throwable t) {
RuntimeException re = new RuntimeException(t);
re.setStackTrace(t.getStackTrace());
throw re;
}
}
}
}