Wicket: how to render page programmatically and get result as string?
Asked Answered
G

3

9

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.

Gift answered 15/8, 2011 at 17:38 Comment(1)
This is a very good question, I'm already looking for an answer to it.Dipsomania
S
6

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.

Settling answered 15/8, 2011 at 18:23 Comment(2)
Thanks, I used a slightly modified version of your first link: gist.github.com/1152059Gift
Only sharing links to other pages has the disadvantage that the target page maybe gets removed in the future ..Diaconicum
D
5

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() );
Dipsomania answered 15/8, 2011 at 18:40 Comment(4)
I can't get the above code to compile: WebResponse is abstract, ServletWebRequest missing constructor argument, WebRequestCycle not found, RequestCycle has no request method. What Wicket version is this?Gaskell
@MartinWickman 1.4, as it was the latest release in last August.Dipsomania
Ah, that explains it. 1.5 changed a lot apparently, so the code doesn't work there. Maybe update the answer to reflect that.Gaskell
Sure, I'll give it a try but I can't go around updating all my answers as new versions of Wicket/Java/etc are released :)Dipsomania
P
5

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

Parrish answered 15/10, 2013 at 9:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.