代码之家  ›  专栏  ›  技术社区  ›  sagar limbu

javax.servlet.ServletException:无法解析名为“dispatcherServlet”的servlet中名为“forward:/index”的视图

  •  0
  • sagar limbu  · 技术社区  · 6 年前

    这是我的参考项目

    https://github.com/Baeldung/spring-security-registration
    

    我使用的是springboot2,我想完全控制我的MVC。 所以我用 @EnablWebMvc 禁用自动配置为如下所示:-

    @Configuration
    @ComponentScan(basePackages = { "com.example.demo" })
    @EnableWebMvc
    public class MvcConfig implements WebMvcConfigurer {
    
        public MvcConfig() {
            super();
        }
    
        @Autowired
        private MessageSource messageSource;
    
        @Override
        public void addViewControllers(final ViewControllerRegistry registry) {
            registry.addViewController("/").setViewName("forward:/index");
            registry.addViewController("/index");
            registry.addViewController("/loginRememberMe");
            registry.addViewController("/customLogin");
            registry.addViewController("/registration.html");
            registry.addViewController("/registrationCaptcha.html");
            registry.addViewController("/logout.html");
            registry.addViewController("/homepage.html");
            registry.addViewController("/expiredAccount.html");
            registry.addViewController("/badUser.html");
            registry.addViewController("/emailError.html");
            registry.addViewController("/home.html");
            registry.addViewController("/invalidSession.html");
            registry.addViewController("/console.html");
            registry.addViewController("/admin.html");
            registry.addViewController("/successRegister.html");
            registry.addViewController("/forgetPassword.html");
            registry.addViewController("/updatePassword.html");
            registry.addViewController("/changePassword.html");
            registry.addViewController("/users.html");
            registry.addViewController("/qrcode.html");
        }
    
        @Override
        public void configureDefaultServletHandling(final DefaultServletHandlerConfigurer configurer) {
            configurer.enable();
        }
    
        @Override
        public void addResourceHandlers(final ResourceHandlerRegistry registry) {
            registry.addResourceHandler("/resources/**").addResourceLocations("/", "/resources/");
        }
    
        @Override
        public void addInterceptors(final InterceptorRegistry registry) {
            final LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();
            localeChangeInterceptor.setParamName("lang");
            registry.addInterceptor(localeChangeInterceptor);
        }
    
        // beans
    
        @Bean
        public LocaleResolver localeResolver() {
            final CookieLocaleResolver cookieLocaleResolver = new CookieLocaleResolver();
            cookieLocaleResolver.setDefaultLocale(Locale.ENGLISH);
            return cookieLocaleResolver;
        }
    
        // @Bean
        // public MessageSource messageSource() {
        // final ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
        // messageSource.setBasename("classpath:messages");
        // messageSource.setUseCodeAsDefaultMessage(true);
        // messageSource.setDefaultEncoding("UTF-8");
        // messageSource.setCacheSeconds(0);
        // return messageSource;
        // }
    
    
        @Bean
        @ConditionalOnMissingBean(RequestContextListener.class)
        public RequestContextListener requestContextListener() {
            return new RequestContextListener();
        }
    
        @Override
        public Validator getValidator() {
            LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
            validator.setValidationMessageSource(messageSource);
            return validator;
        }
    

    enter image description here

    现在当我打开应用程序这是:-

    http://localhost:8888/
    

    javax.servlet.ServletException: Could not resolve view with name 'forward:/index' in servlet with name 'dispatcherServlet'
    

    我试过了

    registry.addViewController("/index");
    registry.addViewController("/index.html");
    

    什么都不管用。

    1 回复  |  直到 6 年前
        1
  •  1
  •   MohammadReza Alagheband    6 年前

    删除 @EnableWebMvc MvcConfig

    换两行试试这些方法,其他的都一样:

    @Override
        public void addViewControllers(final ViewControllerRegistry registry) {
            registry.addViewController("/").setViewName("forward:/index.html"); 
            registry.addViewController("/index.html");
            // add other lines
    
        }