use of freemarker.template.Template in project SpringStepByStep by JavaProgrammerLB.
the class FreemarkerTest method main.
public static void main(String[] args) {
Configuration config = new Configuration();
try {
String path = new File("").getAbsolutePath();
config.setDirectoryForTemplateLoading(new File(path));
Template template = config.getTemplate("src/test.ftl", "UTF-8");
// 创建数据模型
Map root = new HashMap();
List<User> users = new ArrayList<User>();
User u1 = new User();
u1.setId("123");
u1.setName("王五");
users.add(u1);
User u2 = new User();
u2.setId("423");
u2.setName("李四");
users.add(u2);
User u3 = new User();
u3.setId("333");
u3.setName("张三");
users.add(u3);
root.put("userList", users);
Map product = new HashMap();
root.put("lastProduct", product);
product.put("url", "www.baidu.com");
product.put("name", "green hose");
File file = new File(path + "\\src\\test.html");
if (!file.exists()) {
//System.out.println("file exist");
file.createNewFile();
}
Writer out = new BufferedWriter(new FileWriter(file));
template.process(root, out);
out.flush();
} catch (IOException e) {
e.printStackTrace();
} catch (TemplateException e) {
e.printStackTrace();
}
}
use of freemarker.template.Template in project SpringStepByStep by JavaProgrammerLB.
the class FreeMarkerTemplateEngine method render.
@Override
public String render(ModelAndView modelAndView) {
try {
StringWriter stringWriter = new StringWriter();
Template template = configuration.getTemplate(modelAndView.getViewName());
template.process(modelAndView.getModel(), stringWriter);
return stringWriter.toString();
} catch (IOException e) {
throw new IllegalArgumentException();
} catch (TemplateException e) {
throw new IllegalArgumentException();
}
}
use of freemarker.template.Template in project android by JetBrains.
the class StringEvaluator method evaluate.
/** Evaluates the given expression, with the given set of arguments */
@Nullable
public String evaluate(@NonNull String expression, @NonNull Map<String, Object> inputs) {
try {
myCurrentExpression = expression;
Template inputsTemplate = myFreemarker.getTemplate(expression);
StringWriter out = new StringWriter();
Map<String, Object> args = FreemarkerUtils.createParameterMap(inputs);
inputsTemplate.process(args, out);
out.flush();
return out.toString();
} catch (Exception e) {
return null;
}
}
use of freemarker.template.Template in project jmeter by apache.
the class TemplateVisitor method visitFile.
/*
* (non-Javadoc)
*
* @see java.nio.file.SimpleFileVisitor#visitFile(java.lang.Object,
* java.nio.file.attribute.BasicFileAttributes)
*/
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
// Depending on file extension, copy or process file
String extension = FilenameUtils.getExtension(file.toString());
if (TEMPLATED_FILE_EXT.equalsIgnoreCase(extension)) {
// Process template file
String templatePath = source.relativize(file).toString();
Template template = configuration.getTemplate(templatePath);
Path newPath = target.resolve(FilenameUtils.removeExtension(templatePath));
try (FileOutputStream stream = new FileOutputStream(newPath.toString());
Writer writer = new OutputStreamWriter(stream, StandardCharsets.UTF_8);
BufferedWriter bufferedWriter = new BufferedWriter(writer)) {
template.process(data, bufferedWriter);
} catch (TemplateException ex) {
throw new IOException(ex);
}
} else {
// Copy regular file
Path newFile = target.resolve(source.relativize(file));
Files.copy(file, newFile, StandardCopyOption.REPLACE_EXISTING);
}
return FileVisitResult.CONTINUE;
}
use of freemarker.template.Template in project sling by apache.
the class FreemarkerScriptEngine method eval.
public Object eval(Reader reader, ScriptContext scriptContext) throws ScriptException {
final Bindings bindings = scriptContext.getBindings(ScriptContext.ENGINE_SCOPE);
final SlingScriptHelper helper = (SlingScriptHelper) bindings.get(SlingBindings.SLING);
if (helper == null) {
throw new ScriptException("SlingScriptHelper missing from bindings");
}
// ensure GET request
if (!"GET".equals(helper.getRequest().getMethod())) {
throw new ScriptException("FreeMarker templates only support GET requests");
}
freemarkerScriptEngineFactory.getTemplateModels().forEach(bindings::put);
final String scriptName = helper.getScript().getScriptResource().getPath();
try {
final Template template = new Template(scriptName, reader, configuration);
template.process(bindings, scriptContext.getWriter());
} catch (Throwable t) {
final String message = String.format("Failure processing FreeMarker template %s.", scriptName);
logger.error(message, t);
throw new ScriptException(message);
}
return null;
}
Aggregations