[Wicket-user] ServletContext available on SpringApplicationController
Hi, Attached is a fixed version of the wicket-contrib-spring's SpringApplicationController. Because WicketSpringServlet's init(ServletConfig) is never run, ServletContext is not available when using SpringApplicationController. This causes NullPointerException at least in UrlResourceStream. The fix is not too beautiful, it would make more sense to run init(ServletConfig), but I did not find any way to get the ServletConfig from the Controller. -- Janne Hietamäki Cemron Ltd http://www.cemron.com/ /* * $Id: SpringApplicationController.java,v 1.1 2005/03/25 15:02:51 eelco12 Exp $ * $Revision: 1.1 $ * $Date: 2005/03/25 15:02:51 $ * * * Licensed under the Apache License, Version 2.0 (the License); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package wicket.contrib.spring; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.AbstractController; import wicket.protocol.http.WebApplication; import wicket.protocol.http.WicketServlet; /** * I'm not sure this is the best approach to integrate Spring and Wicket. * But beside that you now may use all of Spring's ApplicationContext capabilities, * you may simply add Wicket functionality to existing Spring applications. * * The apprach taken is to create a Spring controller derived from * AbstractController and forward handleRequestInternal() to Wicket's * Servlet object and it's doGet() method. * * The Wicket application to use must be configured with Spring's web application * context (servlet-name-servlet.xml). * * @author Juergen Donnerstag */ public class SpringApplicationController extends AbstractController { /** Logging */ private static Log log = LogFactory.getLog(SpringApplicationController.class); /** The Wicket application object */ private SpringApplication application; /** * JavaBean method to provide Wicket's application object. Will be set * through Spring BeanFactory and WebApplicationContext. * servlet-name-servlet.xml * * @param application Wicket application object */ public void setApplication(final SpringApplication application) { this.application = application; } /** * Initialize the WicketSpringServlet and pass the ServletContext to it. */ boolean inited=false; public void init(){ if(!inited){ WicketSpringServlet servlet=new WicketSpringServlet(application); servlet.init(getWebApplicationContext().getServletContext()); this.application.setWicketServlet(servlet); inited=true; } } /** * Handle the request. Simply forward it to Wicket. * * @see org.springframework.web.servlet.mvc.AbstractController#handleRequestInternal(javax.servlet.http.HttpServletRequest, * javax.servlet.http.HttpServletResponse) */ protected ModelAndView handleRequestInternal(HttpServletRequest servletRequest, HttpServletResponse servletResponse) throws Exception { init(); if (application != null) { application.getWicketServlet().doGet(servletRequest, servletResponse); } else { log.error(Wickets application object is not available. Probably the bean named 'wicketApplication' was not found in Spring's web application context: servlet-name-servlet.xml); } return null; } /** * Spring servlet */ public final class WicketSpringServlet extends WicketServlet { protected ServletContext context; /** * Constructor * @param application The application */ public WicketSpringServlet(final WebApplication application) { this.webApplication = application; } /** * @param context Servlet context */ public void init(ServletContext context){ this.context=context; } /** * @see javax.servlet.GenericServlet#getServletContext() */ public ServletContext getServletContext(){ return context; } /** * @see
RE: [Wicket-user] ServletContext available on SpringApplicationController
There is a cleaner way to integrate with spring w/out the extra servlet. All you need to do is provide an implementation of IWebApplicationFactory that creates the application object from spring's context and carry in all your dependencies through that... public class SpringApplicationFactory implements IWebApplicationFactory { public WebApplication createApplication(WicketServlet servlet) { ServletContext sc=servlet.getServletContext(); ApplicationContext ac=WebApplicationContextUtils.getRequiredWebApplicationContext(sc); return (WebApplication) ac.getBean(wicketApplication); } } In web.xml provide applicationfactoryclassname param instead of applicationclassname param. -Igor -Original Message- From: [EMAIL PROTECTED] [mailto:wicket-user- [EMAIL PROTECTED] On Behalf Of Janne Hietamäki Sent: Tuesday, September 27, 2005 12:55 PM To: wicket-user@lists.sourceforge.net Subject: [Wicket-user] ServletContext available on SpringApplicationController Hi, Attached is a fixed version of the wicket-contrib-spring's SpringApplicationController. Because WicketSpringServlet's init(ServletConfig) is never run, ServletContext is not available when using SpringApplicationController. This causes NullPointerException at least in UrlResourceStream. The fix is not too beautiful, it would make more sense to run init(ServletConfig), but I did not find any way to get the ServletConfig from the Controller. -- Janne Hietamäki Cemron Ltd http://www.cemron.com/ --- This SF.Net email is sponsored by: Power Architecture Resource Center: Free content, downloads, discussions, and more. http://solutions.newsforge.com/ibmarch.tmpl ___ Wicket-user mailing list Wicket-user@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/wicket-user
Re: [Wicket-user] ServletContext available on SpringApplicationController
Igor Vaynberg wrote: There is a cleaner way to integrate with spring w/out the extra servlet. All you need to do is provide an implementation of IWebApplicationFactory that creates the application object from spring's context and carry in all your dependencies through that... Yes, I know, but I don't think this would work in my case because I need to have Spring's OpenSessionInViewInterceptor, and so I have to use Spring's DispatcherServlet. -- Janne Hietamäki Cemron Ltd http://www.cemron.com/ --- This SF.Net email is sponsored by: Power Architecture Resource Center: Free content, downloads, discussions, and more. http://solutions.newsforge.com/ibmarch.tmpl ___ Wicket-user mailing list Wicket-user@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/wicket-user
RE: [Wicket-user] ServletContext available on SpringApplicationController
Why not use opensessioninviewfilter instead? -Igor -Original Message- From: [EMAIL PROTECTED] [mailto:wicket-user- [EMAIL PROTECTED] On Behalf Of Janne Hietamäki Sent: Tuesday, September 27, 2005 2:49 PM To: wicket-user@lists.sourceforge.net Subject: Re: [Wicket-user] ServletContext available on SpringApplicationController Igor Vaynberg wrote: There is a cleaner way to integrate with spring w/out the extra servlet. All you need to do is provide an implementation of IWebApplicationFactory that creates the application object from spring's context and carry in all your dependencies through that... Yes, I know, but I don't think this would work in my case because I need to have Spring's OpenSessionInViewInterceptor, and so I have to use Spring's DispatcherServlet. -- Janne Hietamäki Cemron Ltd http://www.cemron.com/ --- This SF.Net email is sponsored by: Power Architecture Resource Center: Free content, downloads, discussions, and more. http://solutions.newsforge.com/ibmarch.tmpl ___ Wicket-user mailing list Wicket-user@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/wicket-user --- This SF.Net email is sponsored by: Power Architecture Resource Center: Free content, downloads, discussions, and more. http://solutions.newsforge.com/ibmarch.tmpl ___ Wicket-user mailing list Wicket-user@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/wicket-user
Re: [Wicket-user] ServletContext available on SpringApplicationController
Igor Vaynberg wrote: Why not use opensessioninviewfilter instead? -Igor Because Filter does not have all the features, for example I can't tune session flushing like in the interceptor. I could override the filter to do everything I want.. Anyway, thanks. -- Janne Hietamäki Cemron Ltd http://www.cemron.com/ --- This SF.Net email is sponsored by: Power Architecture Resource Center: Free content, downloads, discussions, and more. http://solutions.newsforge.com/ibmarch.tmpl ___ Wicket-user mailing list Wicket-user@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/wicket-user