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

前端 未结 3 507
-上瘾入骨i
-上瘾入骨i 2020-12-10 05:52

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

相关标签:
3条回答
  • 2020-12-10 06:14

    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() );
    
    0 讨论(0)
  • 2020-12-10 06:16

    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.

    0 讨论(0)
  • 2020-12-10 06:18

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

    0 讨论(0)
提交回复
热议问题