I guess I need to add an example of this in wicket-examples ... Please create a ticket to not forget it.
Here is how it should work: IRequestHandler handler = new RenderPageRequestHandler(Page.class, parameters); PageReneder renderer = application.getgetPageRendererProvider().get(handler); oldResponse = requestCycle.getResponse(); StringResponse newResponse = new StringResponse(); requestCycle.setResponse(newResponse); renderer.respond(requestCycle); requestCycle.setResponse(oldResponse); pageAsText = newResponse.toString(); On Thu, Sep 29, 2011 at 12:09 PM, Stefan Neumann <[email protected]> wrote: > Hey Martin, > > thanks for the quick response. It sounds quite good, but it is not working > yet. > > Since we use guice, I tried to use the WebApplication we have > configured already: > > final WicketTester wicketTester = new > WicketTester(WebApplication.get()); > wicketTester.startPage(pageClass,pageParameters); > return wicketTester.getLastResponseAsString(); > > But I get following exception[1]. > > Some more basic conditions. > * A request is currently running. > * I need to get session information into the rendering, since the data > loading for the page contains authorization checks on the currently > logged in user. > > Any suggestions? > > Stefan > > > > > Caused by: java.lang.reflect.InvocationTargetException > at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native > Method) > at > sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) > at > sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) > at java.lang.reflect.Constructor.newInstance(Constructor.java:513) > at > org.apache.wicket.session.DefaultPageFactory.newPage(DefaultPageFactory.java:177) > ... 37 more > Caused by: java.lang.IllegalStateException: Application name can only > be set once. > at org.apache.wicket.Application.setName(Application.java:846) > at > org.apache.wicket.util.tester.BaseWicketTester.<init>(BaseWicketTester.java:291) > at > org.apache.wicket.util.tester.BaseWicketTester.<init>(BaseWicketTester.java:241) > at > org.apache.wicket.util.tester.WicketTester.<init>(WicketTester.java:192) > at > com.freiheit.tango.presentation.application.WicketUtils.renderPageWithWicketTester(WicketUtils.java:43) > at > com.freiheit.tango.presentation.dashboard.DashboardPage.<init>(DashboardPage.java:124) > at > com.freiheit.tango.presentation.dashboard.DashboardPage.<init>(DashboardPage.java:108) > at > com.freiheit.tango.presentation.dashboard.DashboardPage.<init>(DashboardPage.java:104) > ... 42 more > > > 2011/9/29 Martin Grigorov <[email protected]>: >> 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] >> >> > > > > -- > 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]
