SpringMVC控制器无法处理请求

时间:2017-04-01 06:24:44

标签: java spring spring-mvc

  1. 我遵循SprinvMVC章节中的“Spring in action”一书中的说明

  2. 当我运行tomcat服务器并尝试演示时,它会返回如下错误:

    HTTP状态404 - 未找到

    输入状态报告

    描述原始服务器未找到目标资源的当前表示,或者不愿透露该目标资源是否存在。

  3. enter image description here

    1. 我使用macOS,tomcat9,Intellij 2017.1,jdk 1.9 这是我的代码。
    2. 的web.xml:

      <?xml version="1.0" encoding="UTF-8"?>
      <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
           version="3.1">
      
      <context-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>/WEB-INF/applicationContext.xml</param-value>
      </context-param>
      
      <listener>
          <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>
      <servlet>
          <servlet-name>spitter</servlet-name>
          <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
          <load-on-startup>1</load-on-startup>
      </servlet>
      <servlet-mapping>
          <servlet-name>spitter</servlet-name>
          <url-pattern>/</url-pattern>
      </servlet-mapping>
      

      spitter.xml:

      <?xml version="1.0" encoding="UTF-8"?>
      <beans xmlns="http://www.springframework.org/schema/beans"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns:context="http://www.springframework.org/schema/context"
         xmlns:mvc="http://www.springframework.org/schema/mvc"
         xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
      
      <!--static resources getting-->
      <mvc:resources mapping="/resources/**" location="/WEB-INF/resources"/>
      
      <!--use annotations to create the mapping between-->
      <!-- url and class deal with request(Controller) -->
      <mvc:annotation-driven/>
      
      <!--scan the component and auto regist as bean-->
      <context:component-scan base-package="com.springmvc"/>
      
      <!--Use this bean to map the jsp file according to the name return by Controller-->
      <!--It will automatically add the prefix and suffix to the name string-->
      <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
          <property name="prefix" value="/WEB-INF/views/"/>
          <property name="suffix" value=".jsp"/>
      </bean>
      

      控制器:

      package com.springmvc.controller;
      
      import org.springframework.stereotype.Controller;
      import org.springframework.web.bind.annotation.RequestMapping;
      
      /**
      * Created by xwh on 29/3/2017.
      */
      @Controller
        public class HomeController {
      //    public static final int DEAFAULT_SPITTLES_PER_PAGE = 25;
      
      
      public HomeController() {
          System.out.println("-------HomeController init-------");
      }
      
      @RequestMapping("/")
      public String showHomePage() {
      
          System.out.println("-------showHomePage Method show-------");
      
      
          return "home";
        }
      }
      

      这是我的目录图片。 enter image description here

1 个答案:

答案 0 :(得分:1)

你的contextConfigLocation文件是spitter-servlet.xml吗? 在您的web.xml文件中,您定义了上下文配置文件是applicationContext.xml。这意味着您没有使用spitter.xml中的配置。 尝试将contextConfigLocation更改为/WEB-INF/spitter-servlet.xml