web.xml无法正确标记Spring ServletDispatcher

时间:2014-12-28 17:40:35

标签: java xml spring spring-mvc tomcat

所以我正在制作一个简单的项目来测试Spring tutorial,并遇到一个我无法弄清楚的问题。

我的项目结构:

Project structure

的web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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/javaee/web-app_3_0.xsd">

    <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>*.html</url-pattern>
        <url-pattern>*.htm</url-pattern>
        <url-pattern>*.json</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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

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

</beans>

HomeController.java:

package com.itaSS.controller;

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

@Controller
public class HomeController {

    @RequestMapping("/home")
    public String home() {
        return "/WEB-INF/jsp/home.jsp";
    }
}

所以当在tomcat上部署战争并尝试去localhost:8080 / home.html之后 我明白了:

Tomcat response

我确信在web.xml中有些错误或者我错过了什么。 如果有人帮我这个,我将非常感激!

2 个答案:

答案 0 :(得分:0)

我认为你的dispatcher-servlet.xml文件中缺少以下行。

<mvc:annotation-driven/>

答案 1 :(得分:0)

您需要在应用程序上下文文件dispatcher-servlet.xml中定义ViewResolver,如下所示:

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