use WicketTester.startPage() and tester.getLastResponseAsString()

On Thu, Sep 29, 2011 at 11:06 AM, Stefan Neumann
<[email protected]> wrote:
> Hey guys,
>
>  I need to render a page to a String or file. I have already read the
> typical reference [1] [2] for this topic.
>
> The difference is, that I am using wicket 1.5 and there must be some
> API changes so I could not use the given examples.
>
> Has someone an example? I tried my best to make the exampel compile
> but i alwas get a NPE at
> bufferedWebResponse.getText().toString()
>
> Appreciate your help in advance,
>
> Stefan
>
>
> [1]
> http://www.danwalmsley.com/2008/10/21/render-a-wicket-page-to-a-string-for-html-email/
> [2]
> https://gist.github.com/1152059
> [3]
>  public static String renderPage(final Class<? extends Page>
> pageClass, final PageParameters pageParameters)
>        {
>            final WebApplication application = WebApplication.get();
>            final ServletContext context =
> application.getServletContext();  //fake a request/response cycle
>            final MockHttpSession servletSession = new 
> MockHttpSession(context);
>            servletSession.setTemporary(true);
>            final MockHttpServletRequest servletRequest = new
> MockHttpServletRequest(application, servletSession, context);
>            final MockHttpServletResponse servletResponse = new
> MockHttpServletResponse(servletRequest);  //initialize request and
> response
>            servletRequest.initialize();
>            servletResponse.initialize();
>
>            final ServletWebRequest webRequest = new
> ServletWebRequest(servletRequest,"/");
>            final WebResponse webResponse = new
> ServletWebResponse(webRequest,servletResponse);
>            final BufferedWebResponse bufferedWebResponse = new
> BufferedWebResponse(webResponse);
>            //webResponse.setAjax(true);
>            final RequestCycle requestCycle = new RequestCycle(
>                new RequestCycleContext(webRequest, webResponse,
> application.getRootRequestMapper(,
> application.getExceptionMapperProvider().get()));
>
>            //requestCycle.setRequestTarget(new
> BookmarkablePageRequestTarget(pageClass, pageParameters));
>            requestCycle.setResponsePage(pageClass, pageParameters);
>
>            try
>            {
>                final boolean handled = requestCycle.processRequestAndDetach();
>
>
>                LOGGER.info("Response after request: " +
> bufferedWebResponse.getText().toString());
>
>                if (! handled)
>                {
>                    LOGGER.error(">>>>>>>>>>>>>>>>>>>>>>> NOT HANDLED");
>                    //requestCycle.set(new
> ErrorCodeRequestHandler(HttpServletResponse.SC_NOT_FOUND));
>                }
>
>                //requestCycle.detach();
>            }
>            finally
>            {
>                requestCycle.getResponse().close();
>            }
>
>            return bufferedWebResponse.getText().toString();
>        }
>
> --
> Stefan Neumann
> Dipl.-Ing.
> freiheit.com technologies gmbh
> Straßenbahnring 22 / 20251 Hamburg, Germany
> fon +49 (0)40 / 890584-0
> fax +49 (0)40 / 890584-20
> HRB Hamburg 70814
> 1CB2 BA3C 168F 0C2B 6005 FC5E 3EBA BCE2 1BF0 21D3
> Geschäftsführer: Claudia Dietze, Stefan Richter, Jörg Kirchhof
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [email protected]
> For additional commands, e-mail: [email protected]
>
>



-- 
Martin Grigorov
jWeekend
Training, Consulting, Development
http://jWeekend.com

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to