このThymeleafチュートリアル「Thymeleafを使用したSpringのリッチHTMLメール」に従って、Thymeleafテンプレートを使用してメールを生成しました。すべて問題ありませんが、アプリケーションの他の場所で使用しているApplicationContextBeanにアクセスできません。
具体的には、メールテンプレートに次のようなものを入れたいと思います。
<span th:text="${@myBean.doSomething()}">
ここで、「myBean」は@Componentです。それは可能ですか?私は次のような回避策を探していません
<span th:text="${myBean.doSomething()}">
ここで、Beanはテンプレート処理コンテキストで変数として追加されます。
構成:
@Configuration
@EnableWebMvc
@EnableSpringDataWebSupport
public class MyWebConfig extends WebMvcConfigurerAdapter {
[....]
public ClassLoaderTemplateResolver emailTemplateResolver() {
ClassLoaderTemplateResolver resolver = new ClassLoaderTemplateResolver();
resolver.setPrefix("emailtemplates/");
resolver.setSuffix(".html");
resolver.setCharacterEncoding("UTF-8");
resolver.setTemplateMode("HTML5");
return resolver;
}
@Bean
public SpringTemplateEngine emailTemplateEngine() {
SpringTemplateEngine engine = new SpringTemplateEngine();
engine.addTemplateResolver(emailTemplateResolver());
engine.addDialect(new LayoutDialect()); // thymeleaf-layout-dialect
addSpringSecurityDialect(engine); // thymeleaf-SpringSecurity-dialect
return engine;
}
[....]
}
メールサービス:
@Service
public class MyEmailService {
@Resource SpringTemplateEngine emailTemplateEngine;
[....]
public boolean sendHtmlEmail(...) {
final Context ctx = new Context(locale);
ctx.setVariable("someVariable", "someValue"); // Don't want to add myBean here
final String body = this.emailTemplateEngine.process("myTemplate", ctx);
[....]
コンポーネント:
@Component
public class MyBean {
public String doSomething() {
return "Something done!";
}
}
テンプレート:
<span th:text="${@myBean.doSomething()}">
エラー:
EL1057E:(pos 1):Bean'myBean 'へのアクセスを解決するためのBeanリゾルバーがコンテキストに登録されていません
thymeleaf-2.1.4とspring-4.1.6を使用しています
編集:
リクエストを確実に使用できない@Asyncメソッドでほとんどのメールを送信するため、HttpServletRequestを使用できないことに注意してください。これが、WebContextではなくContextを使用する理由です(SpringWebContextについては知りませんでしたが、誰かが「beans」変数を介してBeanにアクセスするためにそのクラスを作成した場合、@ myBean表記を使用することは不可能であるに違いありません)。
ここには3つの異なるコンテキストがあるため、混乱します。
ApplicationContext
、SpringはではありませんWebApplicationContext
。org.thymeleaf.context.Context
変数を設定/追加するThymeleaf 。org.springframework.expression.EvaluationContext
SpEL式を評価するために使用されるSpEL評価コンテキスト。Thymeleafの場合はそれThymeleafEvaluationContext
です。Spring Webコンテキストがない場合、必要なのはThymeleafEvaluationContext
、アプリケーションコンテキストへの参照を変数としてThymeleafに追加することだけContext
です。
例(Kotlin)
@Service
class TemplateService(
private val templateEngine: TemplateEngine,
private val applicationContext: ApplicationContext
) {
fun processTemplate(locale: Locale, template: String, vararg variables: Pair<String, Any?>): String {
val context = Context(locale)
// Set the Thymeleaf evaluation context to allow access to Spring beans with @beanName in SpEL expressions
context.setVariable(ThymeleafEvaluationContext.THYMELEAF_EVALUATION_CONTEXT_CONTEXT_VARIABLE_NAME,
ThymeleafEvaluationContext(applicationContext, null))
// Set additional variables
variables.forEach { (key, value) -> context.setVariable(key, value) }
return templateEngine.process(template, context)
}
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加