2013-06-22 44 views
0

我在弹簧应用程序中遇到问题。我正在尝试创建非常简单的应用程序。简单的弹簧应用程序无法启动(映射问题)

的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找到控制器,执行它,找到视图(页面),...找不到映射(页面)。我的错误在哪里?谢谢您的回答

+0

什么是你用来访问你的应用程序的完整网址? – vincentks

+0

localost:8080 /问题已经解决。感谢您的回答。 – DemongD

回答

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

非常感谢。它有助于!!! – DemongD