在DispatcherServlet中找不到带有URI [/ FirstSpringMVCProject / welcome]的HTTP请求的映射,名称为' spring-dispatcher'

时间:2016-06-20 17:27:47

标签: java spring spring-mvc

我是弹簧mvc框架的新手,我有以下问题。因此,当我在服务器上运行下面的代码然后在浏览器中键入以下URL http://localhost:8080/FirstSpringMVCProject/welcome时,我收到错误404错误呈现而没有其他内容。

的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID" version="3.0">
    <display-name>FirstSpringMVCProject</display-name>

    <servlet>
        <servlet-name>spring-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

    </servlet>

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


</web-app>

弹簧调度-servlet.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"
    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"> 

    <bean id="HandlerMapping"
        class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />

    <context:component-scan base-package="com.stack" />

    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>


    </bean>


</beans>

HelloPage.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>

<head>
<!-- <title>Insert title here</title> -->
</head>

<h1> First Spring MVC Application Demo </h1>

<h2>${msg}</h2>

<body>

</body>
</html>

HelloController中

package com.stack;

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

@Controller
public class HelloController {

    @RequestMapping("/welcome")
    public ModelAndView helloWorld() {
        ModelAndView model = new ModelAndView("HelloPage");
        model.addObject("msg", "hello world!");

        return model;
    }

}
我在浏览器中输入的

网址

  

http://localhost:8080/FirstSpringMVCProject/welcome

在浏览器中输入URL后,我在控制台中收到以下内容:

Jun 20, 2016 7:15:15 PM org.springframework.web.servlet.PageNotFound noHandlerFound
WARNUNG: No mapping found for HTTP request with URI [/FirstSpringMVCProject/welcome] in DispatcherServlet with name 'spring-dispatcher'

屏幕截图

enter image description here

1 个答案:

答案 0 :(得分:-1)

请添加

web.xml中的

<load-on-startup>1</load-on-startup>。这将初具春天。

同时添加

<mvc:annotation-driven /> 

在spring-dispatcher-servlet.xml中 支持像Jackson这样的Spring mvc消息转换器