title: Spring JavaConfig and Tomcat 8
body: <p>I have a Spring 4 web application (webapp-module.war) working and running locally in eclipse using Java 8, tomcat 8 and JavaConfig (no web.xml):</p>
  
  <p><a href="https://i.stack.imgur.com/ifhd9.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ifhd9.png" alt="enter image description here"></a></p>
  
  <p>But when I deploy to tomcat 8 (same version I am using locally in eclipse) on a remote Ubuntu server I get:</p>
  
  <p><a href="https://i.stack.imgur.com/38AAR.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/38AAR.png" alt="enter image description here"></a></p>
  
  <p>I verified host and port which are correct. There is no error in the log (/var/lib/tomcat8/logs/catalina.out)</p>
  
  <pre><code>Jun 21, 2016 10:32:44 PM org.apache.catalina.startup.HostConfig undeploy
  INFO: Undeploying context [/webapp-module]
  Jun 21, 2016 10:32:44 PM org.apache.catalina.startup.HostConfig deployWAR
  INFO: Deploying web application archive /var/lib/tomcat8/webapps/webapp-module.war
  Jun 21, 2016 10:32:46 PM org.apache.jasper.servlet.TldScanner scanJars
  INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
  Jun 21, 2016 10:32:46 PM org.apache.catalina.startup.HostConfig deployWAR
  INFO: Deployment of web application archive /var/lib/tomcat8/webapps/webapp-module.war has finished in 1,870 ms
  root@vmi63860:/var/lib/tomcat8/logs# 
  </code></pre>
  
  <p>The access log contains:</p>
  
  <pre><code>root@vmi63860:/var/log/tomcat8# cat localhost_access_log.2016-06-22.txt 
  xx.xxx.xxx.xx - - [22/Jun/2016:22:36:00 +0200] "GET /webapp-module/ HTTP/1.1" 404 1040
  xx.xxx.xxx.xx - - [22/Jun/2016:22:36:00 +0200] "GET /favicon.ico HTTP/1.1" 404 1034
  xx.xxx.xxx.xx - - [22/Jun/2016:22:36:50 +0200] "GET /webapp-module/hello HTTP/1.1" 404 1050
  </code></pre>
  
  <p>Where xx.xxx.xxx.xx is the IP of my local machine from where I try to access the web app in my browser.</p>
  
  <p>I took a look at:
  <a href="https://stackoverflow.com/questions/21529577/spring-java-config-tomcat-deploy-without-web-xml">Spring Java Config: Tomcat deploy without web.xml</a> but it does not really provide a solution.</p>
  
  <p>Details on my project below:</p>
  
  <p><strong>Sources</strong></p>
  
  <p><a href="https://i.stack.imgur.com/D6K66.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/D6K66.png" alt="enter image description here"></a></p>
  
  <p><strong>Config.java</strong></p>
  
  <pre><code>@Configuration // Marks this class as configuration
  // Specifies which package to scan
  @ComponentScan("com.samples")
  // Enables Spring's annotations
  @EnableWebMvc
  public class Config {
  
    @Bean
    public UrlBasedViewResolver setupViewResolver() {
      UrlBasedViewResolver resolver = new UrlBasedViewResolver();
      resolver.setPrefix("/WEB-INF/jsp/");
      resolver.setSuffix(".jsp");
      resolver.setViewClass(JstlView.class);
      return resolver;
    }
  
  }
  </code></pre>
  
  <p><strong>WebInitializer.java</strong></p>
  
  <pre><code>public class WebInitializer implements WebApplicationInitializer {
  
    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
  
      AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
      ctx.register(Config.class);
      ctx.setServletContext(servletContext);
  
      Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx));
      servlet.addMapping("/");
      servlet.setLoadOnStartup(1);
  
    }
  
  }
  </code></pre>
  
  <p><strong>HelloController.java</strong></p>
  
  <pre><code>@Controller
  public class HelloController {
  
    @RequestMapping("/")
    public String home() {
      return "index";
    }
  
    @RequestMapping("/hello")
    public String showhello(ModelMap model) {
      model.addAttribute("message", "Hello Spring MVC Framework!");
      return "hello";
    }
  
  }
  </code></pre>