org.thymeleaf.spring5.SpringTemplateEngine Java Examples

The following examples show how to use org.thymeleaf.spring5.SpringTemplateEngine. 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: 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 #3
Source File: WallRideThymeleafConfiguration.java    From wallride with Apache License 2.0 6 votes vote down vote up
@Bean
	public SpringTemplateEngine templateEngine(WallRideThymeleafDialect wallRideThymeleafDialect) {
		SpringTemplateEngine engine = new SpringTemplateEngine();
//		engine.setTemplateResolver(templateResolver());
		Set<ITemplateResolver> templateResolvers = new LinkedHashSet<>();
		templateResolvers.add(homePathTemplateResolver());
		templateResolvers.add(classPathTemplateResolver());
		engine.setTemplateResolvers(templateResolvers);

		Set<IDialect> dialects = new HashSet<>();
		dialects.add(new SpringSecurityDialect());
		dialects.add(new Java8TimeDialect());
		dialects.add(wallRideThymeleafDialect);
		engine.setAdditionalDialects(dialects);
		return engine;
	}
 
Example #4
Source File: SystemIndexController.java    From wallride with Apache License 2.0 6 votes vote down vote up
@Transactional(propagation = Propagation.REQUIRED)
@RequestMapping(value = "/clear-cache", method = RequestMethod.POST)
public String clearCache(
		@PathVariable String language,
		RedirectAttributes redirectAttributes,
		HttpServletRequest request,
		HttpServletResponse response) throws Exception {

	WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(servletContext, "org.springframework.web.servlet.FrameworkServlet.CONTEXT.guestServlet");
	if (context == null) {
		throw new ServiceException("GuestServlet is not ready yet");
	}

	DefaultModelAttributeInterceptor interceptor = context.getBean(DefaultModelAttributeInterceptor.class);
	ModelAndView mv = new ModelAndView("dummy");
	interceptor.postHandle(request, response, this, mv);

	SpringTemplateEngine templateEngine = context.getBean("templateEngine", SpringTemplateEngine.class);
	logger.info("Clear cache started");
	templateEngine.clearTemplateCache();
	logger.info("Clear cache finished");

	redirectAttributes.addFlashAttribute("clearCache", true);
	redirectAttributes.addAttribute("language", language);
	return "redirect:/_admin/{language}/system";
}
 
Example #5
Source File: WebConfig.java    From Spring5Tutorial with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Bean
public SpringTemplateEngine templateEngine(ITemplateResolver templateResolver) {
    SpringTemplateEngine engine = new SpringTemplateEngine();
    engine.setEnableSpringELCompiler(true);
    engine.setTemplateResolver(templateResolver);
    return engine;
}
 
Example #6
Source File: WebMvcConfig.java    From spring-comparing-template-engines with Apache License 2.0 5 votes vote down vote up
@Bean
public SpringTemplateEngine thymeleafTemplateEngine() {
	SpringTemplateEngine templateEngine = new SpringTemplateEngine();
	templateEngine.setTemplateResolver(thymeleafTemplateResolver());

	return templateEngine;
}
 
Example #7
Source File: WebConfig.java    From Spring5Tutorial with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Bean
public ViewResolver viewResolver(SpringTemplateEngine engine) {
    ThymeleafViewResolver resolver = new ThymeleafViewResolver();
    resolver.setTemplateEngine(engine);
    resolver.setCharacterEncoding("UTF-8");
    resolver.setCache(false);
    return resolver;
}
 
Example #8
Source File: SecurityDialectPostProcessor.java    From Learning-Spring-Boot-2.0-Second-Edition with MIT License 5 votes vote down vote up
@Override
public Object postProcessBeforeInitialization(
	Object bean, String beanName) throws BeansException {
	if (bean instanceof SpringTemplateEngine) {
		SpringTemplateEngine engine =
					(SpringTemplateEngine) bean;
		SecurityExpressionHandler<MethodInvocation> handler =
			applicationContext.getBean(
							SecurityExpressionHandler.class);
		SecurityDialect dialect = new SecurityDialect(handler);
		engine.addDialect(dialect);
	}
	return bean;
}
 
Example #9
Source File: SecurityDialectPostProcessor.java    From Learning-Spring-Boot-2.0-Second-Edition with MIT License 5 votes vote down vote up
@Override
public Object postProcessBeforeInitialization(
	Object bean, String beanName) throws BeansException {
	if (bean instanceof SpringTemplateEngine) {
		SpringTemplateEngine engine =
					(SpringTemplateEngine) bean;
		SecurityExpressionHandler<MethodInvocation> handler =
			applicationContext.getBean(
							SecurityExpressionHandler.class);
		SecurityDialect dialect = new SecurityDialect(handler);
		engine.addDialect(dialect);
	}
	return bean;
}
 
Example #10
Source File: MailService.java    From jhipster-online with Apache License 2.0 5 votes vote down vote up
public MailService(JHipsterProperties jHipsterProperties, JavaMailSender javaMailSender,
        MessageSource messageSource, SpringTemplateEngine templateEngine) {

    this.jHipsterProperties = jHipsterProperties;
    this.javaMailSender = javaMailSender;
    this.messageSource = messageSource;
    this.templateEngine = templateEngine;
}
 
Example #11
Source File: TemplateConfig.java    From zhcet-web with Apache License 2.0 5 votes vote down vote up
@Bean(name = "extraTemplateEngine")
public TemplateEngine emailTemplateEngine() {
    final SpringTemplateEngine templateEngine = new SpringTemplateEngine();
    // Resolver for TEXT
    templateEngine.addTemplateResolver(textTemplateResolver());
    // Resolver for HTML
    templateEngine.addTemplateResolver(htmlTemplateResolver());
    // Resolver for JS
    templateEngine.addTemplateResolver(jsTemplateResolver());
    // Resolver for HTML (editable) as string
    templateEngine.addTemplateResolver(stringTemplateResolver());
    return templateEngine;
}
 
Example #12
Source File: SendMailHelper.java    From spring-boot-doma2-sample with Apache License 2.0 5 votes vote down vote up
/**
 * 指定したテンプレートのメール本文を返します。
 *
 * @param template
 * @param objects
 * @return
 */
public String getMailBody(String template, Map<String, Object> objects) {
    val templateEngine = new SpringTemplateEngine();
    templateEngine.setTemplateResolver(templateResolver());

    val context = new Context();
    if (isNotEmpty(objects)) {
        objects.forEach(context::setVariable);
    }

    return templateEngine.process(template, context);
}
 
Example #13
Source File: ThymeleafConfiguration.java    From graviteeio-access-management with Apache License 2.0 5 votes vote down vote up
@Bean
public SpringTemplateEngine getTemplateEngine() {
    SpringTemplateEngine templateEngine = new SpringTemplateEngine();
    templateEngine.setEnableSpringELCompiler(true);

    // set template resolvers
    TemplateResolver overrideTemplateResolver = (TemplateResolver) overrideTemplateResolver();
    overrideTemplateResolver.setTemplateEngine(templateEngine);
    templateEngine.setTemplateResolver(overrideTemplateResolver);
    templateEngine.addTemplateResolver(defaultTemplateResolver());
    return templateEngine;
}
 
Example #14
Source File: ThymeleafConfiguration.java    From graviteeio-access-management with Apache License 2.0 5 votes vote down vote up
@Bean
public ViewResolver getViewResolver(SpringTemplateEngine templateEngine) {
    ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
    viewResolver.setTemplateEngine(templateEngine);
    viewResolver.setCharacterEncoding("UTF-8");
    return viewResolver;
}
 
Example #15
Source File: MailService.java    From ehcache3-samples with Apache License 2.0 5 votes vote down vote up
public MailService(JHipsterProperties jHipsterProperties, JavaMailSender javaMailSender,
        MessageSource messageSource, SpringTemplateEngine templateEngine) {

    this.jHipsterProperties = jHipsterProperties;
    this.javaMailSender = javaMailSender;
    this.messageSource = messageSource;
    this.templateEngine = templateEngine;
}
 
Example #16
Source File: MVCConfig.java    From convergent-ui with Apache License 2.0 5 votes vote down vote up
@Bean
public ThymeleafViewResolver thymeleafViewResolver(WebApplicationContext wac) {
    ThymeleafViewResolver resolver = new ThymeleafViewResolver();
    SpringTemplateEngine templateEngine = new SpringTemplateEngine();
    ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver(wac.getServletContext());
    templateResolver.setPrefix("/WEB-INF/templates/");
    templateResolver.setSuffix(".html");
    templateResolver.setTemplateMode("HTML");
    templateResolver.setCharacterEncoding("UTF-8");
    templateEngine.setTemplateResolver(templateResolver);
    resolver.setTemplateEngine(templateEngine);
    resolver.setOrder(2);
    resolver.setApplicationContext(wac);
    resolver.setCharacterEncoding("UTF-8");

    //Enable the Spring Security Thymeleaf integration
    templateEngine.addDialect(new LayoutDialect());

    // caching
    String[] activeProfiles = env.getActiveProfiles();
    if (Arrays.asList(activeProfiles).contains("dev")) {
        log.info("DEV Profile is active. Disabling template caching.");
        templateResolver.setCacheable(false);
        templateEngine.setCacheManager(null);
        resolver.setCache(false);
    }
    return resolver;
}
 
Example #17
Source File: MVCConfig.java    From convergent-ui with Apache License 2.0 5 votes vote down vote up
@Bean
public ThymeleafViewResolver thymeleafViewResolver(WebApplicationContext wac) {
    ThymeleafViewResolver resolver = new ThymeleafViewResolver();
    SpringTemplateEngine templateEngine = new SpringTemplateEngine();
    ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver(wac.getServletContext());
    templateResolver.setPrefix("/WEB-INF/templates/");
    templateResolver.setSuffix(".html");
    templateResolver.setTemplateMode("HTML");
    templateResolver.setCharacterEncoding("UTF-8");
    templateEngine.setTemplateResolver(templateResolver);
    resolver.setTemplateEngine(templateEngine);
    resolver.setOrder(2);
    resolver.setApplicationContext(wac);
    resolver.setCharacterEncoding("UTF-8");

    //Enable the Spring Security Thymeleaf integration
    templateEngine.addDialect(new LayoutDialect());

    // caching
    String[] activeProfiles = env.getActiveProfiles();
    if (Arrays.asList(activeProfiles).contains("dev")) {
        log.info("DEV Profile is active. Disabling template caching.");
        templateResolver.setCacheable(false);
        templateEngine.setCacheManager(null);
        resolver.setCache(false);
    }
    return resolver;
}
 
Example #18
Source File: OghamSpringBoot2ThymeleafAutoConfigurationTests.java    From ogham with Apache License 2.0 5 votes vote down vote up
@Test
public void oghamWithThymeleafAutoConfigShouldUseSpringTemplateEngine() throws Exception {
	contextRunner = contextRunner.withConfiguration(of(ThymeleafAutoConfiguration.class, OghamSpringBoot2AutoConfiguration.class));
	contextRunner.run((context) -> {
		MessagingService messagingService = context.getBean(MessagingService.class);
		checkEmail(messagingService);
		checkSms(messagingService);
		OghamInternalAssertions.assertThat(messagingService)
			.thymeleaf()
				.all()
					.engine(isA(SpringTemplateEngine.class));
	});
}
 
Example #19
Source File: MailService.java    From 21-points with Apache License 2.0 5 votes vote down vote up
public MailService(JHipsterProperties jHipsterProperties, JavaMailSender javaMailSender,
        MessageSource messageSource, SpringTemplateEngine templateEngine) {

    this.jHipsterProperties = jHipsterProperties;
    this.javaMailSender = javaMailSender;
    this.messageSource = messageSource;
    this.templateEngine = templateEngine;
}
 
Example #20
Source File: WallRideThymeleafConfiguration.java    From wallride with Apache License 2.0 5 votes vote down vote up
@Bean
public ThymeleafViewResolver thymeleafViewResolver(SpringTemplateEngine templateEngine) {
	ThymeleafViewResolver viewResolver = new ExtendedThymeleafViewResolver();
	viewResolver.setTemplateEngine(templateEngine);
	viewResolver.setViewNames(this.thymeleafProperties.getViewNames());
	viewResolver.setCharacterEncoding(this.thymeleafProperties.getEncoding().name());
	viewResolver.setContentType(this.thymeleafProperties.getServlet().getContentType() + ";charset=" + this.thymeleafProperties.getEncoding());
	viewResolver.setCache(false);
	viewResolver.setOrder(2);
	return viewResolver;
}
 
Example #21
Source File: WebAdminConfiguration.java    From wallride with Apache License 2.0 5 votes vote down vote up
@Bean
public SpringTemplateEngine templateEngine() {
	SpringTemplateEngine engine = new SpringTemplateEngine();
	Set<ITemplateResolver> resolvers = new HashSet<>();
	resolvers.add(adminTemplateResolver());
	engine.setTemplateResolvers(resolvers);

	Set<IDialect> dialects = new HashSet<>();
	dialects.add(new SpringSecurityDialect());
	dialects.add(new Java8TimeDialect());
	dialects.add(wallRideThymeleafDialect);
	engine.setAdditionalDialects(dialects);
	return engine;
}
 
Example #22
Source File: WallRideMailConfiguration.java    From wallride with Apache License 2.0 5 votes vote down vote up
@Bean(name = "emailTemplateEngine")
public SpringTemplateEngine emailTemplateEngine() {
	SpringTemplateEngine engine = new SpringTemplateEngine();
	Set<ITemplateResolver> resolvers = new HashSet<>();
	resolvers.add(emailTemplateResolver());
	engine.setTemplateResolvers(resolvers);

	Set<IDialect> dialects = new HashSet<>();
	dialects.add(wallRideThymeleafDialect);
	dialects.add(new Java8TimeDialect());
	engine.setAdditionalDialects(dialects);
	return engine;
}
 
Example #23
Source File: AdminServerNotifierAutoConfiguration.java    From spring-boot-admin with Apache License 2.0 5 votes vote down vote up
@Bean
public TemplateEngine mailNotifierTemplateEngine() {
	SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
	resolver.setApplicationContext(this.applicationContext);
	resolver.setTemplateMode(TemplateMode.HTML);
	resolver.setCharacterEncoding(StandardCharsets.UTF_8.name());

	SpringTemplateEngine templateEngine = new SpringTemplateEngine();
	templateEngine.addTemplateResolver(resolver);
	return templateEngine;
}
 
Example #24
Source File: WebConfig.java    From tutorials with MIT License 5 votes vote down vote up
@Bean
public ISpringTemplateEngine templateEngine() {
  SpringTemplateEngine engine = new SpringTemplateEngine();
  engine.setEnableSpringELCompiler(true);
  engine.setTemplateResolver(templateResolver());
  engine.addDialect(new SpringSecurityDialect());
  return engine;
}
 
Example #25
Source File: WebMVCConfig.java    From tutorials with MIT License 5 votes vote down vote up
private ISpringTemplateEngine templateEngine(ITemplateResolver templateResolver) {
    SpringTemplateEngine engine = new SpringTemplateEngine();
    engine.addDialect(new LayoutDialect(new GroupingStrategy()));
    engine.addDialect(new Java8TimeDialect());
    engine.setTemplateResolver(templateResolver);
    engine.setTemplateEngineMessageSource(messageSource());
    return engine;
}
 
Example #26
Source File: MailService.java    From tutorials with MIT License 5 votes vote down vote up
public MailService(JHipsterProperties jHipsterProperties, JavaMailSender javaMailSender,
        MessageSource messageSource, SpringTemplateEngine templateEngine) {

    this.jHipsterProperties = jHipsterProperties;
    this.javaMailSender = javaMailSender;
    this.messageSource = messageSource;
    this.templateEngine = templateEngine;
}
 
Example #27
Source File: MailService.java    From tutorials with MIT License 5 votes vote down vote up
public MailService(JHipsterProperties jHipsterProperties, JavaMailSender javaMailSender,
        MessageSource messageSource, SpringTemplateEngine templateEngine) {

    this.jHipsterProperties = jHipsterProperties;
    this.javaMailSender = javaMailSender;
    this.messageSource = messageSource;
    this.templateEngine = templateEngine;
}
 
Example #28
Source File: AdminServerNotifierAutoConfiguration.java    From Moss with Apache License 2.0 5 votes vote down vote up
@Bean
public TemplateEngine mailNotifierTemplateEngine() {
    SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
    resolver.setApplicationContext(this.applicationContext);
    resolver.setTemplateMode(TemplateMode.HTML);
    resolver.setCharacterEncoding(StandardCharsets.UTF_8.name());

    SpringTemplateEngine templateEngine = new SpringTemplateEngine();
    templateEngine.addTemplateResolver(resolver);
    return templateEngine;
}
 
Example #29
Source File: WebConfig.java    From Spring5Tutorial with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Bean
public SpringTemplateEngine templateEngine(ITemplateResolver templateResolver) {
    // 建立與設定模版引擎
    SpringTemplateEngine engine = new SpringTemplateEngine();
    engine.setEnableSpringELCompiler(true);
    engine.setTemplateResolver(templateResolver);
    return engine;
}
 
Example #30
Source File: EmailMessageTemplateAutoConfiguration.java    From magic-starter with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Bean(MessageConstants.EMAIL_TEMPLATE_ENGINE_BEAN)
public TemplateEngine templateEngine(@Qualifier(MessageConstants.EMAIL_TEMPLATE_RESOLVER_BEAN) SpringResourceTemplateResolver templateResolver) {
	SpringTemplateEngine engine = new SpringTemplateEngine();
	engine.addDialect(new Java8TimeDialect());
	engine.addTemplateResolver(templateResolver);
	engine.getConfiguration();

	return engine;
}