Wicket:如何以编程方式呈现页面并将结果作为字符串获取?

2022-01-18 00:00:00 internationalization java wicket

我正在将应用程序转换为在其所有页面上使用 i18n/l10n.我对 Wicket 对此的支持感到非常高兴,而且到目前为止进展顺利.我遇到的一个棘手部分如下:

I'm in the process of converting an app to use i18n/l10n on all its pages. I'm very happy with Wicket's support for this, and it's going well so far. The one tricky part I've run into is the following:

我们有一个文本文件,用作 HTML 模板,当用户在网站上执行特定操作时发送电子邮件.当用户单击特定链接时,我手动阅读此模板,进行一些文本替换,例如 "Dear $USERNAME",然后将结果作为 HTML 电子邮件发送给用户.

We have a text file that is used as an HTML template to send email when users perform a certain operation on the site. When the user clicks a particular link, I read in this template manually, do some text substitutions like "Dear $USERNAME", and send the result as an HTML email to the user.

为了支持我们所针对的 10 种左右的语言,我要么必须维护这个模板文件的 10 个副本,要么想办法使用 Wicket 的内置 i18n 支持来呈现这个页面",将结果作为字符串抓取,然后发送.

In order to support the 10 or so languages we're targeting, I'll either have to maintain 10 copies of this template file, or figure out a way to render this "page" using Wicket's built-in i18n support, grab the result as a string, and then send it.

因此我的问题:如何以编程方式呈现"Wicket 页面并将结果作为字符串获取?

Hence my question: how can I "render" a Wicket page programmatically and get the result as a string?

如果可能的话,我宁愿避免像使用 HttpClient 这样的黑客攻击;HttpClient 不会有用户的 Locale,不会以用户身份自动登录等,所以这对我来说似乎不是一个好的解决方案.

I'd prefer to avoid hacks like using HttpClient if at all possible; HttpClient won't have the user's Locale, won't be automatically logged in as the user, etc., so that doesn't seem like a good solution to me.

推荐答案

关于这个的两篇文章:

将 Wicket 页面呈现为 HTML 电子邮件的字符串

将面板渲染为字符串

目前唯一的其他方法是使用 WicketTester 为此,但我不记得如何做到这一点的详细信息.

Currently the only other approach was using WicketTester for that, but I do not remember details how to do that.

相关文章