Spring基本的MVC应用程序无法正常工作


问题内容

我尝试了《 Spring in Action 3rd Edition》一书中所述的Spring
MVC应用程序。不幸的是,到目前为止,浪费了6个小时之后,我仍无法运行最简单的应用程序。我总是收到“ HTTP状态404”错误。

我在这个论坛上发现了许多关于“
@RequestMapping在Spring中不起作用”的主题,现在我想知道我是在遵循正确的书还是有一些非常基础的东西困扰着许多Spring学习者。

我尝试在书本中运行MVC应用程序(Ch-7,sitter-web),但同样,该应用程序也无法正常工作。看起来作者在尝试使用自己的应用程序时并没有特别注意。

为了完整起见,我的web.xml是:

<?xml version="1.0" encoding="UTF-8" ?>

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

<context-param>
    <description>locations of the Spring configuration files</description>
    <param-name>contextConfigLocation</param-name>
    <param-value>
    </param-value>
</context-param>

<servlet>
    <servlet-name>application</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

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

<servlet-mapping>
    <servlet-name>application</servlet-name>
    <url-pattern>*.jsp</url-pattern>
</servlet-mapping>

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


</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"
 xmlns:util="http://www.springframework.org/schema/util"
 xmlns:mvc="http://www.springframework.org/schema/mvc"
 xmlns:p="http://www.springframework.org/schema/p"
 xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

   <!--<start id="spring_component_scan" />-->
   <context:component-scan base-package="com.truality.demo.web.controller" />
   <!--<end id="spring_component_scan" />-->

   <!--<start id="mvc_annotatedcontrollers" />-->
   <mvc:annotation-driven/>
   <!--<end id="mvc_annotatedcontrollers" />-->

   <context:annotation-config />

   <!--<start id="mvc_resources"/>--> 
   <mvc:resources mapping="/resources/**" location="/resources/" />
   <!--<end id="mvc_resources"/>-->

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


 </beans>

我的Controller类是:package com.truality.demo.web.controller;

import java.util.Map;

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

@Controller
public class HomeController {

    @RequestMapping(value={"/home.htm"}, method=RequestMethod.GET)
      public String showHomePage(Map<String, String> model) {
        model.put("name","Google");
        return "home";
    }

当我尝试访问时,总是出现404错误

- http://localhost:8080/demo-prop-1-1/home.htm OR 
- http://localhost:8080/demo-prop-1-1/home.jsp
- http://localhost:8080/demo-prop-1-1/home
- http://localhost:8080/demo-prop-1-1

如果不能解决这个琐碎的问题,我将开始评估其他技术,例如PHP或…。

请帮忙。另外,有人可以给我指出一本优秀的spring书,该书提供了工作示例,以便有人可以快速学习它吗?

非常感谢。


问题答案:

你有这个

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

<servlet-mapping>
    <servlet-name>application</servlet-name>
    <url-pattern>*.jsp</url-pattern>
</servlet-mapping>

DispatcherServlet名为的application处理*.jsp请求没有任何意义。默认情况下,Spring和您通过配置未提供JSP资源的处理程序。摆脱它servlet- mapping

然后你就剩下

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

/*匹配所有内容的路径映射在哪里。这意味着,即使您删除了other <servlet- mapping>,该映射仍将用于处理对以.jsp(以其他方式结尾)任何路径的请求。这意味着,资源由您注册解决InternalResourceViewResolver,那就是/WEB- INF/views/home.jsp,还将由Spring的处理DispatcherServlet。但是 同样 ,它没有这样的路径的处理程序。

相反,您需要让Servlet容器的JSP Servlet处理请求。您可以通过映射DispatcherServlet/

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

将其标记为默认servlet。默认的servlet将处理任何其他匹配的请求<servlet-mapping>。大多数Servlet容器都有一个JSP
Servlet映射到*.jsp,因此任何对带有扩展名的资源的请求*.jsp都将由Spring处理,直到您的Spring
DispatcherServlet碰上它。