6

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>
4
  • You need to add your servlets to the webapps folder of your tomcat which will discover your servlets at runtime. Or do you want to have an embedded tomcat launch by your java app? Commented Jul 18, 2017 at 14:44
  • Traditional Spring WEB MVC should run in servlet container, that could be Tomcat or Pivotal tc server. You don't need to create application context in this case, bootstrapping is done by container itself. From your IDE right click on project, select Run as and select either Tomcat or Pivotal tc. Commented Jul 18, 2017 at 14:46
  • 1
    Look here if you want one fat jar with embedded tomcat Commented Jul 18, 2017 at 14:48
  • Thank you both, I am in the process of reconfiguring my Intellij project to launch with Tomcat. Will report back. Commented Jul 18, 2017 at 14:50

2 Answers 2

6

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();
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

The hard coded path "src/main" maces me suspicious. This will not work when packed into a JAR file but only using the files lying around in the project folder, won't it?
0

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.

3 Comments

web.xml is not required for Java Config Servlet 3+ configuration.
To fg78nc's point I did make sure I was using Servlet 3+ to avoid XML configuration. I do agree, I will read over the documentation a bit more. Thank you Akah for the reply.
Yup, sorry if my answer was not helpfull.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.