org.thymeleaf.templateresolver.ClassLoaderTemplateResolver Java Examples

The following examples show how to use org.thymeleaf.templateresolver.ClassLoaderTemplateResolver. 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: MailNotifierTest.java    From Moss with Apache License 2.0 6 votes vote down vote up
@Before
public void setup() {
    repository = mock(InstanceRepository.class);
    when(repository.find(instance.getId())).thenReturn(Mono.just(instance));

    sender = mock(JavaMailSender.class);
    when(sender.createMimeMessage()).thenAnswer(args -> new MimeMessage(Session.getInstance(new Properties())));

    SpringTemplateEngine templateEngine = new SpringTemplateEngine();
    ClassLoaderTemplateResolver resolver = new ClassLoaderTemplateResolver();
    resolver.setTemplateMode(TemplateMode.HTML);
    resolver.setCharacterEncoding(StandardCharsets.UTF_8.name());
    templateEngine.addTemplateResolver(resolver);

    notifier = new MailNotifier(sender, repository, templateEngine);
    notifier.setTo(new String[]{"[email protected]"});
    notifier.setCc(new String[]{"[email protected]"});
    notifier.setFrom("SBA <[email protected]>");
    notifier.setBaseUrl("http://localhost:8080");
    notifier.setTemplate("/META-INF/spring-boot-admin-server/mail/status-changed.html");
}
 
Example #2
Source File: ThymeleafTest.java    From ext-opensource-netty with Mozilla Public License 2.0 6 votes vote down vote up
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 #3
Source File: HttpResourceThymeleaf.java    From ext-opensource-netty with Mozilla Public License 2.0 6 votes vote down vote up
@Override
protected ByteBuf buildRes(String resPath, Map<String, Object> parameters) {
	if (!isThymeleafFile(resPath)) {
		
		return super.buildRes(resPath, parameters);
	} else {
		try {
			Context context = new Context();
			context.setVariables(parameters);

			if (!engine.isInitialized()) {
				ClassLoaderTemplateResolver resolver = new ClassLoaderTemplateResolver();
				resolver.setPrefix(this.getRootDir());
				//设置不缓存
				resolver.setCacheable(false);
				engine.setTemplateResolver(resolver);
			}

			String htmlContext = engine.process(resPath, context);
			return Unpooled.copiedBuffer(htmlContext, CharsetUtil.UTF_8);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return null;
	}
}
 
Example #4
Source File: SpringMailTestConfiguration.java    From konker-platform with Apache License 2.0 6 votes vote down vote up
@Bean
  public SpringTemplateEngine templateEngine() {
  	SpringTemplateEngine mock = spy(SpringTemplateEngine.class);
  	
  	final ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
templateResolver.setOrder(2);
templateResolver.setResolvablePatterns(Collections.singleton("html/*"));
templateResolver.setPrefix("/mail/");
templateResolver.setSuffix(".html");
templateResolver.setTemplateMode(TemplateMode.HTML);
templateResolver.setCharacterEncoding("UTF-8");
templateResolver.setCacheable(false);
  	mock.addTemplateResolver(templateResolver);
  	
  	final ClassLoaderTemplateResolver templateResolverTxt = new ClassLoaderTemplateResolver();
templateResolverTxt.setOrder(1);
templateResolverTxt.setResolvablePatterns(Collections.singleton("text/*"));
templateResolverTxt.setPrefix("/mail/");
templateResolverTxt.setSuffix(".txt");
templateResolverTxt.setTemplateMode(TemplateMode.TEXT);
templateResolverTxt.setCharacterEncoding("UTF-8");
templateResolverTxt.setCacheable(false);
mock.addTemplateResolver(templateResolverTxt);
  	
return mock;
  }
 
Example #5
Source File: MailService.java    From tutorial with MIT License 6 votes vote down vote up
private String getMailBodyFromTemplate(String templateName, Map<String, Object> vars){
    ClassLoaderTemplateResolver resolver = new ClassLoaderTemplateResolver();
    resolver.setPrefix("email-templates/");     //模板所在目录,相对于当前classloader的classpath。
    resolver.setSuffix(".html");                //模板文件后缀
    TemplateEngine templateEngine = new TemplateEngine();
    templateEngine.setTemplateResolver(resolver);

    Context context = new Context();
    for(Iterator<String> iterator = vars.keySet().iterator(); iterator.hasNext();){
        String key = iterator.next();
        context.setVariable(key, vars.get(key));
    }

    StringWriter writer = new StringWriter();
    templateEngine.process("example", context, writer);
    return writer.toString();
}
 
Example #6
Source File: SwaggerMappingSupport.java    From swagger with Apache License 2.0 6 votes vote down vote up
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 #7
Source File: MailNotifierTest.java    From spring-boot-admin with Apache License 2.0 6 votes vote down vote up
@BeforeEach
public void setup() {
	repository = mock(InstanceRepository.class);
	when(repository.find(instance.getId())).thenReturn(Mono.just(instance));

	sender = mock(JavaMailSender.class);
	when(sender.createMimeMessage()).thenAnswer((args) -> new MimeMessage(Session.getInstance(new Properties())));

	SpringTemplateEngine templateEngine = new SpringTemplateEngine();
	ClassLoaderTemplateResolver resolver = new ClassLoaderTemplateResolver();
	resolver.setTemplateMode(TemplateMode.HTML);
	resolver.setCharacterEncoding(StandardCharsets.UTF_8.name());
	templateEngine.addTemplateResolver(resolver);

	notifier = new MailNotifier(sender, repository, templateEngine);
	notifier.setTo(new String[] { "[email protected]" });
	notifier.setCc(new String[] { "[email protected]" });
	notifier.setFrom("SBA <[email protected]>");
	notifier.setBaseUrl("http://localhost:8080");
	notifier.setTemplate("/META-INF/spring-boot-admin-server/mail/status-changed.html");
}
 
Example #8
Source File: Config.java    From tutorials with MIT License 5 votes vote down vote up
@Bean
public ITemplateResolver templateResolver() {
    ClassLoaderTemplateResolver resolver = new ClassLoaderTemplateResolver();
    resolver.setPrefix("templates/books/");
    resolver.setSuffix(".html");
    resolver.setTemplateMode(TemplateMode.HTML);
    resolver.setCharacterEncoding("UTF-8");
    return resolver;
}
 
Example #9
Source File: SpringMailConfig.java    From konker-platform with Apache License 2.0 5 votes vote down vote up
private ITemplateResolver stringTemplateResolver() {
	final ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
	templateResolver.setOrder(3);
	templateResolver.setTemplateMode(TemplateMode.HTML5);
	templateResolver.setCacheable(false);
	return templateResolver;
}
 
Example #10
Source File: ThymeleafTemplateProcessorImpl.java    From codenvy with Eclipse Public License 1.0 5 votes vote down vote up
@Inject
public ThymeleafTemplateProcessorImpl() {
  final ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
  templateResolver.setTemplateMode(HTML);
  templateResolver.setSuffix(".html");
  templateResolver.setCacheTTLMs(3600000L);
  this.templateEngine = new TemplateEngine();
  this.templateEngine.setTemplateResolver(templateResolver);
}
 
Example #11
Source File: SiteGenerator.java    From contribution with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Creates thymeleaf template engine.
 *
 * @return template engine.
 */
private static TemplateEngine getTemplateEngine() {
    final ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
    templateResolver.setTemplateMode("HTML");
    templateResolver.setPrefix("/");
    templateResolver.setSuffix(".template");
    final TemplateEngine tplEngine = new TemplateEngine();
    tplEngine.setTemplateResolver(templateResolver);
    return tplEngine;
}
 
Example #12
Source File: ThymeleafTemplateEngine.java    From spark-template-engines with Apache License 2.0 5 votes vote down vote up
private static ITemplateResolver createDefaultTemplateResolver(String prefix, String suffix) {
    final ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
    templateResolver.setTemplateMode(TemplateMode.HTML);

    templateResolver.setPrefix(
            prefix != null ? prefix : DEFAULT_PREFIX
    );

    templateResolver.setSuffix(
            suffix != null ? suffix : DEFAULT_SUFFIX
    );

    templateResolver.setCacheTTLMs(DEFAULT_CACHE_TTL_MS);
    return templateResolver;
}
 
Example #13
Source File: SpringMailConfig.java    From konker-platform with Apache License 2.0 5 votes vote down vote up
private ITemplateResolver htmlTemplateResolver() {
	final ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
	templateResolver.setOrder(2);
	templateResolver.setResolvablePatterns(Collections.singleton("html/*"));
	templateResolver.setPrefix("/mail/");
	templateResolver.setSuffix(".html");
	templateResolver.setTemplateMode(TemplateMode.HTML);
	templateResolver.setCharacterEncoding("UTF-8");
	templateResolver.setCacheable(false);
	return templateResolver;
}
 
Example #14
Source File: Config.java    From tutorials with MIT License 5 votes vote down vote up
@Bean
public ITemplateResolver templateResolver() {
    ClassLoaderTemplateResolver resolver 
        = new ClassLoaderTemplateResolver();
    resolver.setPrefix("templates/sessionattrs/");
    resolver.setSuffix(".html");
    resolver.setTemplateMode(TemplateMode.HTML);
    resolver.setCharacterEncoding("UTF-8");
    return resolver;
}
 
Example #15
Source File: PDFThymeleafExample.java    From tutorials with MIT License 5 votes vote down vote up
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 #16
Source File: PDFThymeleafUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
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 #17
Source File: ShiroDialectTest.java    From thymeleaf-extras-shiro with Apache License 2.0 5 votes vote down vote up
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 #18
Source File: AbstractThymeleafShiroDialectTest.java    From thymeleaf-extras-shiro with Apache License 2.0 5 votes vote down vote up
private static void setupThymeleaf() {
    ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
    templateResolver.setCacheable(false);
    templateResolver.setCharacterEncoding(Charsets.UTF_8.name());
    templateResolver.setTemplateMode(TemplateMode.HTML);

    templateEngine = new TemplateEngine();
    templateEngine.setTemplateResolver(templateResolver);
    final ShiroDialect dialect = new ShiroDialect();
    templateEngine.addDialect(dialect.getPrefix(), dialect);

}
 
Example #19
Source File: SpringMailConfig.java    From konker-platform with Apache License 2.0 5 votes vote down vote up
private ITemplateResolver textTemplateResolver() {
	final ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
	templateResolver.setOrder(1);
	templateResolver.setResolvablePatterns(Collections.singleton("text/*"));
	templateResolver.setPrefix("/mail/");
	templateResolver.setSuffix(".txt");
	templateResolver.setTemplateMode(TemplateMode.TEXT);
	templateResolver.setCharacterEncoding("UTF-8");
	templateResolver.setCacheable(false);
	return templateResolver;
}
 
Example #20
Source File: ThymeleafConfiguration.java    From graviteeio-access-management with Apache License 2.0 5 votes vote down vote up
private ITemplateResolver defaultTemplateResolver() {
    ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
    templateResolver.setPrefix("/views/");
    templateResolver.setSuffix(".html");
    templateResolver.setTemplateMode("HTML");
    return templateResolver;
}
 
Example #21
Source File: ThymeleafConfiguration.java    From graviteeio-access-management with Apache License 2.0 5 votes vote down vote up
private ITemplateResolver defaultTemplateResolver() {
    ClassLoaderTemplateResolver templateResolver = new CustomClassLoaderTemplateResolver();
    templateResolver.setPrefix("/webroot/views/");
    templateResolver.setSuffix(".html");
    templateResolver.setTemplateMode("HTML");
    return templateResolver;
}
 
Example #22
Source File: ThymeleafTemplateEngine.java    From enkan with Eclipse Public License 1.0 5 votes vote down vote up
private ITemplateResolver createDefaultTemplateResolver() {
    if (classLoader == null) {
        classLoader = Thread.currentThread().getContextClassLoader();
    }
    ClassLoaderTemplateResolver resolver = new ClassLoaderTemplateResolver(classLoader);
    resolver.setTemplateMode(TemplateMode.HTML);
    resolver.setPrefix(prefix);
    resolver.setSuffix(suffix);
    resolver.setCharacterEncoding(encoding);
    return resolver;
}
 
Example #23
Source File: TemplateConfig.java    From zhcet-web with Apache License 2.0 5 votes vote down vote up
private ITemplateResolver jsTemplateResolver() {
    final ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
    templateResolver.setOrder(3);
    templateResolver.setResolvablePatterns(Collections.singleton("js/*"));
    templateResolver.setPrefix(TEMPLATE_PREFIX);
    templateResolver.setSuffix(".js");
    templateResolver.setTemplateMode(TemplateMode.JAVASCRIPT);
    templateResolver.setCharacterEncoding(TEMPLATE_ENCODING);
    templateResolver.setCacheable(thymeleafProperties.isCache());
    return templateResolver;
}
 
Example #24
Source File: TemplateConfig.java    From zhcet-web with Apache License 2.0 5 votes vote down vote up
private ITemplateResolver htmlTemplateResolver() {
    final ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
    templateResolver.setOrder(2);
    templateResolver.setResolvablePatterns(Collections.singleton("html/*"));
    templateResolver.setPrefix(TEMPLATE_PREFIX);
    templateResolver.setSuffix(".html");
    templateResolver.setTemplateMode(TemplateMode.HTML);
    templateResolver.setCharacterEncoding(TEMPLATE_ENCODING);
    templateResolver.setCacheable(thymeleafProperties.isCache());
    return templateResolver;
}
 
Example #25
Source File: TemplateConfig.java    From zhcet-web with Apache License 2.0 5 votes vote down vote up
private ITemplateResolver textTemplateResolver() {
    final ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
    templateResolver.setOrder(1);
    templateResolver.setResolvablePatterns(Collections.singleton("text/*"));
    templateResolver.setPrefix(TEMPLATE_PREFIX);
    templateResolver.setSuffix(".txt");
    templateResolver.setTemplateMode(TemplateMode.TEXT);
    templateResolver.setCharacterEncoding(TEMPLATE_ENCODING);
    templateResolver.setCacheable(thymeleafProperties.isCache());
    return templateResolver;
}
 
Example #26
Source File: GithubPullRequestReporter.java    From cover-checker with Apache License 2.0 5 votes vote down vote up
private void templateInit() {
    ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
    templateResolver.setTemplateMode(TemplateMode.TEXT);
    templateResolver.setPrefix("/templates/");
    templateResolver.setSuffix(".md");
    templateResolver.setCacheable(false);

    templateEngine = new TemplateEngine();
    templateEngine.setTemplateResolver(templateResolver);
}
 
Example #27
Source File: MailUtils.java    From pacbot with Apache License 2.0 5 votes vote down vote up
/**
* 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 #28
Source File: ThymeleafConfig.java    From Project with Apache License 2.0 5 votes vote down vote up
@Bean
	public ClassLoaderTemplateResolver emailTemplateResolver() {
		ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
		templateResolver.setPrefix("/");
		templateResolver.setTemplateMode("HTML5");
		//减轻thymeleaf对HTML语法的疯狂检查
//	    templateResolver.setTemplateMode("LEGACYHTML5");
		templateResolver.setCharacterEncoding("UTF-8");
	    templateResolver.setOrder(1);
		return templateResolver;
	}
 
Example #29
Source File: Test.java    From code with Apache License 2.0 5 votes vote down vote up
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 #30
Source File: ClassPathResolverAdapter.java    From ogham with Apache License 2.0 4 votes vote down vote up
@Override
public ITemplateResolver adapt(ResourceResolver resolver) {
	ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
	applyOptions(templateResolver);
	return templateResolver;
}