Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无法在thymeleaf中自动连接TemplateEngine?_Java_Spring_Jetty_Thymeleaf_Html Email - Fatal编程技术网

Java 无法在thymeleaf中自动连接TemplateEngine?

Java 无法在thymeleaf中自动连接TemplateEngine?,java,spring,jetty,thymeleaf,html-email,Java,Spring,Jetty,Thymeleaf,Html Email,这是我使用JavaSpring和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_M

这是我使用JavaSpring和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>

org.thymeleaf
百里香
${thymeleaf.version}
编译
org.thymeleaf.extras
百里香附加条件说明
${thymeleaf附加条件注释.version}
org.thymeleaf.extras
thymeleaf-extras-Java8时间
${thymeleaf-extras-java8time.version}
org.thymeleaf.extras
thymeleaf-extras-springsecurity4
${thymeleaf-extras-springsecurity4.version}

在尝试查看错误消息时,我了解到错误可能是由于添加模板引擎时出错造成的。但是我试了好几天都没能修好。请帮助

问题可能是您混用了不兼容的版本。您可以通过运行以下命令来检查您的版本:

mvn dependency:tree
(如果您有Eclipse,您可以打开pom文件并切换到“Dependency Hierarchy”(依赖层次结构)选项卡进行相同的操作。)

特别检查以下各项:

  • org.thymeleaf:thymeleaf-spring4:jar
  • org.thymeleaf:thymeleaf:jar

这两个版本应该是相同的。如果没有,请调整依赖项。

问题可能是您混合了不兼容的版本。您可以通过运行以下命令来检查您的版本:

mvn dependency:tree
(如果您有Eclipse,您可以打开pom文件并切换到“Dependency Hierarchy”(依赖层次结构)选项卡进行相同的操作。)

特别检查以下各项:

  • org.thymeleaf:thymeleaf-spring4:jar
  • org.thymeleaf:thymeleaf:jar

这两个版本应该是相同的。如果没有,请调整依赖项。

您将得到
java.lang.NoClassDefFoundError
错误。请运行maven update并重试您有哪些Thymeleaf依赖项/jar文件?我使用maven依赖项进行配置。我将把它们作为编辑放在question@holmis83在中添加了依赖项question@AjitSoman已经做了几次了,你已经得到了
java.lang.NoClassDefFoundError
错误。请运行maven update并重试您有哪些Thymeleaf依赖项/jar文件?我使用maven依赖项进行配置。我将把它们作为编辑放在question@holmis83在中添加了依赖项question@AjitSoman已经做了好几次了