【问题标题】:Spring 4: failing to config Servlet's mappingSpring 4:未能配置 Servlet 的映射
【发布时间】:2014-10-21 11:42:49
【问题描述】:

我正在为我的Spring MVC web-app 的 servlet 配置而苦苦挣扎。

  • 我正在使用 Intellij IDEA IDE;
  • 引入 spring-boot-starter-parent,版本 1.1.8.RELEASE,作为 Maven 的 pom.xml 中的父级,spring-boot-starter-webspring-webmvc 作为依赖项;
  • 用这些 bean 定义了一个 Config 类:

`

@Configuration
@EnableAutoConfiguration
@ComponentScan(basePackages = {"sblog", "sblog.controller", "sblog.repository", "sblog.service"})
@EnableJpaRepositories("sblog.repository")
@EnableTransactionManagement
@EnableWebMvc
public class Config {
    ...
    @Bean
    public DispatcherServlet dispatcherServlet() {
        return new DispatcherServlet(); 
    }

    @Bean
    public ServletRegistrationBean dispatcherRegistration() {
        ServletRegistrationBean registration = new ServletRegistrationBean(
                dispatcherServlet(), "/*", "/index");
        registration.setLoadOnStartup(1);
        System.err.println(registration.getServletName());
        System.err.println(registration.getUrlMappings());
        return registration;
    }
    @Bean
    public InternalResourceViewResolver viewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/resources/");
        resolver.setSuffix(".jsp");
        return resolver;
    }
  • 定义了一个类应用程序:

`

public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Config.class, args);
    }
}

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

`

@Controller
public class ApplicationController {

    @RequestMapping(method = GET, value = "/")
    public String index(Model model) {
        System.err.println("Index");
        return "index";
    }
  • 并定义了以下项目结构:

`

sblog/
├── pom.xml
├── sblog.iml
├── src
│   ├── main
│   │   └── java
│   │       └── sblog
│   │           ├── Application.java
│   │           ├── Config.java
│   │           ├── controller
│   │           │   ├── ApplicationController.java
│   │           │   ├── PageController.java
│   │           │   ├── PostController.java
│   │           │   └── SessionController.java
│   │           ├── orm
│   │           │   ├── Author.java
│   │           │   └── Post.java
│   │           ├── repository
│   │           │   ├── AuthorRepository.java
│   │           │   └── PostRepository.java
│   │           ├── service
│   │           │   └── PostService.java
│   │           └── WebMVCApplicationInitializer.java
│   └── resources
│       └── index.jsp

`

但每次我访问根目录时,我都会得到o.s.web.servlet.PageNotFound : No mapping found for HTTP request with URI [/resources/index.jsp] in DispatcherServlet with name 'dispatcherServletRegistration'

提前感谢您,任何帮助将不胜感激! :)

【问题讨论】:

    标签: java spring jsp spring-mvc servlets


    【解决方案1】:

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

    我建议删除除@Configuration@EnableAutoConfiguration@ComponentScan 之外的所有注释。其他一切都将由 Spring Boot 添加/提供。我还建议将您的 ConfigApplication 课程合并为一个。留下以下内容(是的,没有 @Bean 方法)..

    @Configuration
    @EnableAutoConfiguration
    @ComponentScan
    public class Application {
    
        public static void main(String[] args) {
            SpringApplication.run(Config.class, args);
        }
    }
    

    您只需添加spring-boot-starter-webspring-boot-starter-data-jpa作为依赖项,其他必要的依赖项(spring-webmvc等)将自动添加。当然你仍然需要添加hibernate作为依赖。

    Spring Boot 将检测 Spring Web 并为您配置 DispatcherServlet 和其他 bean。

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

    spring.view.prefix=
    spring.view.suffix=.jsp
    

    这对应于InternalResourceViewResolver 的前缀和后缀属性。要查看属性列表,请查看the reference guide

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

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

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

    【讨论】:

    • 谢谢!我不知道 boot 和 JSP 的问题,所以我改用了 thymeleaf,使用了一个入门项目。
    【解决方案2】:

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

    @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;
        }
    }
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-30
      • 2016-01-02
      • 1970-01-01
      • 1970-01-01
      • 2020-03-02
      • 2014-06-27
      • 1970-01-01
      • 2016-12-31
      相关资源
      最近更新 更多