Spring JavaConfig and Tomcat 8

I have a Spring 4 web application (webapp-module.war) running and working locally in eclipse using Java 8, tomcat 8 and JavaConfig (no web.xml):

enter image description here

But when I deploy tomcat 8 (the same version that I use locally in eclipse) on a remote Ubuntu server, I get:

enter image description here

I checked the host and port correctly. There is no error in the log (/var/lib/tomcat8/logs/catalina.out)

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# 

The access log contains:

 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 

Where xx.xxx.xxx.xx is the IP address of my local machine, from where I am trying to access the web application in my browser.

I looked: Spring Java configuration: deploying Tomcat without web.xml , but it really doesn't provide a solution.

Details of my project below:

Sources

enter image description here

Config.java

 @Configuration // Marks this class as configuration // Specifies which package to scan @ComponentScan("com.samples") // Enables Spring 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; } } 

WebInitializer.java

 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); } } 

HelloController.java

 @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"; } } 
+8
java spring eclipse spring-mvc tomcat
source share
3 answers

I'm sorry I was too hasty

It seems that the spring context is not loaded at all.

I think the problem is in this piece of 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); } } 

You used ctx.register(Config.class);

In any case, I always used this initialization:

 public class AppInitializer implements WebApplicationInitializer { @Override public void onStartup(ServletContext servletContext) throws ServletException { // Create the 'root' Spring application context AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext(); rootContext.scan("com.spring"); rootContext.setConfigLocations(new String[]{"com.spring.config.WebAppContextConfig", "com.spring.config.AppConfig"}); // Manages the lifecycle of the root application context servletContext.addListener(new ContextLoaderListener(rootContext)); // Declare dispatcher servlet. Handles requests into the application ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher", new DispatcherServlet(rootContext)); dispatcher.setLoadOnStartup(1); dispatcher.addMapping("/"); } } 

As you can see, I used rootContext.setConfigLocations to indicate where to find spring configuration classes

In any case, here you can find a working sample that I have successfully deployed to tomcat 8.0.39 and 8.5.4

Hope this is helpful

Angelo

+4
source share

It turns out that I did not do anything wrong with the code. The Tomcat server is configured to use strict servlet matching.

 org.apache.catalina.STRICT_SERVLET_COMPLIANCE=true 

This parameter affects several other properties ( see here ). One of them is the resourceOnlyServlets attribute of any Context element. By setting this value to "jsp" in the context of application.xml, the problem is resolved.

A comma-separated list of servlet names (used in / WEB -INF / web.xml) that expect the resource to be present. Ensures that servlet-related welcome files that wait for the resource to be present (for example, the JSP servlet) are not used when the resource is missing. This prevents problems caused by finding out the matching of welcome files in section 10.10 of the Servlet 3.0 specification. If the system property org.apache.catalina.STRICT_SERVLET_COMPLIANCE is set to true, the default value for this attribute will be an empty string, otherwise the default value will be jsp.

0
source share

Add finalName as ROOT in pom.xml . This will create the ROOT.war file in your target folder.

  <build> <plugins> <!--All plugins are here --> </plugins> <finalName>ROOT</finalName> </build> 

After that, if you decompose the ROOT.war file in your tomcat. Then your url will be http: // localhost: 8080

Hope this solves your problem.

For further verification, go to http: // localhost: 8080 / manager / html it will ask for a username and password. Make sure installed or not.

If this is not set, check out this guide: Unable to access Tomcat 8 Manager App , as suggested by E-Riz

As a complete example, go through this tutorial: http://websystique.com/springmvc/spring-4-mvc-helloworld-tutorial-annotation-javaconfig-full-example/

-one
source share

All Articles