【问题标题】:Unable to map request properly无法正确映射请求
【发布时间】:2016-01-12 04:16:30
【问题描述】:

我正在创建一个仅使用 java 配置的示例 hello world 程序。我已经定义了调度程序 servlet 的配置,并提供了一个映射到 "/" 的 defaultController。但总是以No mapping found for HTTP request with URI [/sample/] in DispatcherServlet with name 'dispatcher' 结尾

有人能指出这里的问题吗?

我的pom.xml

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.sample</groupId>
    <artifactId>sample</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>sample</name>

    <properties>
        <org.springframework.version>4.1.7.RELEASE</org.springframework.version>
    </properties>

    <dependencies>
        <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${org.springframework.version}</version>
        </dependency>

        <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>${org.springframework.version}</version>
        </dependency>

        <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>${org.springframework.version}</version>
        </dependency>

        <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.6</version>
        </dependency>

        <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
        </dependency>

        <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.0.1</version>
        <scope>provided</scope>
        </dependency>

    </dependencies>

    <build>
        <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.3</version>
            <configuration>
            <source>1.8</source>
            <target>1.8</target>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <configuration>
            <failOnMissingWebXml>false</failOnMissingWebXml>
            </configuration>
        </plugin>

        </plugins>
    </build>
    </project>

ApplicationInitializer.java

    package sample.config;

    import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

    public class ApplicationInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

        @Override
        protected Class<?>[] getRootConfigClasses() {
            return new Class<?>[] {RootConfiguration.class};
        }

        @Override
        protected Class<?>[] getServletConfigClasses() {
            return new Class<?>[] {DispatcherServeltConfig.class};
        }

        @Override
        protected String[] getServletMappings() {
            return new String [] {"/"};
        }

    }

DispatcherServeltConfig.java

    package sample.config;

    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.servlet.ViewResolver;
    import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
    import org.springframework.web.servlet.config.annotation.EnableWebMvc;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
    import org.springframework.web.servlet.view.InternalResourceViewResolver;

    @Configuration
    @EnableWebMvc
    public class DispatcherServeltConfig extends WebMvcConfigurerAdapter{

        @Bean
        public ViewResolver viewResolver(){
            InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
            viewResolver.setPrefix("/WEB-INF/views/");
            viewResolver.setSuffix(".jsp");
            return viewResolver;
        }

    }

RootConfiguration.java

    package sample.config;

    import org.springframework.context.annotation.ComponentScan;

    @ComponentScan(basePackages = {"sample.web"})
    public class RootConfiguration {

    }

DefaultController.java

    package sample.web;

    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;

    @Controller
    public class DefaultController {

        @RequestMapping(value="/", method=RequestMethod.GET)
        public String home(){
            return "welcome";
        }

    }

【问题讨论】:

    标签: maven spring-mvc jakarta-ee


    【解决方案1】:

    你需要告诉@EnabledWebMvc注解去哪里搜索那些包含请求映射的类。您可以通过将@ComponentScan 添加到带有@EnableWebMvc (DispatcherServeltConfig) 注释的类中,该类指向包含具有请求映射(控制器)的类的包

    @Configuration
    @EnableWebMvc
    @ComponentScan(basePackages = {"sample.web"})
    public class DispatcherServeltConfig extends WebMvcConfigurerAdapter
    

    【讨论】:

      【解决方案2】:

      我认为你需要启用&lt;mvc:default-servlet-handler/&gt;

      此处理程序会将所有请求转发到默认 Servlet

      @Configuration
      @EnableWebMvc
      public class DispatcherServeltConfig extends WebMvcConfigurerAdapter{
      
          @Bean
          public ViewResolver viewResolver(){
              InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
              viewResolver.setPrefix("/WEB-INF/views/");
              viewResolver.setSuffix(".jsp");
              return viewResolver;
          }
      
          @Override
          public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
              configurer.enable();
          }
      
      }
      

      【讨论】:

        猜你喜欢
        • 2017-01-19
        • 1970-01-01
        • 2018-09-12
        • 1970-01-01
        • 2016-05-23
        • 1970-01-01
        • 2016-06-30
        • 2023-03-04
        • 2020-01-22
        相关资源
        最近更新 更多