注释驱动Spring 2.5 MVC无法正常工作

时间:2011-12-28 02:05:34

标签: spring-mvc

我正在尝试一个非常简单的Spring MVC,它只显示一条欢迎信息。

我的web.xml如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.5"
 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_2_5.xsd">
 <display-name>courtannotation</display-name>
 <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/courtannotation-servlet.xml</param-value>
 </context-param>
 <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>
 <servlet>
  <servlet-name>courtannotation</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <load-on-startup>1</load-on-startup>
 </servlet>
 <servlet-mapping>
  <servlet-name>courtannotation</servlet-name>
  <url-pattern>*.htm</url-pattern>
 </servlet-mapping>
</web-app>

我的bean配置文件如下:

<?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-2.5.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
    <context:component-scan base-package="com.apress.springrecipes.court.web" />

    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
    <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>

    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>
    <bean id="messageSource"
        class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basename" value="messages" />
    </bean>    
</beans>

我的控制器 - WelcomeController如下:

package com.apress.springrecipes.court.web;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/welcome.htm")
public class WelcomeController {

    @Autowired
    public WelcomeController() {

    }

    @RequestMapping(method = RequestMethod.GET)
    public ModelAndView welcome() {
        Date today = new Date();
        return new ModelAndView("welcome", "today", today);
    }
}

在WEB-INF lib中,我有以下jar文件:

  1. 的spring.jar
  2. 弹簧webmvc.jar
  3. 弹簧context.jar
  4. 弹簧上下文support.jar
  5. 在类路径中我有servlet-api.jar。 我使用的是Spring 2.5.5,Eclipse Indigo,用于eclipse的Spring IDE插件,java 1.6,windows XP,jBoss-5.1.0。 http://localhost:8080/courtannotation/welcome.htm导致[PageNotFound]在DispatcherServlet中找不到带有URI [/courtannotation/welcome.htm]的HTTP请求的映射,名称为“courtannotation”。 我的welcome.jsp如下:

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>
    <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
    <%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title><spring:message code="welcome.message" text="Welcome to Court Reservation System"/></title>
    </head>
    <body>
    <fmt:formatDate value="${today}" pattern="yyyy-MM-dd"/>
    </body>
    </html>
    

    我的message.properties如下:

    welcome.title=Welcome
    welcome.message=Welcome to Court Reservation System
    

    我在这里缺少什么?请帮助。

0 个答案:

没有答案