How do I embed Tomcat in a Spring Framework MVC ap

2020-06-27 02:00发布

问题:

I have created the required configurations/controller classes. But it's not clear to me how I should orchestrate these classes to use run a tomcat instance. I know with spring boot it's a matter of using SpringApplication.run(..). But I'm trying to explore the alternate method used prior to Spring Boot. I'm a bit new to the Spring Framework so forgive my ignorance. I'm also not using any XML configuration only using Java

public class SpittrWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer
{

@Override //....
protected String[] getServletMappings(){
    return new String[] { "/" }; 
}

@Override //...
protected Class<?>[] getRootConfigClasses(){
    return new Class<?>[] { RootConfig.class };
}

@Override //.....
protected Class<?>[] getServletConfigClasses(){
    return new Class<?>[] { WebConfig.class };
}
}

I have created a controller

@Controller
@RequestMapping("/")
public class HomeController {

    @RequestMapping(method = RequestMethod.GET)
    public String home(){
        return "home";
    }

POM File:

    <dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.3.9.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>4.3.9.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>4.3.9.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>4.3.9.RELEASE</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
        <scope>provided</scope>
    </dependency>
</dependencies>

回答1:

Finally fixed the problem I was coming across. I added an embedded instance of Tomcat to my POM just like VitalyZ recommended. I configured the embedded tomcat instance in a new class.

Added the following to my Pom file

    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
        <version>8.5.15</version>
    </dependency>

Created a new class named Application.java

public class Application {
    public static void main(String[] args) throws Exception {

        String webAppDirLocation = "src/main/";
        Tomcat tomcat = new Tomcat();

        //Set Port #
        tomcat.setPort(8080);

        StandardContext ctx = (StandardContext) tomcat.addWebapp("/", new File(webAppDirLocation).getAbsolutePath());

        tomcat.start();
        tomcat.getServer().await();
    }
}


回答2:

I would suggest you to look at the spring official documentation to understand the mecanism, and more precisely the DispatcherServlet part.

To me, you are missing the web.xml part :

<web-app>
    <servlet>
        <servlet-name>example</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>example</servlet-name>
        <url-pattern>/example/*</url-pattern>
    </servlet-mapping>

</web-app>

This stackoverflow answer can help you too.