在DispatcherServlet中找不到带有URI [... / helloworld.xhtml]的HTTP请求的映射,其名称为' dispatcher'

时间:2015-04-24 13:15:05

标签: spring-mvc

大家好,我知道有几十个类似的问题,但我找不到适合我的解决方案。我想做一个简单的'hello world'使用spring mvc的应用程序。它给了我404错误, 并在控制台上:

avr. 22, 2015 4:10:42 PM org.springframework.web.servlet.PageNotFound noHandlerFound
AVERTISSEMENT: No mapping found for HTTP request with URI [/SpringMVC/WEB-  INF/views/helloworld.xhtml] in DispatcherServlet with name 'dispatcher'

的pom.xml

<properties>
    <spring.version>4.1.6.RELEASE</spring.version>
</properties>
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>${spring.version}</version>
</dependency>

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

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

的web.xml

<web-app>
<display-name>Archetype Created Web Application</display-name>

<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>

<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>

</web-app>

WEB-INF /调度程序-servlet.xml中

 <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    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">
    <context:component-scan base-package="com.controller" />

<bean           class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/views/</value>
        </property>
        <property name="suffix">
            <value>.xhtml</value>
        </property>
    </bean>
</beans>

控制器

package com.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class SpringController {

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

}

的index.xhtml

<html>
<body>
<h2>Hello World!</h2>

<h3>
    <a href="hello?name=youness">Click Here</a>
</h3>
</body>
</html>

WEB-INF /视图/ helloworld.xhtml

<html>
<head>
<title>Spring 4 MVC -HelloWorld</title>
</head>
<body>
    <h2>Hello World</h2>
    <h2>
        ${message} ${name}
    </h2>
</body>
</html>

当我点击链接&#34;点击此处&#34;

我收到此错误:

  avr. 23, 2015 2:48:58 PM org.springframework.web.servlet.PageNotFound     noHandlerFound
    AVERTISSEMENT: No mapping found for HTTP request with URI [/SpringMVC/WEB-  INF/views/helloworld.xhtml] in DispatcherServlet with name 'dispatcher'

2 个答案:

答案 0 :(得分:0)

我建议更改web.xml中的servlet-mapping条目。

<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

答案 1 :(得分:0)

原因可能是你的java类文件不在你的目标战争中。 要确保首先执行maven clean编译包然后将战争发布到服务器,请确保在发布war文件时不会出现任何错误。 我也面临同样的问题并通过上述步骤解决了这个问题。