0
votes

I'm creating a webapp project with Spring MVC and Maven. I'm also new using Spring annotation driven config.

I want to create a parent project having all the SpringMVC configuration and the ServletContext listener for my container. Then, all my web projects will have a dependency to this project in order to be initialized with Spring MVC.

I also have ZK framework in my base project, but I think that's fine.

So I have the following classes:

Base Project:

public class DefaultInitializer implements WebApplicationInitializer {

    private Class<?> config = BaseConfig.class;

    public void onStartup(ServletContext container) throws ServletException {
    init();

        // Create the 'root' Spring application context
        AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
        rootContext.register(config);

        // Manage the lifecycle of the root application context
        container.addListener(new ContextLoaderListener(rootContext));

        // Create the dispatcher servlet's Spring application context
        AnnotationConfigWebApplicationContext dispatcherServlet = new AnnotationConfigWebApplicationContext();
        dispatcherServlet.register(MvcConfig.class);

        // Register and map the dispatcher servlets
        ServletRegistration.Dynamic dispatcher = container.addServlet(
            "dispatcher", new DispatcherServlet(dispatcherServlet));
        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping("/");

        ServletRegistration.Dynamic zkDispatcher = container.addServlet(
            "zkLoader", new DHtmlLayoutServlet());
        zkDispatcher.setLoadOnStartup(1);
        zkDispatcher.setInitParameter("update-uri", "/zkau");
        zkDispatcher.addMapping("*.zul");

        ServletRegistration.Dynamic zkUpdater = container.addServlet(
            "auEngine", new DHtmlUpdateServlet());
        zkUpdater.addMapping("/zkau/*");
    }

    protected void setConfig(Class<?> config){
        this.config = config;
    }

    protected void init(){
        setConfig(BaseConfig.class);
    }

}

Webapp Project:

public class Initializer extends DefaultInitializer{

    protected void init(){
        setConfig(WebConfig.class);
    }

}

These are the web.xml and mvc-config in my base project

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns="http://java.sun.com/xml/ns/javaee"
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
     id="WebApp_ID" version="2.5">

<display-name>base-config</display-name>

</web-app>


<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- Uncomment and your base-package here:
         <context:component-scan
            base-package="org.springframework.samples.web"/>  -->

    <mvc:annotation-driven />

</beans>

And these is the web.xml in my webapp project

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns="http://java.sun.com/xml/ns/javaee"
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
     id="WebApp_ID" version="2.5">

    <display-name>web-project</display-name>

</web-app>

I checked that if I run my base project in tomcat it works fine, but when I try to run the web app project (with a pom dependency to my base project) it doesn't calls the onStartup method.

What am I doing wrong?

Thank you.

EDIT:

I also have this output when running my server

No Spring WebApplicationInitializer types detected on classpath

1

1 Answers

0
votes

Well, finally it works.

I'm not sure what it was, but I did the following steps:

  • Change the Base Project pom packaging to 'jar' and import this as a dependency
  • Clean the tomcat server

In my research I learned some stuff so if this is happening to you, check this:

  • A parent pom can't be packaging jar, so it can't have source code
  • Check if you have more WebApplicationInitializer classes in your classpath with different versions. +info