简单的spring应用程序无法启动(映射问题)

时间:2013-06-22 21:53:23

标签: spring configuration

我的弹簧应用有问题。我正在尝试创建一个非常简单的应用程序。

的web.xml

    <web-app version="3.0"
         xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    http://java.sun.com/xml/ns/j2ee/web-app_3_0.xsd">

    <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
    </context-param>

    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>
</web-app>

MVC-调度-servlet.xml中

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

    <context:component-scan base-package="com.controller" />
    <context:annotation-config/>
    <mvc:annotation-driven/>

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

MainController

package com.controller;

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

@Controller
public class MainController {

    @RequestMapping("/")
    public String index() {
        return "index";
    }
}

的index.jsp

<%@page contentType="text/html; charset=utf-8" %>

<!DOCTYPE html>
<html>
<head>
    <title>Some page</title>
    <jsp:include page="parts/head.jsp"/>
</head>

<body>
<jsp:include page="parts/header.jsp"/>

<section role="main">
    Hello. This will be main page.
</section>

<jsp:include page="parts/footer.jsp" />
</body>
</html>

所以......一切看起来都不错......但是有一个问题。我正在启动此应用并记录:

INFO: Mapped "{[/],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String com.controller.MainController.index()
WARNING: No mapping found for HTTP request with URI [/WEB-INF/pages/index.jsp] in DispatcherServlet with name 'mvc-dispatcher'
jun 23, 2013 12:37:49 AM org.springframework.web.servlet.DispatcherServlet noHandlerFound

因此。 Tomcat找到控制器,执行它,找到视图(页面)和...找不到它的映射(页面)。我的错误在哪里?谢谢你的回答

1 个答案:

答案 0 :(得分:0)

改变这个:

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

到此:

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