Java Spring 4:无法配置Servlet的映射

Java Spring 4:无法配置Servlet的映射,java,spring,jsp,spring-mvc,servlets,Java,Spring,Jsp,Spring Mvc,Servlets,我正在为我的SpringMVCWeb应用程序的servlet配置而挣扎 我正在使用Intellij IDE; 引入了SpringBootStarter父版本1.1.8.RELEASE,作为Maven的pom.xml中的父版本,SpringBootStarterWeb和SpringWebMVC作为依赖项; 使用以下bean定义了配置类: ` 定义了一个类应用程序: ` ` -添加了一个简单的控制器: ` 并定义了以下项目结构: ` ` 但每次访问根目录时,我都会得到o.s.web.servlet.

我正在为我的SpringMVCWeb应用程序的servlet配置而挣扎

我正在使用Intellij IDE; 引入了SpringBootStarter父版本1.1.8.RELEASE,作为Maven的pom.xml中的父版本,SpringBootStarterWeb和SpringWebMVC作为依赖项; 使用以下bean定义了配置类: `

定义了一个类应用程序: `

` -添加了一个简单的控制器:

`

并定义了以下项目结构: `

`

但每次访问根目录时,我都会得到o.s.web.servlet.PageNotFound:在名为“dispatcherServletRegistration”的DispatcherServlet中找不到URI为[/resources/index.jsp]的HTTP请求的映射


提前感谢您,我们将感谢您的帮助!:

首先,我使用了相同的结构,只是我的“资源”文件夹在“主”文件夹中。其次,我使用了如下配置

@Configuration
@EnableAutoConfiguration
public class CoreConfig {
    @Bean
    public WebMvcConfigurerAdapter initAdapter(){
        return new WebMvcConfigurerAdapter() {
            @Override
            public void addResourceHandlers(ResourceHandlerRegistry registry) {
                registry.addResourceHandler("/**").addResourceLocations("/resources/");
            }
        };
    }

    @Bean
    public InternalResourceViewResolver getInternalResourceViewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setSuffix(".jsp");
        return resolver;
    }
}

如果您想了解更多详细信息,请查看我的示例项目。

首先,我建议您阅读一下Spring Boot可以为您做些什么,您正在努力避免使用Spring Boot

我建议除去@Configuration、@EnableAutoConfiguration和@ComponentScan之外的所有注释。其他一切都将由Spring Boot添加/提供。我还建议将配置类和应用程序类合并为一个。在没有@Bean方法的情况下给您留下以下yes

@Configuration
@EnableAutoConfiguration
@ComponentScan
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Config.class, args);
    }
}
您只需要添加spring boot starter web和spring boot starter数据jpa作为依赖项,其他必要的依赖项spring webmvc等将自动添加。当然,您仍然需要添加hibernate作为依赖项

SpringBoot将检测SpringWeb并为您配置DispatcherServlet和其他bean

接下来,src/resources目录未知,因此它将不能作为资源使用,因此也找不到。我还建议不要将JSP用作视图层。JSP和Spring引导存在一些问题,如中所述。如果确实必须不显式配置InternalResourceViewResolver,而是将application.properties文件添加到src/main/resources,并将以下内容添加到属性

spring.view.prefix=
spring.view.suffix=.jsp
对应于InternalResourceViewResolver的前缀和后缀属性。要查看属性列表,请选中

我建议不要使用src/main/resources/resources文件夹来存储JSP页面,因为在Spring Boot中默认情况下,该目录也是公开可读的。例如,我建议创建一个视图目录。这将导致以下属性

spring.view.prefix=/views/
spring.view.suffix=.jsp

最后一点需要注意的是,Spring Boot驱动的应用程序不会检测到WebApplicationInitializer,因此如果您在其中执行某些操作,则不会对Spring Boot应用程序执行此操作,除非它是特定的Spring Boot WebApplicationInitializer

谢谢大家!!我不知道boot和JSP的问题,所以我使用一个初学者项目切换到了thymeleaf。
@Configuration
@EnableAutoConfiguration
public class CoreConfig {
    @Bean
    public WebMvcConfigurerAdapter initAdapter(){
        return new WebMvcConfigurerAdapter() {
            @Override
            public void addResourceHandlers(ResourceHandlerRegistry registry) {
                registry.addResourceHandler("/**").addResourceLocations("/resources/");
            }
        };
    }

    @Bean
    public InternalResourceViewResolver getInternalResourceViewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setSuffix(".jsp");
        return resolver;
    }
}
@Configuration
@EnableAutoConfiguration
@ComponentScan
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Config.class, args);
    }
}
spring.view.prefix=
spring.view.suffix=.jsp
spring.view.prefix=/views/
spring.view.suffix=.jsp