为什么不让我从WebMvcConfigurer界面重写addViewControllers方法?

时间:2019-11-01 20:16:35

标签: spring-boot

上下文:

1-启动新的Spring Boot Web应用程序; 2-实现WebMvcConfigurer接口的WebMvcConfig类

该代码是从以前工作的Spring MVC项目中复制的。

STS建​​议删除覆盖注释,并且删除确实会使错误消失,但是,我的意图是覆盖该方法,并且在没有覆盖的情况下它真的可以工作吗?

import javax.sql.DataSource;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.lookup.JndiDataSourceLookup;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
import org.springframework.web.servlet.view.JstlView;
import org.springframework.web.servlet.view.UrlBasedViewResolver;

@Configuration
@ComponentScan("com.xxxxxx.mwadmin")
@EnableWebMvc
public class WebMvcConfig implements WebMvcConfigurer {

    @Bean
    public DataSource dataSource(){
        final JndiDataSourceLookup dsLookup = new JndiDataSourceLookup();
        dsLookup.setResourceRef(true);
        DataSource dataSource = dsLookup.getDataSource("jdbc/springdb");
        return dataSource;

    }

    @Bean
    public RequestMappingHandlerMapping requestMappingHandlerMapping() {
        RequestMappingHandlerMapping rmhm = new RequestMappingHandlerMapping();
        rmhm.setUseSuffixPatternMatch(true);
        rmhm.setUseTrailingSlashMatch(true);
        return rmhm;
    }

    @Bean
    public UrlBasedViewResolver urlBasedViewResolver() {
        UrlBasedViewResolver resolver = new UrlBasedViewResolver();
        resolver.setPrefix("/WEB-INF/views/");
        resolver.setSuffix(".jsp");
        resolver.setViewClass(JstlView.class);

        return resolver;
    }

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {

        registry.addViewController("/").setViewName("home");

    }

}

问题:

在我的配置类STS上,显示错误“ WebMvcConfig类型的方法addViewControllers(ViewControllerRegistry)必须重写超类方法”。

0 个答案:

没有答案