一尘不染

Spring Boot,Java Config-在名称为“ dispatcherServlet”的DispatcherServlet中找不到带有URI[/…]的HTTP请求的映射

spring-boot

这在stackOverflow中是一个非常普遍的问题,但是同一问题的主题都无法解决我的问题。

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

所以我有一个使用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.classIS是主要的包。通过@ComponentScan@SpringBootApplication带来的,它会扫描都是对等的配置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;
    }
}

LoginController.class定义为:

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

    @RequestMapping(value = "/login")
    public ModelAndView login() {
        return new ModelAndView("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>

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

鉴于所有这些,当我尝试访问 / adm / login,都会调用* LoginController.login()
。但是,显然,它无法找到正确的jsp文件。
* __

它返回 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'

任何建议表示赞赏!

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

编辑2:

发现问题出在 org.springframework.web.servlet.DispatcherServlet#getHandler中
。它根本找不到该请求的HandlerMapping。我必须注册一个吗?


阅读 764

收藏
2020-05-30

共1个答案

一尘不染

好!找到了问题。

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

更具体地说,此配置:

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

通过设置默认处理程序,我将不再获得白页,而是将JSP代码转换为html,这清楚地告诉我已找到但未呈现JSP。

所以答案就在此页面上:JSP文件未在Spring BootWeb应用程序中呈现

我错过了 tomcat-embed-jasper 工件。

2020-05-30