第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會有你想問的

Spring MVC 中的 ModalAndView 到 html 字符串

Spring MVC 中的 ModalAndView 到 html 字符串

Qyouu 2023-09-20 17:07:02
我需要發(fā)送一封包含一些 HTML 內(nèi)容的郵件,但HTML內(nèi)容在JSP文件中我需要在 JSP 文件中設(shè)置內(nèi)容數(shù)據(jù),為此,我將 ModalAndView 與 addObject 一起使用,如下所示ModelAndView view = new ModelAndView(PageConstants.wwEmail);view.addObject("title",title);view.addObject("content",html);prepareEmailMessage(message, to, title, view.toString());我以正確的方式做嗎?或者還有其他方法可以做到嗎?如何獲取包含填充數(shù)據(jù)的 HTML?
查看完整描述

1 回答

?
天涯盡頭無女友

TA貢獻(xiàn)1831條經(jīng)驗(yàn) 獲得超9個(gè)贊

我們可以使用速度來實(shí)現(xiàn)這一點(diǎn),我已經(jīng)實(shí)現(xiàn)了使用模板控制器的目標(biāo),這是我的最終解決方案


使用這些 Maven 依賴項(xiàng)


<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>

因?yàn)槲覀冃枰獮榇藙?chuàng)建一個(gè)模板(.vm)文件src/main/resources


然后創(chuàng)建一個(gè)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();

現(xiàn)在我們需要用這個(gè)生成一個(gè)模板velocityEngine


String page = "./ww_email.vm";

Template template = velocityEngine.getTemplate(page);

要將數(shù)據(jù)傳遞給模板,我們應(yīng)該使用VelocityContext


VelocityContext velocityContext = new VelocityContext();

velocityContext.put("title", title);

velocityContext.put("content", html);

然后創(chuàng)建一個(gè)StringWriter從模板中獲取HTML字符串


StringWriter stringWriter = new StringWriter();

然后將其stringWriter與velocityContext


template.merge(velocityContext, stringWriter);

最后,你可以得到像這樣的HTML字符串


String htmlCodeString = stringWriter.toString()


查看完整回答
反對 回復(fù) 2023-09-20
  • 1 回答
  • 0 關(guān)注
  • 102 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號