Search in sources :

Example 91 with Context

use of org.thymeleaf.context.Context in project jlineup by otto-de.

the class HTMLReportWriter method renderReport.

String renderReport(String template, List<ScreenshotComparisonResult> screenshotComparisonResults) throws FileNotFoundException {
    ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
    templateResolver.setTemplateMode("HTML");
    templateResolver.setPrefix("templates/");
    templateResolver.setSuffix(".html");
    TemplateEngine templateEngine = new TemplateEngine();
    templateEngine.setTemplateResolver(templateResolver);
    final Map<String, Object> variables = prepareVariablesForReportTemplate(screenshotComparisonResults);
    return templateEngine.process(template, new Context(Locale.US, variables));
}
Also used : Context(org.thymeleaf.context.Context) ClassLoaderTemplateResolver(org.thymeleaf.templateresolver.ClassLoaderTemplateResolver) TemplateEngine(org.thymeleaf.TemplateEngine)

Example 92 with Context

use of org.thymeleaf.context.Context in project judge by zjnu-acm.

the class ResetPasswordController method doPost.

@PostMapping("/resetPassword")
public void doPost(HttpServletRequest request, HttpServletResponse response, @RequestParam(value = "action", required = false) String action, @RequestParam(value = "verify", required = false) String verify, @RequestParam(value = "username", required = false) String username, Locale locale) throws IOException {
    response.setContentType("text/javascript;charset=UTF-8");
    PrintWriter out = response.getWriter();
    HttpSession session = request.getSession(false);
    String word = null;
    if (session != null) {
        word = (String) session.getAttribute("word");
        session.removeAttribute("word");
    }
    if (word == null || !word.equalsIgnoreCase(verify)) {
        out.print("alert('验证码错误');");
        return;
    }
    User user = userMapper.findOne(username);
    if (user == null) {
        out.print("alert('用户不存在');");
        return;
    }
    String email = user.getEmail();
    if (email == null || !email.toLowerCase().matches(ValueCheck.EMAIL_PATTERN)) {
        out.print("alert('该用户未设置邮箱或邮箱格式不对,如需重置密码,请联系管理员!');");
        return;
    }
    try {
        String vc = resetPasswordService.getOrCreate(user.getId());
        String url = getPath(request, "/resetPassword.html?vc=", vc + "&u=", user.getId());
        String title = systemService.getResetPasswordTitle();
        Map<String, Object> map = ImmutableMap.of("url", url, "title", Objects.toString(title, ""));
        String content = templateEngine.process("users/password", new Context(locale, map));
        emailService.send(email, title, content);
    } catch (MailException | MessagingException ex) {
        log.error("fail to send email", ex);
        out.print("alert('邮件发送失败,请稍后再试')");
        return;
    }
    out.print("alert('已经将邮件发送到" + user.getEmail() + ",请点击链接重设密码');");
}
Also used : Context(org.thymeleaf.context.Context) User(cn.edu.zjnu.acm.judge.domain.User) MessagingException(javax.mail.MessagingException) HttpSession(javax.servlet.http.HttpSession) MailException(org.springframework.mail.MailException) PrintWriter(java.io.PrintWriter) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Example 93 with Context

use of org.thymeleaf.context.Context in project DisCal-Discord-Bot by NovaFox161.

the class ThymeleafTemplateEngine method render.

/**
 * Process the specified template (usually the template name).
 * Output will be written into a String that will be returned from calling this method,
 * once template processing has finished.
 *
 * @param modelAndView model and view
 * @param locale       A Locale object represents a specific geographical, political, or cultural region
 * @return processed template
 */
public String render(ModelAndView modelAndView, Locale locale) {
    Object model = modelAndView.getModel();
    if (model instanceof Map) {
        Context context = new Context(locale);
        context.setVariables((Map<String, Object>) model);
        return templateEngine.process(modelAndView.getViewName(), context);
    } else {
        throw new IllegalArgumentException("modelAndView.getModel() must return a java.util.Map");
    }
}
Also used : Context(org.thymeleaf.context.Context) Map(java.util.Map)

Example 94 with Context

use of org.thymeleaf.context.Context in project tutorials by eugenp.

the class MailService method sendActivationEmail.

@Async
public void sendActivationEmail(User user) {
    log.debug("Sending activation e-mail to '{}'", user.getEmail());
    Locale locale = Locale.forLanguageTag(user.getLangKey());
    Context context = new Context(locale);
    context.setVariable(USER, user);
    context.setVariable(BASE_URL, jHipsterProperties.getMail().getBaseUrl());
    String content = templateEngine.process("activationEmail", context);
    String subject = messageSource.getMessage("email.activation.title", null, locale);
    sendEmail(user.getEmail(), subject, content, false, true);
}
Also used : Locale(java.util.Locale) Context(org.thymeleaf.context.Context) Async(org.springframework.scheduling.annotation.Async)

Example 95 with Context

use of org.thymeleaf.context.Context in project tutorials by eugenp.

the class MailService method sendActivationEmail.

@Async
public void sendActivationEmail(User user) {
    log.debug("Sending activation e-mail to '{}'", user.getEmail());
    Locale locale = Locale.forLanguageTag(user.getLangKey());
    Context context = new Context(locale);
    context.setVariable(USER, user);
    context.setVariable(BASE_URL, jHipsterProperties.getMail().getBaseUrl());
    String content = templateEngine.process("activationEmail", context);
    String subject = messageSource.getMessage("email.activation.title", null, locale);
    sendEmail(user.getEmail(), subject, content, false, true);
}
Also used : Locale(java.util.Locale) Context(org.thymeleaf.context.Context) Async(org.springframework.scheduling.annotation.Async)

Aggregations

Context (org.thymeleaf.context.Context)242 Test (org.junit.Test)179 Album (org.thymeleaf.spring5.reactive.data.Album)141 ReactiveDataDriverContextVariable (org.thymeleaf.spring5.context.webflux.ReactiveDataDriverContextVariable)130 TemplateEngine (org.thymeleaf.TemplateEngine)30 Locale (java.util.Locale)24 Async (org.springframework.scheduling.annotation.Async)21 ClassLoaderTemplateResolver (org.thymeleaf.templateresolver.ClassLoaderTemplateResolver)13 SpringTemplateEngine (org.thymeleaf.spring5.SpringTemplateEngine)11 MockServletContext (org.springframework.mock.web.MockServletContext)10 RequestContext (org.springframework.web.servlet.support.RequestContext)10 Test (org.junit.jupiter.api.Test)9 WebContext (org.thymeleaf.context.WebContext)9 MessagingException (javax.mail.MessagingException)7 StringTemplateResolver (org.thymeleaf.templateresolver.StringTemplateResolver)7 DefaultTemplateResolver (org.thymeleaf.templateresolver.DefaultTemplateResolver)6 ServletContext (jakarta.servlet.ServletContext)5 ArrayList (java.util.ArrayList)5 MimeMessage (javax.mail.internet.MimeMessage)5 MimeMessageHelper (org.springframework.mail.javamail.MimeMessageHelper)5