Spring Download, Java Configuration - No mapping for HTTP request with URI [/ ...] in DispatcherServlet named 'dispatcherServlet'

This is a fairly common problem in stackOverflow, but none of the topics in the same problem solve the problem.

We have a template configuration that uses xml config, but now we are trying to get away from this and start using the Java configuration.

So, I have a new project using Java configuration and Spring Boot. We also use JSP and Tiles 3.

The problem is that it does not display our admin login page.

Here is the code:

Main configuration class:

@SpringBootApplication @EnableScheduling @Import(OnAdminBeans.class) public class AppConfig extends SpringBootServletInitializer { public static void main(String[] args) { SpringApplication.run(AppConfig.class, args); } @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(AppConfig.class); } } 

AppConfig.class is the main package. Via @ComponentScan , which @SpringBootApplication brings, it looks at other configurations that are on mainpackage.config , so it imports the view configuration class:

 @Configuration @EnableWebMvc public class ViewConfig extends WebMvcConfigurerAdapter { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/adm/static/**").addResourceLocations("/adm/static/"); } // @Override // public void addViewControllers(ViewControllerRegistry registry) { // registry.addViewController("/adm/login").setViewName("login-template-tiles"); // } @Override public void configureViewResolvers(ViewResolverRegistry registry) { registry.viewResolver(viewResolver()); registry.viewResolver(jspViewResolver()); registry.viewResolver(tilesViewResolver()); } @Bean public LocaleResolver localeResolver() { CookieLocaleResolver localeResolver = new CookieLocaleResolver(); localeResolver.setCookieName("locale"); localeResolver.setCookieMaxAge(30); localeResolver.setDefaultLocale(new Locale("pt", "BR")); return localeResolver; } @Bean public MultipleViewResolver viewResolver() { Map<String, ViewResolver> viewsResolvers = new HashMap<String, ViewResolver>(); viewsResolvers.put(MultipleViewResolver.ViewType.JSP.getKey(), jspViewResolver()); viewsResolvers.put(MultipleViewResolver.ViewType.TILES.getKey(), tilesViewResolver()); MultipleViewResolver viewResolver = new MultipleViewResolver(); viewResolver.setViewsResolvers(viewsResolvers); viewResolver.setOrder(1); return viewResolver; } @Bean public InternalResourceViewResolver jspViewResolver() { InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); viewResolver.setPrefix("/WEB-INF/jsp/"); viewResolver.setSuffix(".jsp"); viewResolver.setViewClass(JstlView.class); viewResolver.setOrder(2); return viewResolver; } @Bean public UrlBasedViewResolver tilesViewResolver() { UrlBasedViewResolver viewResolver = new UrlBasedViewResolver(); viewResolver.setViewClass(TilesView.class); viewResolver.setOrder(3); return viewResolver; } @Bean public TilesConfigurer tilesConfigurer() { TilesConfigurer configurer = new TilesConfigurer(); configurer.setDefinitions("/WEB-INF/tile-defs/tiles-definitions.xml"); return configurer; } } 

LoginController.class defined as:

 @Controller @RequestMapping(value = "/adm") public class LoginController { @RequestMapping(value = "/login") public ModelAndView login() { return new ModelAndView("login-template-tiles"); } } 

And in tiles-definitions.xml , I have the following definition for login-templates:

 <definition name="login-template-tiles" template="/WEB-INF/jsp/adm/templates/login-template.jsp"> <put-attribute name="admin-title" value="Admin" /> <put-attribute name="content" value="/WEB-INF/jsp/adm/templates/sections/login/index.jsp" /> </definition> 

Please note that both files exist.

Given all that LoginController.login () gets called when I try to access / adm / login. But it cannot find the correct jsp file, apparently.

He returns 404 . When TRACE is enabled, I get the following log:

 DispatcherServlet with name 'dispatcherServlet' processing GET request for [/WEB-INF/jsp/adm/templates/login-template.jsp] Testing handler map [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping@2118c09a] in DispatcherServlet with name 'dispatcherServlet' Looking up handler method for path /WEB-INF/jsp/adm/templates/login-template.jsp Did not find handler method for [/WEB-INF/jsp/adm/templates/login-template.jsp] Testing handler map [org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping@2c148974] in DispatcherServlet with name 'dispatcherServlet' No handler mapping found for [/WEB-INF/jsp/adm/templates/login-template.jsp] Testing handler map [org.springframework.web.servlet.handler.SimpleUrlHandlerMapping@784c3547] in DispatcherServlet with name 'dispatcherServlet' No handler mapping found for [/WEB-INF/jsp/adm/templates/login-template.jsp] Testing handler map [org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport$EmptyHandlerMapping@533e0604] in DispatcherServlet with name 'dispatcherServlet' Testing handler map [org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport$EmptyHandlerMapping@cfd1b4e] in DispatcherServlet with name 'dispatcherServlet' No mapping found for HTTP request with URI [/WEB-INF/jsp/adm/templates/login-template.jsp] in DispatcherServlet with name 'dispatcherServlet' 

Any suggestions are welcome!

EDIT: OK. While debugging, I found out that this has something to do with the built-in Tomcat. Other than that, I have no idea what is going on.

EDIT 2:

The problem was found to be in org.springframework.web.servlet.DispatcherServlet # getHandler . It simply does not find HandlerMapping for this request. Should I register it?

+8
java spring spring-boot spring-java-config spring-mvc
source share
2 answers

OK! Found a problem.

This link helped me: https://samerabdelkafi.wordpress.com/2014/08/03/spring-mvc-full-java-based-config/

More specifically, this configuration:

 @Override public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { configurer.enable(); } 

By setting the default handler, I would no longer get a white page, but instead the JSP code as html, which clearly tells me that the JSP was found but not displayed.

So, the answer was on this page: JSP file does not appear in Spring boot web application

I missed the tomcat-embed-jasper artifact.

+16
source

Add below depending on your pom.xml

  <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <scope>provided</scope> </dependency> 
0
source

All Articles