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

The following examples show how to use org.thymeleaf.templateresolver.ClassLoaderTemplateResolver#setSuffix() . 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: 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 2
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 3
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 4
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 5
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 6
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 7
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 8
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 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: 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 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: 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 18
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 19
Source File: SysUserModalController.java    From danyuan-application with Apache License 2.0 4 votes vote down vote up
@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;
}
 
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;
}