Java Code Examples for org.thymeleaf.context.Context#setVariable()
The following examples show how to use
org.thymeleaf.context.Context#setVariable() .
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: EmailController.java From SpringAll with MIT License | 6 votes |
@RequestMapping("sendTemplateEmail") public String sendTemplateEmail(String code) { MimeMessage message = null; try { message = jms.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom(from); helper.setTo("[email protected]"); // 接收地址 helper.setSubject("邮件摸板测试"); // 标题 // 处理邮件模板 Context context = new Context(); context.setVariable("code", code); String template = templateEngine.process("emailTemplate", context); helper.setText(template, true); jms.send(message); return "发送成功"; } catch (Exception e) { e.printStackTrace(); return e.getMessage(); } }
Example 2
Source File: TemplateUtilsTest.java From SkaETL with Apache License 2.0 | 6 votes |
@Test public void astext() { StringTemplateResolver templateResolver = new StringTemplateResolver(); templateResolver.setOrder(1); templateResolver.setTemplateMode(TemplateMode.TEXT); // TODO Cacheable or Not ? templateResolver.setCacheable(false); TemplateEngine templateEngine = new TemplateEngine(); templateEngine.setTemplateResolver(templateResolver); Context context = new Context(); context.setVariable("mavar","cela"); String process = templateEngine.process("ceci [[${mavar}]]", context); assertThat(process).isEqualTo("ceci cela"); String notInterpreted = templateEngine.process("ceci <span th:text=\"${mavar}\"></span>", context); assertThat(notInterpreted).isEqualTo("ceci <span th:text=\"${mavar}\"></span>"); }
Example 3
Source File: EmailUtilTest.java From SpringBoot-Home with Apache License 2.0 | 6 votes |
/** * 工具方法测试 * @throws MessagingException */ @Test public void sendMail2() throws MessagingException { MailDomain mail = new MailDomain(); mail.setReceiver("[email protected]"); mail.setSubject("测试模版邮件邮件"); //创建模版正文 Context context = new Context(); // 设置模版需要更换的参数 context.setVariable("verifyCode", "6666"); mail.setEmailTemplateContext(context); // 模版名称(模版位置位于templates目录下) mail.setEmailTemplateName("emailTemplate"); mail.setIsTemplate(true); emailUtil.sendMail(mail); }
Example 4
Source File: SpringbootMailApplication.java From springBoot with MIT License | 6 votes |
@GetMapping("/") public String index() throws MessagingException { //简单邮件 springBootMailService.sendSimpleMail("[email protected]","Simple Mail","第一封简单邮件"); //HTML格式邮件 Context context = new Context(); context.setVariable("username","我的小号"); springBootMailService.sendHtmlMail("[email protected]","HTML Mail",templateEngine.process("mail/mail",context)); //HTML格式邮件,带附件 Context context2 = new Context(); context2.setVariable("username","我的小号(带附件)"); ArrayList<File> files = new ArrayList<>(); //File对象 files.add(new File("C:\\Users\\Administrator\\Desktop\\上传测试.txt")); files.add(new File("C:\\Users\\Administrator\\Desktop\\上传测试2.txt")); springBootMailService.sendAttachmentsMail("[email protected]","Attachments Mail",templateEngine.process("mail/attachment",context2),files); return "欢迎访问 springboot-mail,邮件发送成功!"; }
Example 5
Source File: ThymeleafTest.java From ext-opensource-netty with Mozilla Public License 2.0 | 6 votes |
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 6
Source File: ThymeleafEngine.java From microprofile-starter with Apache License 2.0 | 6 votes |
public String processFile(String file, Set<String> alternatives, Map<String, String> variables) { StringWriter writer = new StringWriter(); Context context = new Context(); for (Map.Entry<String, String> variable : variables.entrySet()) { context.setVariable(variable.getKey(), variable.getValue()); } String fileIndication = filesLocator.findFile(file, alternatives); if ("-1".equals(fileIndication)) { throw new TemplateFileResolutionException(file, alternatives); } engine.process(fileIndication, context, writer); return writer.toString(); }
Example 7
Source File: MailService.java From jhipster-online with Apache License 2.0 | 5 votes |
@Async public void sendEmailFromTemplate(User user, String templateName, String titleKey) { if (user.getEmail() == null) { log.debug("Email doesn't exist for user '{}'", user.getLogin()); return; } 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(templateName, context); String subject = messageSource.getMessage(titleKey, null, locale); sendEmail(user.getEmail(), subject, content, false, true); }
Example 8
Source File: _MailService.java From jhipster-ribbon-hystrix with GNU General Public License v3.0 | 5 votes |
@Async public void sendPasswordResetMail(User user, String baseUrl) { log.debug("Sending password reset e-mail to '{}'", user.getEmail()); Locale locale = Locale.forLanguageTag(user.getLangKey()); Context context = new Context(locale); context.setVariable("user", user); context.setVariable("baseUrl", baseUrl); String content = templateEngine.process("passwordResetEmail", context); String subject = messageSource.getMessage("email.reset.title", null, locale); sendEmail(user.getEmail(), subject, content, false, true); }
Example 9
Source File: MailService.java From gpmr with Apache License 2.0 | 5 votes |
@Async public void sendCreationEmail(User user, String baseUrl) { log.debug("Sending creation e-mail to '{}'", user.getEmail()); Locale locale = Locale.forLanguageTag(user.getLangKey()); Context context = new Context(locale); context.setVariable(USER, user); context.setVariable(BASE_URL, baseUrl); String content = templateEngine.process("creationEmail", context); String subject = messageSource.getMessage("email.activation.title", null, locale); sendEmail(user.getEmail(), subject, content, false, true); }
Example 10
Source File: MailService.java From Spring-5.0-Projects with MIT License | 5 votes |
@Async public void sendEmailFromTemplate(User user, String templateName, String titleKey) { 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(templateName, context); String subject = messageSource.getMessage(titleKey, null, locale); sendEmail(user.getEmail(), subject, content, false, true); }
Example 11
Source File: MailService.java From ehcache3-samples with Apache License 2.0 | 5 votes |
@Async public void sendEmailFromTemplate(User user, String templateName, String titleKey) { 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(templateName, context); String subject = messageSource.getMessage(titleKey, null, locale); sendEmail(user.getEmail(), subject, content, false, true); }
Example 12
Source File: LoginRegisterController.java From DouBiNovel with Apache License 2.0 | 5 votes |
@RequestMapping(method = RequestMethod.POST,value = "/sendRegisterMail") @ResponseBody public MvcResult sendRegisterMail(String email, HttpSession session){ MvcResult result = MvcResult.create(); if (StringUtils.isEmpty(email)){ result.setSuccess(false); result.setMessage("发送验证码失败:邮箱不能为空"); }else if (!email.matches("[\\w\\.\\-]+@([\\w\\-]+\\.)+[\\w\\-]+")) { result.setSuccess(false); result.setMessage("发送验证码失败:邮箱格式不正确"); }else if (userService.getByPhoneOrCodeOrEmail(email)!=null){ result.setSuccess(false); result.setMessage("发送验证码失败:该邮箱已被注册"); }else { try { String code = String.format("%5d",new Random().nextInt(100000)); SystemSetting setting = systemSettingService.getSetting(); Context context = new Context(); context.setVariable("siteName", setting.getSitename()); context.setVariable("mail",email); context.setVariable("code",code); String emailContent = templateEngine.process("mail/register_mail", context); mailService.sendHtmlMail(email,String.format("欢迎注册 %s ",setting.getSitename()),emailContent); session.setAttribute(Const.session.REGISTER_CODE,code); session.setAttribute(Const.session.REGISTER_EMAIL,email); }catch (Exception e){ result.setSuccess(false); if (StringUtils.isEmpty(e.getMessage())){ result.setMessage("发送验证码失败:原因未知"); }else { result.setMessage("发送验证码失败:"+e.getMessage()); } } } return result; }
Example 13
Source File: MailService.java From tutorials with MIT License | 5 votes |
@Async public void sendEmailFromTemplate(User user, String templateName, String titleKey) { 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(templateName, context); String subject = messageSource.getMessage(titleKey, null, locale); sendEmail(user.getEmail(), subject, content, false, true); }
Example 14
Source File: MailService.java From flair-engine with Apache License 2.0 | 5 votes |
@Async public void sendEmailFromTemplate(User user, String templateName, String titleKey) { 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(templateName, context); String subject = messageSource.getMessage(titleKey, null, locale); sendEmail(user.getEmail(), subject, content, false, true); }
Example 15
Source File: MailService.java From OpenIoE with Apache License 2.0 | 5 votes |
@Async public void sendCreationEmail(User user, String baseUrl) { log.debug("Sending creation e-mail to '{}'", user.getEmail()); Locale locale = Locale.forLanguageTag(user.getLangKey()); Context context = new Context(locale); context.setVariable(USER, user); context.setVariable(BASE_URL, baseUrl); String content = templateEngine.process("creationEmail", context); String subject = messageSource.getMessage("email.activation.title", null, locale); sendEmail(user.getEmail(), subject, content, false, true); }
Example 16
Source File: ShiroDialectTest.java From thymeleaf-extras-shiro with Apache License 2.0 | 5 votes |
@Test public void testHasRole() { Subject subjectUnderTest = new Subject.Builder(getSecurityManager()).buildSubject(); setSubject(subjectUnderTest); Context context = new Context(); context.setVariable("roleExpression", "roled"); String result; // Guest user result = templateEngine.process(TEST_TEMPLATE_PATH, context); assertFalse(result.contains("shiro:")); assertFalse(result.contains("HASROLE1")); assertFalse(result.contains("HASROLE2")); // Logged in user 1 subjectUnderTest.login(new UsernamePasswordToken(USER1, PASS1)); assertTrue(subjectUnderTest.hasRole("rolea")); // sanity result = templateEngine.process(TEST_TEMPLATE_PATH, context); assertFalse(result.contains("shiro:")); assertTrue(result.contains("HASROLE1")); assertTrue(result.contains("HASROLE2")); subjectUnderTest.logout(); // Logged in user 2 subjectUnderTest.login(new UsernamePasswordToken(USER2, PASS2)); assertFalse(subjectUnderTest.hasRole("rolea")); // sanity result = templateEngine.process(TEST_TEMPLATE_PATH, context); assertFalse(result.contains("shiro:")); assertFalse(result.contains("HASROLE1")); assertFalse(result.contains("HASROLE2")); subjectUnderTest.logout(); }
Example 17
Source File: MailService.java From tutorials with MIT License | 5 votes |
@Async public void sendCreationEmail(User user) { log.debug("Sending creation 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("creationEmail", context); String subject = messageSource.getMessage("email.activation.title", null, locale); sendEmail(user.getEmail(), subject, content, false, true); }
Example 18
Source File: PDFThymeleafExample.java From tutorials with MIT License | 5 votes |
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: PermissionsMother.java From thymeleaf-extras-shiro with Apache License 2.0 | 4 votes |
private static Context contextWithPermission(Object permissions) { final Context context = new Context(); context.setVariable("permissions", permissions); return context; }
Example 20
Source File: UserService.java From wallride with Apache License 2.0 | 4 votes |
public PasswordResetToken createPasswordResetToken(PasswordResetTokenCreateRequest request) { User user = userRepository.findOneByEmail(request.getEmail()); if (user == null) { throw new EmailNotFoundException(); } LocalDateTime now = LocalDateTime.now(); PasswordResetToken passwordResetToken = new PasswordResetToken(); passwordResetToken.setUser(user); passwordResetToken.setEmail(user.getEmail()); passwordResetToken.setExpiredAt(now.plusHours(24)); passwordResetToken.setCreatedAt(now); passwordResetToken.setCreatedBy(user.toString()); passwordResetToken.setUpdatedAt(now); passwordResetToken.setUpdatedBy(user.toString()); passwordResetToken = passwordResetTokenRepository.saveAndFlush(passwordResetToken); try { Blog blog = blogService.getBlogById(Blog.DEFAULT_ID); String blogTitle = blog.getTitle(LocaleContextHolder.getLocale().getLanguage()); ServletUriComponentsBuilder builder = ServletUriComponentsBuilder.fromCurrentContextPath(); if (blog.isMultiLanguage()) { builder.path("/{language}"); } builder.path("/password-reset"); builder.path("/{token}"); Map<String, Object> urlVariables = new LinkedHashMap<>(); urlVariables.put("language", request.getLanguage()); urlVariables.put("token", passwordResetToken.getToken()); String resetLink = builder.buildAndExpand(urlVariables).toString(); Context ctx = new Context(LocaleContextHolder.getLocale()); ctx.setVariable("passwordResetToken", passwordResetToken); ctx.setVariable("resetLink", resetLink); MimeMessage mimeMessage = mailSender.createMimeMessage(); MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true, "UTF-8"); // true = multipart message.setSubject(MessageFormat.format( messageSourceAccessor.getMessage("PasswordResetSubject", LocaleContextHolder.getLocale()), blogTitle)); message.setFrom(mailProperties.getProperties().get("mail.from")); message.setTo(passwordResetToken.getEmail()); String htmlContent = templateEngine.process("password-reset", ctx); message.setText(htmlContent, true); // true = isHtml mailSender.send(mimeMessage); } catch (MessagingException e) { throw new ServiceException(e); } return passwordResetToken; }