Java Code Examples for org.thymeleaf.TemplateEngine#setTemplateResolver()
The following examples show how to use
org.thymeleaf.TemplateEngine#setTemplateResolver() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GaeInfoServlet.java From java-docs-samples with Apache License 2.0 | 6 votes |
@Override public void init() { // Setup ThymeLeaf ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver(this.getServletContext()); templateResolver.setPrefix("/WEB-INF/templates/"); templateResolver.setSuffix(".html"); templateResolver.setCacheTTLMs(1200000L); // TTL=20m // Cache is set to true by default. Set to false if you want templates to // be automatically updated when modified. templateResolver.setCacheable(true); templateEngine = new TemplateEngine(); templateEngine.setTemplateResolver(templateResolver); }
Example 2
Source File: GaeInfoServlet.java From java-docs-samples with Apache License 2.0 | 6 votes |
@Override public void init() { // Setup ThymeLeaf ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver(this.getServletContext()); templateResolver.setPrefix("/WEB-INF/templates/"); templateResolver.setSuffix(".html"); templateResolver.setCacheTTLMs(Long.valueOf(1200000L)); // TTL=20m // Cache is set to true by default. Set to false if you want templates to // be automatically updated when modified. templateResolver.setCacheable(true); templateEngine = new TemplateEngine(); templateEngine.setTemplateResolver(templateResolver); }
Example 3
Source File: SwaggerMappingSupport.java From swagger with Apache License 2.0 | 6 votes |
public SwaggerMappingSupport(ServletContext servletContext, String urlPatternMapping) throws ServletException { this.contextPath = servletContext.getContextPath(); urlPrefix = contextPath + urlPatternMapping; urlPrefix = urlPrefix.replaceAll("/{2,}", "/"); if (!"/".equals(urlPrefix) && urlPrefix.endsWith("/")) { urlPrefix = urlPrefix.substring(0, urlPrefix.length() - 1); } requestMatchers = Arrays.stream(RESOURCE_PATTERNS) .map(pattern -> String.join("/", this.contextPath, urlPatternMapping, pattern)) .map(pattern -> pattern.replaceAll("/{2,}", "/")) .map(pattern -> new AntPathRequestMatcher(pattern, HttpMethod.GET.name())) .collect(Collectors.toList()); ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver(SwaggerFilter.class.getClassLoader()); templateResolver.setPrefix(TEMPLATES_PATH + "/"); templateResolver.setTemplateMode(TemplateMode.HTML); TemplateEngine templateEngine = new TemplateEngine(); templateEngine.setTemplateResolver(templateResolver); this.templateEngine = templateEngine; }
Example 4
Source File: MetadataServlet.java From java-docs-samples with Apache License 2.0 | 6 votes |
@Override public void init() { // Setup ThymeLeaf ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver(this.getServletContext()); templateResolver.setPrefix("/WEB-INF/templates/"); templateResolver.setSuffix(".html"); templateResolver.setCacheTTLMs(Long.valueOf(1200000L)); // TTL=20m // Cache is set to true by default. Set to false if you want templates to // be automatically updated when modified. templateResolver.setCacheable(true); templateEngine = new TemplateEngine(); templateEngine.setTemplateResolver(templateResolver); }
Example 5
Source File: TemplateUtilsTest.java From SkaETL with Apache License 2.0 | 6 votes |
@Test public void ashtml() { StringTemplateResolver templateResolver = new StringTemplateResolver(); templateResolver.setOrder(1); templateResolver.setTemplateMode(TemplateMode.HTML); // TODO Cacheable or Not ? templateResolver.setCacheable(false); TemplateEngine templateEngine = new TemplateEngine(); templateEngine.setTemplateResolver(templateResolver); Context context = new Context(); context.setVariable("mavar","cela"); String process = templateEngine.process("ceci [[${mavar}]]", context); assertThat(process).isEqualTo("ceci cela"); String variant = templateEngine.process("ceci <span th:text=\"${mavar}\"></span>", context); assertThat(variant).isEqualTo("ceci <span>cela</span>"); }
Example 6
Source File: TemplateUtilsTest.java From SkaETL with Apache License 2.0 | 6 votes |
@Test public void astext() { StringTemplateResolver templateResolver = new StringTemplateResolver(); templateResolver.setOrder(1); templateResolver.setTemplateMode(TemplateMode.TEXT); // TODO Cacheable or Not ? templateResolver.setCacheable(false); TemplateEngine templateEngine = new TemplateEngine(); templateEngine.setTemplateResolver(templateResolver); Context context = new Context(); context.setVariable("mavar","cela"); String process = templateEngine.process("ceci [[${mavar}]]", context); assertThat(process).isEqualTo("ceci cela"); String notInterpreted = templateEngine.process("ceci <span th:text=\"${mavar}\"></span>", context); assertThat(notInterpreted).isEqualTo("ceci <span th:text=\"${mavar}\"></span>"); }
Example 7
Source File: ThymeleafTest.java From ext-opensource-netty with Mozilla Public License 2.0 | 6 votes |
public static void testB() { ClassLoaderTemplateResolver resolver = new ClassLoaderTemplateResolver(); //模板所在目录,相对于当前classloader的classpath。 resolver.setPrefix("static/"); ///模板文件后缀 // resolver.setSuffix(".html"); // resolver.setCacheable(false);//设置不缓存 // resolver.setTemplateMode("HTML5"); TemplateEngine engine = new TemplateEngine(); engine.setTemplateResolver(resolver); Context context = new Context(); context.setVariable("socketurl", "ws"); System.out.println(engine.process("websocket.html", context)); }
Example 8
Source File: MailUtils.java From pacbot with Apache License 2.0 | 5 votes |
/** * Formate common fix body. * * @param silentautoFixTrans the silentauto fix trans * @param ruleParam the rule param * @param resourceOwner the resource owner * @return the string */ public static String formateCommonFixBody(List<AutoFixTransaction> silentautoFixTrans,Map<String, String> ruleParam,ResourceOwner resourceOwner) { TemplateEngine templateEngine = new TemplateEngine(); ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver(); templateResolver.setTemplateMode("HTML"); templateResolver.setSuffix(".html"); templateEngine.setTemplateResolver(templateResolver); List<String> columnsList = Arrays.asList(CommonUtils.getPropValue(PacmanSdkConstants.PACMAN_MAIL_TEMPLATE_COLUMNS + ruleParam.get(PacmanSdkConstants.RULE_ID)).split("\\s*,\\s*")); Context context = new Context(); context.setVariable("columns", columnsList); context.setVariable("resources", silentautoFixTrans); String policyUrl = getPolicyKnowledgeBasePathURL(ruleParam); String name =CommonUtils.getPropValue(PacmanSdkConstants.SEND_EMAIL_SILENT_FIX_ADMIN + ruleParam.get(PacmanSdkConstants.RULE_ID)); if(StringUtils.isNullOrEmpty(name)){ name = resourceOwner.getName(); } String postFixMessage = CommonUtils.getPropValue(PacmanSdkConstants.EMAIL_FIX_MESSAGE_PREFIX + ruleParam.get(PacmanSdkConstants.RULE_ID)); context.setVariable("AUTOFIX_POST_FIX_MESSAGE", postFixMessage); context.setVariable("POLICY_URL", policyUrl); context.setVariable("NAME", "Hello "+name); context.setVariable("RESOURCE_TYPE", " Resource Type : "+ruleParam.get(PacmanSdkConstants.TARGET_TYPE)); context.setVariable("AUTO_FIX_APPLIED", "Total AutoFixs Applied : "+silentautoFixTrans.size()); StringWriter writer = new StringWriter(); if(CommonUtils.getPropValue("pacman.auto.fix.common.email.notifications." + ruleParam.get(PacmanSdkConstants.RULE_ID)).equals("commonTemplate")){ templateEngine.process("/template/autofix-user-notification-action-common.html", context, writer); }else{ templateEngine.process("/template/autofix-silent-autodelete-usernotification-info.html", context, writer); } return writer.toString(); }
Example 9
Source File: ThymeleafViewEngine.java From generator-jvm with MIT License | 5 votes |
@PostConstruct public void postConstruct() { final ServletContextTemplateResolver resolver = new ServletContextTemplateResolver(servletContext); resolver.setPrefix("/WEB-INF/layouts/"); resolver.setSuffix(".html"); resolver.setTemplateMode(TemplateMode.HTML); resolver.setCacheable(false); engine = new TemplateEngine(); engine.setTemplateResolver(resolver); }
Example 10
Source File: DefaultTemplateEngineProducer.java From krazo with Apache License 2.0 | 5 votes |
@Produces @ViewEngineConfig public TemplateEngine getTemplateEngine() { ITemplateResolver resolver = new ServletContextTemplateResolver(this.servletContext); TemplateEngine engine = new TemplateEngine(); engine.setTemplateResolver(resolver); return engine; }
Example 11
Source File: PDFThymeleafUnitTest.java From tutorials with MIT License | 5 votes |
private String parseThymeleafTemplate() { ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver(); templateResolver.setSuffix(".html"); templateResolver.setTemplateMode(TemplateMode.HTML); TemplateEngine templateEngine = new TemplateEngine(); templateEngine.setTemplateResolver(templateResolver); Context context = new Context(); context.setVariable("to", "Baeldung.com"); return templateEngine.process("thymeleaf_template", context); }
Example 12
Source File: Test.java From code with Apache License 2.0 | 5 votes |
public static void main(String[] args) { // 1.上下文 Context context = new Context(); //创建数据模型 Map<String, Object> dataModel = new HashMap<String, Object>(1); dataModel.put("name", "青橙电商系统"); context.setVariables(dataModel); // 2.准备文件 File dest = new File("d:/test_out.html"); // 3.生成页面 try { PrintWriter writer = new PrintWriter(dest, "UTF-8"); //模板解析器 ClassLoaderTemplateResolver resolver = new ClassLoaderTemplateResolver(); //模板模型 resolver.setTemplateMode(TemplateMode.HTML); //后缀 resolver.setSuffix(".html"); //创建模板引擎 TemplateEngine engine = new TemplateEngine(); //设置模板解析器 engine.setTemplateResolver(resolver); //执行模板引擎 engine.process("test", context, writer); } catch (Exception e) { e.printStackTrace(); } }
Example 13
Source File: HTMLReportWriter.java From jlineup with Apache License 2.0 | 5 votes |
public HTMLReportWriter(FileService fileService) { this.fileService = fileService; templateResolver.setTemplateMode("HTML"); templateResolver.setPrefix("templates/"); templateResolver.setSuffix(".html"); templateEngine = new TemplateEngine(); templateEngine.setTemplateResolver(templateResolver); }
Example 14
Source File: ShiroDialectTest.java From thymeleaf-extras-shiro with Apache License 2.0 | 5 votes |
private static void setupThymeleaf() { ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver(); templateResolver.setCacheable(false); templateResolver.setCharacterEncoding("UTF-8"); templateResolver.setTemplateMode(TemplateMode.HTML); templateEngine = new TemplateEngine(); templateEngine.setTemplateResolver(templateResolver); templateEngine.addDialect("shiro", new ShiroDialect()); }
Example 15
Source File: TemplateEngineSupplierProducer.java From portals-pluto with Apache License 2.0 | 5 votes |
@Bean @Scope(proxyMode = ScopedProxyMode.INTERFACES) public TemplateEngineSupplier getTemplateEngineSupplier(PortletConfig portletConfig, ServletContext servletContext, BeanFactory beanFactory) { TemplateEngine templateEngine = new TemplateEngine(); templateEngine.setMessageResolver(new PortletMessageResolver(portletConfig)); templateEngine.setTemplateResolver(new PortletTemplateResolver(servletContext, new SpringConfigurationSupplier(beanFactory))); return new DefaultTemplateEngineSupplier(templateEngine); }
Example 16
Source File: ThymeleafTest.java From ext-opensource-netty with Mozilla Public License 2.0 | 5 votes |
/** * 使用 Thymeleaf 渲染 HTML * @param template HTML模板 * @param params 参数 * @return 渲染后的HTML */ public static String render(String template,Map<String,Object> params){ Context context = new Context(); context.setVariables(params); TemplateEngine engine=new TemplateEngine(); StringTemplateResolver resolver = new StringTemplateResolver(); engine.setTemplateResolver(resolver); return engine.process(template,context); }
Example 17
Source File: ThymeleafTemplateDelegate.java From reactor-guice with Apache License 2.0 | 5 votes |
@Override public String template(Object handleObject, ModelMap modelMap, String templateName) { String controllerName = handleObject.getClass().getSimpleName(); String templateDirectory = controllerName.toLowerCase().substring(0, controllerName.length() - "handle".length()); templateResolver.setPrefix(this.getClass().getResource("/template/" + templateDirectory).getPath() + "/"); TemplateEngine engine = new TemplateEngine(); engine.setTemplateResolver(templateResolver); Context context = new Context(); context.setVariables(modelMap); return engine.process(templateName, context); }
Example 18
Source File: GenerationRunner.java From rdflint with MIT License | 4 votes |
/** * rdflint generation process. */ void execute(RdfLintParameters params, String targetDir) throws IOException { logger.trace("execute: in"); if (params.getGeneration() == null) { logger.trace("execute: exit"); return; } // clear output long errSize = params.getGeneration().stream().map(g -> { File f = new File(targetDir + "/" + g.getOutput()); if (!f.exists()) { return true; } return f.delete(); }).filter(v -> !v).count(); if (errSize > 0) { throw new IOException("rdflint generation, fail to clear existed output."); } // prepare thymeleaf template engine FileTemplateResolver templateResolver = new FileTemplateResolver(); templateResolver.setTemplateMode("TEXT"); templateResolver.setPrefix(targetDir + "/"); TemplateEngine templateEngine = new TemplateEngine(); templateEngine.setTemplateResolver(templateResolver); // prepare rdf dataset Model m = DatasetLoader.loadRdfSet(params, targetDir); params.getGeneration().forEach(g -> { String q = g.getQuery(); try { // execute query and build result set Query query = QueryFactory.create(q); QueryExecution qe = QueryExecutionFactory.create(query, m); ResultSet results = qe.execSelect(); List<Map<String, String>> lst = new LinkedList<>(); List<String> cols = new LinkedList<>(); while (results.hasNext()) { QuerySolution sol = results.next(); Iterator<String> it = sol.varNames(); cols.clear(); while (it.hasNext()) { cols.add(it.next()); } lst.add(cols.stream() .collect(Collectors.toMap( c -> c, c -> sol.get(c).toString() ))); } // apply template Context ctx = new Context(); ctx.setVariable("params", params); ctx.setVariable("rs", lst); templateEngine.process( g.getTemplate(), ctx, Files.newBufferedWriter(Paths.get(targetDir + "/" + g.getOutput())) ); } catch (Exception ex) { ex.printStackTrace(); // NOPMD } }); logger.trace("execute: out"); }
Example 19
Source File: TemplateEngineSupplierProducer.java From portals-pluto with Apache License 2.0 | 4 votes |
@ApplicationScoped @Produces public TemplateEngineSupplier getTemplateEngineSupplier(PortletConfig portletConfig, ServletContext servletContext, MvcContext mvcContext) { TemplateEngine templateEngine = new TemplateEngine(); templateEngine.setMessageResolver(new PortletMessageResolver(portletConfig)); Configuration configuration = mvcContext.getConfig(); String templateLocation = (String) configuration.getProperty(ViewEngine.VIEW_FOLDER); if (templateLocation == null) { templateLocation = ViewEngine.DEFAULT_VIEW_FOLDER; } templateEngine.setTemplateResolver(new PortletTemplateResolver(servletContext, new CDITemplateLocationSupplier(templateLocation))); return new DefaultTemplateEngineSupplier(templateEngine); }
Example 20
Source File: SysUserModalController.java From danyuan-application with Apache License 2.0 | 4 votes |
@RequestMapping(path = "/showResume/{userUuid}/{resume}") public ModelAndView showResume(@PathVariable("userUuid") String userid, @PathVariable("resume") String resume) { // 构造模板引擎 ClassLoaderTemplateResolver resolver = new ClassLoaderTemplateResolver(); resolver.setPrefix("templates/");// 模板所在目录,相对于当前classloader的classpath。 resolver.setSuffix(".html");// 模板文件后缀 TemplateEngine templateEngine = new TemplateEngine(); templateEngine.setTemplateResolver(resolver); // 构造上下文(Model) ModelAndView view = new ModelAndView("resume/modal/" + resume + ".html"); // 基本 view.addObject("base", sysUserBaseService.findById(userid)); // 教育 SysUserEducation education = new SysUserEducation(); education.setUserUuid(userid); List<SysUserEducation> educations = sysUserEducationService.findAll(education); view.addObject("educations", educations); // 技能 SysUserSkill skill = new SysUserSkill(); skill.setUserUuid(userid); List<SysUserSkill> skills = sysUserSkillService.findAll(skill); view.addObject("skills", skills); // 技能 SysUserWorkExpreience workExpreience = new SysUserWorkExpreience(); workExpreience.setUserUuid(userid); List<SysUserWorkExpreience> workExpreiences = sysUserWorkExpreienceService.findAll(workExpreience); view.addObject("workExpreiences", workExpreiences); // 技能 SysUserProject project = new SysUserProject(); project.setUserUuid(userid); List<SysUserProject> projects = sysUserProjectService.findAll(project); view.addObject("projects", projects); // 评价 SysUserEvaluate evaluate = new SysUserEvaluate(); evaluate.setUserUuid(userid); evaluate.setType("自我评价"); evaluate = sysUserEvaluateService.findOne(evaluate); view.addObject("evaluate", evaluate); // 渲染模板 return view; }