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 wicket.protocol.http.WicketServlet#init()
         */
        public void init()
        {
            ; // replace super implementation with nothing. Apllication class
              // will be defined through Spring xml. 
        }
    }
}

Reply via email to