Spring Boot,Java Config - 在 DispatcherServlet 中找不到名称为“dispatcherServlet”的带有 URI [/...] 的 HTTP 请求的映射
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30406186/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring Boot, Java Config - No mapping found for HTTP request with URI [/...] in DispatcherServlet with name 'dispatcherServlet'
提问by Glauber Néspoli
This has been a quite common problem here in stackOverflow, but none of the topics of the same problem solves mine.
这是 stackOverflow 中的一个非常普遍的问题,但同一问题的主题都没有解决我的问题。
We have a template configuration that uses xml config, but now we're trying to move away from that and start using Java config.
我们有一个使用 xml 配置的模板配置,但现在我们试图摆脱它并开始使用 Java 配置。
So I have a new project using Java config and Spring Boot. We're also using JSP and Tiles 3.
所以我有一个使用 Java 配置和 Spring Boot 的新项目。我们也在使用 JSP 和 Tiles 3。
Problem is: it fails to render our admin login page.
问题是:它无法呈现我们的管理员登录页面。
Here is the code:
这是代码:
Main config 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);
}
}
The AppConfig.class
is is the main package. Through the @ComponentScan
that @SpringBootApplication
brings, it scans the other configurations that are on mainpackage.config
, so it imports the view config class:
的AppConfig.class
IS是主要的包。通过@ComponentScan
that @SpringBootApplication
,它会扫描 on 的其他配置mainpackage.config
,因此它导入视图配置类:
@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;
}
}
The LoginController.class
is defined as:
的LoginController.class
定义为:
@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-template-tiles:
在tiles-definitions.xml
我对 login-template-tiles 有以下定义:
<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>
Note that both files exist.
请注意,这两个文件都存在。
Given all that the LoginController.login()does get calledwhen i try to access /adm/login. But it fails to find the proper jsp file, aparently.
鉴于我尝试访问/adm/login时会调用LoginController.login() 的所有内容。但它显然没有找到正确的 jsp 文件。
It returns a 404. With TRACE enabled, I get the following log:
它返回404。启用 TRACE 后,我得到以下日志:
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 appreciated!
任何建议表示赞赏!
EDIT:Ok. By debugging, I found out that it has something to do with the embedded Tomcat. Other than that, I have no clue what is going on.
编辑:好的。通过调试,我发现它与嵌入式Tomcat有关。除此之外,我不知道发生了什么。
EDIT 2:
编辑2:
Found that the problem is in org.springframework.web.servlet.DispatcherServlet#getHandler. It simply doesn't find a HandlerMapping for that request. Do I have to register one?
发现问题出在org.springframework.web.servlet.DispatcherServlet#getHandler。它根本找不到该请求的 HandlerMapping。我必须注册一个吗?
采纳答案by Glauber Néspoli
OK! Found the problem.
好的!发现问题了。
This link helped me: https://samerabdelkafi.wordpress.com/2014/08/03/spring-mvc-full-java-based-config/
这个链接对我有帮助: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 a 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 being found but not rendered.
通过设置默认处理程序,我将不再得到一个白页,而是将 JSP 代码作为 html,这清楚地告诉我正在找到 JSP 但没有呈现。
So the answer was on this page: JSP file not rendering in Spring Boot web application
所以答案就在这个页面上:JSP file not rendering in Spring Boot web application
I was missing the tomcat-embed-jasperartifact.
我错过了tomcat-embed-jasper工件。
回答by Bheeman
Add below dependency to your pom.xml
将以下依赖项添加到您的 pom.xml
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
回答by Venki Ram
The tips here helped me when I got stuck with a similar problem. I fixed it after adding this fragment on my configuration
当我遇到类似问题时,这里的提示对我有所帮助。我在我的配置中添加了这个片段后修复了它
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}