Java Code Examples for org.thymeleaf.templateresolver.ClassLoaderTemplateResolver#setPrefix()

The following examples show how to use org.thymeleaf.templateresolver.ClassLoaderTemplateResolver#setPrefix() . 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: 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 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: 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 4
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 5
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 6
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 7
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 8
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 9
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 10
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 11
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 12
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 13
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 14
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 15
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 16
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 17
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 18
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 19
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 20
Source File: SysUserModalController.java    From danyuan-application with Apache License 2.0 4 votes vote down vote up
@RequestMapping(path = "/writeResume", method = RequestMethod.POST)
public BaseResult<String> writeResume(@RequestBody SysUserModal info) throws IllegalArgumentException, IllegalAccessException {
	BaseResult<String> result = new BaseResult<>();
	try {
		// 构造模板引擎
		ClassLoaderTemplateResolver resolver = new ClassLoaderTemplateResolver();
		resolver.setPrefix("templates/");// 模板所在目录,相对于当前classloader的classpath。
		resolver.setSuffix(".html");// 模板文件后缀
		// resolver.setTemplateMode("HTML5");
		TemplateEngine templateEngine = new TemplateEngine();
		templateEngine.setTemplateResolver(resolver);
		
		// 构造上下文(Model)
		Context context = new Context();
		// 基本
		SysUserBaseInfo base = sysUserBaseService.findById(info.getUserUuid());
		context.setVariable("base", base);
		// 教育
		SysUserEducation education = new SysUserEducation();
		education.setUserUuid(info.getUserUuid());
		List<SysUserEducation> educations = sysUserEducationService.findAll(education);
		context.setVariable("educations", educations);
		// 技能
		SysUserSkill skill = new SysUserSkill();
		skill.setUserUuid(info.getUserUuid());
		List<SysUserSkill> skills = sysUserSkillService.findAll(skill);
		context.setVariable("skills", skills);
		
		// 工作经验
		SysUserWorkExpreience workExpreience = new SysUserWorkExpreience();
		workExpreience.setUserUuid(info.getUserUuid());
		List<SysUserWorkExpreience> workExpreiences = sysUserWorkExpreienceService.findAll(workExpreience);
		context.setVariable("workExpreiences", workExpreiences);
		// 项目经验
		SysUserProject project = new SysUserProject();
		project.setUserUuid(info.getUserUuid());
		List<SysUserProject> projects = sysUserProjectService.findAll(project);
		context.setVariable("projects", projects);
		
		// 评价
		SysUserEvaluate evaluate = new SysUserEvaluate();
		evaluate.setUserUuid(info.getUserUuid());
		evaluate.setType("自我评价");
		evaluate = sysUserEvaluateService.findOne(evaluate);
		context.setVariable("evaluate", evaluate);
		
		// 渲染模板
		String dirString = System.getProperty("user.dir");
		FileWriter write = new FileWriter(dirString + "/file/" + info.getUserUuid() + ".html");
		SysModalInfo sysModalInfo = sysModalInfoService.findOne(new SysModalInfo(info.getModalUuid()));
		
		templateEngine.process("resume/modal/" + sysModalInfo.getModalFilePath(), context, write);
		// 保存简历路径
		base.setResumePath("/file/" + info.getUserUuid() + ".html");
		sysUserBaseService.save(base);
		result.setData("/" + info.getUserUuid() + ".html");
		result.setCode(200);
	} catch (IOException e) {
		System.err.println(e.getMessage());
		result.setCode(-100);
		result.setMsg(e.getMessage());
	}
	return result;
}