- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是我的代码,我用它来使用 java spring 和 thymeleaf 模板引擎发送 html 电子邮件。
@Service
public class EmailServiceImpl implements EmailService {
private static final String EMAIL_SIMPLE_TEMPLATE_NAME = "html/html";
@Value("${email.user.register.body}")
private String USER_REGISTER_MESSAGE_BODY;
@Value("${email.user.register.subject}")
private String USER_REGISTER_MESSAGE_SUBJECT;
@Value("${mailSender.address}")
private String SENDER_EMAIL_ADDRESS;
@Autowired
private JavaMailSender mailSender;
@Autowired
private TemplateEngine templateEngine;
@Override
public void sendEmail(MimeMessagePreparator preparator) {
mailSender.send(preparator);
}
@Async
@Override
public void sendUserRegisterEmail(String receiver, String receiverEmailAddress){
MimeMessagePreparator preparator = new MimeMessagePreparator() {
public void prepare(MimeMessage mimeMessage) throws Exception {
MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
message.setSubject(USER_REGISTER_MESSAGE_SUBJECT);
message.setTo(receiverEmailAddress);
message.setFrom(SENDER_EMAIL_ADDRESS);
message.setText(String.format(USER_REGISTER_MESSAGE_BODY, receiver));
}
};
sendEmail(preparator);
}
public void sendMailWithInline(
final String recipientName, final String recipientEmail, final String imageResourceName,
final byte[] imageBytes, final String imageContentType, final Locale locale)
throws MessagingException {
// Prepare the evaluation context
final Context ctx = new Context(locale);
ctx.setVariable("name", recipientName);
ctx.setVariable("subscriptionDate", new Date());
ctx.setVariable("hobbies", Arrays.asList("Cinema", "Sports", "Music"));
ctx.setVariable("imageResourceName", imageResourceName); // so that we can reference it from HTML
// Prepare message using a Spring helper
final MimeMessage mimeMessage = this.mailSender.createMimeMessage();
final MimeMessageHelper message =
new MimeMessageHelper(mimeMessage, true, "UTF-8"); // true = multipart
message.setSubject("Example HTML email with inline image");
message.setFrom("adbuylk@gmail.com");
message.setTo(recipientEmail);
// Create the HTML body using Thymeleaf
final String htmlContent = this.templateEngine.process(EMAIL_SIMPLE_TEMPLATE_NAME, ctx);
message.setText(htmlContent, true); // true = isHtml
// Add the inline image, referenced from the HTML code as "cid:${imageResourceName}"
final InputStreamSource imageSource = new ByteArrayResource(imageBytes);
message.addInline(imageResourceName, imageSource, imageContentType);
// Send mail
this.mailSender.send(mimeMessage);
}
我在尝试使用 Jetty 和 Intellij idea 运行它时收到以下错误。
[WARNING] Failed startup of context o.e.j.m.p.JettyWebAppContext@23321be7{/adsops,file:///E:/Projects/ADpost/ops/dev/src/main/webapp/,STARTING}{file:///E:/Projects/ADpost/ops/dev/src/main/webapp/}
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'securityConfiguration': Unsatisfied dependency expressed through field 'userDetailsService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'applicationUserServiceImpl': Unsatisfied dependency expressed through field 'emailService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'emailServiceImpl': Unsatisfied dependency expressed through field 'templateEngine'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'templateEngine' defined in com.vlclabs.adsops.configuration.WebApplicationConfiguration: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.thymeleaf.spring4.SpringTemplateEngine]: Factory method 'templateEngine' threw exception; nested exception is java.lang.NoClassDefFoundError: org/thymeleaf/dialect/IExpressionEnhancingDialect
这些是依赖项
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring4</artifactId>
<version>${thymeleaf.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-conditionalcomments</artifactId>
<version>${thymeleaf-extras-conditionalcomments.version}</version>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-java8time</artifactId>
<version>${thymeleaf-extras-java8time.version}</version>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity4</artifactId>
<version>${thymeleaf-extras-springsecurity4.version}</version>
</dependency>
在尝试查看错误消息时,我了解该错误可能是由于我在添加模板引擎时犯的错误而发生的。然而我尝试了好几天都无法修复它。请帮忙
最佳答案
问题可能是您混合了不兼容的版本。您可以通过运行以下命令来检查您的版本:
mvn dependency:tree
(如果您有 Eclipse,您可以打开 pom 文件并切换到“Dependency Hierarchy”选项卡以获得相同的结果。)
特别检查这些:
这两个应该具有相同的版本。如果没有,请调整您的依赖项。
关于java - 无法在 thymeleaf 中 Autowiring TemplateEngine?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46057413/
我正在构建一个 SpringBoot 应用程序,该应用程序从数据库加载国际化消息。(Followed this tutorial) 由此,我必须创建一个 ThymeleafConfiguration
我对 TemplateEngine 有疑问。 我有邮件模板解析器,它根据名称填充 thymeleaf 模板。当我不指定子目录时它效果很好。方法如下: private String getMailFro
我正在使用 spring 在 Java 中开发一个 Web 应用程序。 此应用程序包含 JavaScript 中的 Ajax 调用,该调用请求 html 代码,然后将其插入到 html 文档中。 为了
我使用 thymeleaf 模板引擎来构建对 SOAP 服务的请求。执行发送的类如下所示: import java.io.StringReader; import java.util.*; impor
这是我的代码,我用它来使用 java spring 和 thymeleaf 模板引擎发送 html 电子邮件。 @Service public class EmailServiceImpl imple
我面临这样一个场景,我需要处理一个 HTML 模板 (index.html),该模板是通过 Thymeleaf TemplateEngine 使用我的数据 POJO 从 ZIP 文件(通过 ZipIn
坦率地说,我正在寻找一个离线模板引擎,以使用 xpath 语法将来自 xml 数据库的信息应用到特殊的文本文件中。 我的文字不太好,举个例子更容易明确。所以我的db.xml好像: 和 f
我是一名优秀的程序员,十分优秀!