春のMVCでのHTML文字列にModalAndView

Dhanu K:

私はいくつかのHTMLコンテンツでメールを送信する必要があり、

しかし、HTMLコンテンツは、JSPファイル内にあります

私は、私はこのようなのaddObjectでModalAndViewを使用しています、このため、コンテンツのためのJSPファイルに設定されたデータに必要

ModelAndView view = new ModelAndView(PageConstants.wwEmail);
view.addObject("title",title);
view.addObject("content",html);
prepareEmailMessage(message, to, title, view.toString());

私は、正しい方法でそれをやっていますか?またはそれを行うための他の方法はありますか?

どのように人口データとI GET HTMLできますか?

Dhanu K:

@ user404我々は@Syed Shahulの服用により、このために速度を使用することができますコメントで述べたように答えを参考に、私はテンプレートのコントローラを使用しての私の目標を達成している、これは私の最終的な解決策であります

これらのMavenの依存関係を使用します

<dependency>
    <groupId>org.apache.velocity</groupId>
    <artifactId>velocity</artifactId>
    <version>1.7</version>
</dependency>
<dependency>
    <groupId>org.apache.velocity</groupId>
    <artifactId>velocity-tools</artifactId>
    <version>2.0</version>
</dependency>

以下のために私たちは、この中のテンプレート(.vm)ファイルを作成する必要があります src/main/resources

その後、作成 VelocityEngine

VelocityEngine velocityEngine = new VelocityEngine();
velocityEngine.setProperty("resource.loader", "class");
velocityEngine.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
velocityEngine.init();

今、私たちはこれでテンプレートを生成する必要があります velocityEngine

String page = "./ww_email.vm";
Template template = velocityEngine.getTemplate(page);

私たちが使用するテンプレートにデータを渡すには VelocityContext

VelocityContext velocityContext = new VelocityContext();
velocityContext.put("title", title);
velocityContext.put("content", html);

そして、テンプレートからHTML文字列を取得するためにStringWriterを作成

StringWriter stringWriter = new StringWriter();

そして、これをマージしstringWritervelocityContext

template.merge(velocityContext, stringWriter);

最後に、あなたはこのようなHTML文字列を取得することができます

String htmlCodeString = stringWriter.toString()

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=332625&siteId=1