在DispatcherServlet中找不到带有URI的HTTP请求的映射,其名称为“dispatcher”“

时间:2015-07-21 10:30:56

标签: java spring spring-mvc

获取错误“WARN PageNotFound:1136 - 在名为”dispatcher“的DispatcherServlet中找不到带有URI [/ WebApp_Local / hello]的HTTP请求的映射”

我正在尝试hello world spring maven项目下面是我的文件,同时运行应用程序无法找到控制器方法

请帮助我,我是该地区的新手并在这里挣扎

DispatcherServlet的

    <beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

<context:component-scan base-package="com.abc.controllers" />
<mvc:annotation-driven />
<mvc:default-servlet-handler />
<bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix">
        <value>/WEB-INF/views/</value>
    </property>
    <property name="suffix">
        <value>.jsp</value>
    </property>
</bean>

的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" version="2.5"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name>Archetype Created Web Application</display-name>
<welcome-file-list>
     <welcome-file>index.jsp</welcome-file> 
</welcome-file-list>
<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
</context-param>
<listener>
    <listener-class>
        org.springframework.web.context.ContextLoaderListener
    </listener-class>
</listener>

HelloWorldController.java

package com.abc.controllers;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class HelloWorldController {
String message = "Welcome to Spring MVC!";

@RequestMapping("/hello")
public ModelAndView showMessage(
        @RequestParam(value = "name", required = false, defaultValue =      "World") String name) {
    System.out.println("in controller");

    ModelAndView mv = new ModelAndView("helloworld");
    mv.addObject("message", message);
    mv.addObject("name", name);
    return mv;
    }
  }

下面是错误日志

2015-07-20 21:31:00 DEBUG DispatcherServlet:861 - DispatcherServlet with      name 'dispatcher' processing GET request for [/WebApp_Local/hello]
2015-07-20 21:31:00 DEBUG RequestMappingHandlerMapping:294 - Looking up handler method for path /hello
2015-07-20 21:31:00 DEBUG RequestMappingHandlerMapping:302 - Did not find handler method for [/hello]
2015-07-20 21:31:00 WARN  PageNotFound:1136 - No mapping found for HTTP request with URI [/WebApp_Local/hello] in DispatcherServlet with name 'dispatcher'
2015-07-20 21:31:00 DEBUG DispatcherServlet:997 - Successfully completed request

1 个答案:

答案 0 :(得分:1)

最后确定了问题的问题,因为我的Controller java文件在目标文件夹中找不到,因此dipatcher servlet无法识别控制器映射方法。

感谢您的评论。