<dependency> <groupId>org.jooby</groupId> <artifactId>jooby</artifactId> <version>2.8.6</version> </dependency> import freemarker.template.Configuration; import freemarker.template.TemplateException; import freemarker.template.TemplateExceptionHandler; import org.jooby.Jooby; import org.jooby.flyway.Flywaydb; import org.jooby.hbm.Hbm; public class MyApp extends Jooby { { use(new Hbm("jdbc:h2:mem:testdb", "sa", "")); use(new Flywaydb("db/migration")); use(new TemplateEngine() { @Override public String name() { return "freemarker"; } @Override public void render(Context context, String view, ModelAndView model) throws Exception { Configuration cfg = new Configuration(Configuration.VERSION_2_3_31); cfg.setClassForTemplateLoading(this.getClass(), "/templates"); cfg.setDefaultEncoding("UTF-8"); cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER); Template template = cfg.getTemplate(view); Writer writer = new StringWriter(); template.process(model, writer); context.send(writer.toString()); } }); get("/", () -> Results.html("index.ftl", "Welcome to Jooby!")); } public static void main(String[] args) { run(MyApp::new, args); } } html <!DOCTYPE html> <html> <head> </head> <body> <h1>${message}</h1> </body> </html>


上一篇:
下一篇:
切换中文