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

发布于 2024-11-29 19:50:51 字数 486 浏览 0 评论 0原文

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

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

为了支持我们目标的 10 种左右语言,我要么必须维护此模板文件的 10 个副本,要么找到一种使用 Wicket 的内置 i18n 支持来呈现此“页面”的方法,获取结果作为字符串,然后发送。

因此我的问题:如何以编程方式“渲染”Wicket 页面并获取字符串形式的结果?

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

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:

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.

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.

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

何必那么矫情 2024-12-06 19:50:51

两篇与此相关的文章:

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

将面板渲染为字符串

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

Two article regarding to this:

Render a Wicket page to a string for HTML email

Rendering Panel to a String

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

遗忘曾经 2024-12-06 19:50:51

如果您只想要原始代码,这里是:(这实际上与文章中描述的解决方案相同。)

//I assumed that you want to use the current user's session for rendering. If this isn't the case, you'll have to use a mock session
MockHttpServletRequest mockReq = new MockHttpServletRequest( WebApplication.get(), ((WebRequest)getRequest()).getHttpServletRequest().getSession(), WebApplication.get().getServletContext() ); 
MockHttpServletResponse mockRes = new MockHttpServletResponse( mockReq );
WebResponse res = new WebResponse(mockRes);
ServletWebRequest req = new ServletWebRequest( mockReq );
RequestCycle cycle = new WebRequestCycle( WebApplication.get(), req, res );
PageParameters pp = new PageParameters();
//add page parameters here
//Your email page should really be a bookmarkable page, but if it isn't, you can replace the request target with something that better suits your case
cycle.request( new BookmarkablePageRequestTarget( EmailPage.class, pp ));
System.out.println( mockRes.getDocument() );

If you just want the raw code, here it is: (This is practically the same as the solution described in the article.)

//I assumed that you want to use the current user's session for rendering. If this isn't the case, you'll have to use a mock session
MockHttpServletRequest mockReq = new MockHttpServletRequest( WebApplication.get(), ((WebRequest)getRequest()).getHttpServletRequest().getSession(), WebApplication.get().getServletContext() ); 
MockHttpServletResponse mockRes = new MockHttpServletResponse( mockReq );
WebResponse res = new WebResponse(mockRes);
ServletWebRequest req = new ServletWebRequest( mockReq );
RequestCycle cycle = new WebRequestCycle( WebApplication.get(), req, res );
PageParameters pp = new PageParameters();
//add page parameters here
//Your email page should really be a bookmarkable page, but if it isn't, you can replace the request target with something that better suits your case
cycle.request( new BookmarkablePageRequestTarget( EmailPage.class, pp ));
System.out.println( mockRes.getDocument() );
别理我 2024-12-06 19:50:51

对于较新的 Wicket 版本:6.7.0 已发布使用新的 ComponentRenderer 正是为了这个目的!

For newer Wicket versions: 6.7.0 came with a new ComponentRenderer precisely for this purpose!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文