当前位置: 首页 > 知识库问答 >
问题:

Spring Boot,JavaConfig-在DispatcherServlet中找不到与URI[/...]的HTTP请求的映射

冯茂实
2023-03-14

这在stackOverflow中是一个非常常见的问题,但同一个问题的主题都不能解决我的问题。

我们有一个使用xml配置的模板配置,但现在我们正试图摆脱它,开始使用Java配置。

所以我有一个使用Java配置和Spring Boot的新项目。我们也使用JSP和Tiles 3。

问题是:它无法呈现我们的管理员登录页面。

以下是代码:

主配置类:

@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是主程序包。通过@springbootplication带来的@ComponentScan,它扫描main包上的其他配置。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;
    }
}

LoginController。类别定义为:

@Controller
@RequestMapping(value = "/adm")
public class LoginController {

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

平铺定义中。xml我对登录模板分幅有以下定义:

<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>

请注意,这两个文件都存在。

考虑到所有这些,LoginController。当我尝试访问/adm/login时,确实会调用login()。但它没有找到合适的jsp文件。

它返回一个404。启用跟踪后,我会得到以下日志:

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'

任何建议都将不胜感激!

编辑:好的。通过调试,我发现它与嵌入式Tomcat有关。除此之外,我不知道发生了什么。

编辑2:

发现问题在组织中。springframework。网状物servlet。DispatcherServlet#getHandler。它根本找不到该请求的HandlerMapping。我必须注册吗?

共有3个答案

常英资
2023-03-14

当我遇到类似问题时,这里的提示帮助了我。在我的配置中添加了这个片段后,我修复了它

@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
    configurer.enable();
}
隆钊
2023-03-14

将以下依赖项添加到您的pom.xml

    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
        <scope>provided</scope>
    </dependency>
谭梓
2023-03-14

好!找到问题了。

这个链接帮助我:https://samerabdelkafi.wordpress.com/2014/08/03/spring-mvc-full-java-based-config/

更具体地说,这种配置:

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

通过设置默认处理程序,我将不再得到一个白色页面,而是作为html的JSP代码,这清楚地告诉我JSP正在被找到,但没有呈现。

所以答案就在这个页面上:JSP文件无法在Spring Boot Web应用程序中呈现

我错过了tomcat嵌入jasper工件。

 类似资料: