spring MVC HTTP 404

时间:2013-07-19 12:25:38

标签: spring spring-mvc

我是Spring MVC的初学者

我尝试在浏览器中显示“hello world”

但我的浏览器显示HTTP 404 -

我的项目结构是:

SpringMVC
     -> src
            -> main
                    -> java
                          -> com
                                -> mkyong
                                         -> commmon
                                                  -> controller


    -> webapp
            -> WEB-INF
                     -> pages

我在文件夹 controller 中有HelloWorldController.java:

 package com.mkyong.common.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

public class HelloWorldController extends AbstractController {

    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest request,
            HttpServletResponse response) throws Exception {

        ModelAndView model = new ModelAndView("HelloWorldPage");
        model.addObject("msg", "hello world");

        return model;
    }

}
页面中的

我有HelloWorldPage.jsp:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<body>
    <h1>Spring MVC Hello World Example</h1>

    <h2>${msg}</h2>

</body>
</html>
WEB-INF 中的

mvc-dispatcher-servlet.xml:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

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

    <bean name="/welcome.htm" 
        class="com.mkyong.common.controller.HelloWorldController" />

</beans>

WEB-INF

中的最终web.xml
<web-app id="WebApp_ID" version="2.4"
    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">

    <display-name>Spring Web MVC Application</display-name>

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

2 个答案:

答案 0 :(得分:0)

在mvc-dispatcher-servlet.xml文件中添加以下行

<bean id="handlerMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>

但是我建议使用更新的方法来使用注释来创建MVC应用程序。它更清洁。

请参阅此链接http://static.springsource.org/spring/docs/3.0.x/reference/mvc.html#mvc-ann-controller

答案 1 :(得分:0)

实际上在另一篇文章中,该示例的作者说:“实际上,声明BeanNameUrlHandlerMapping是可选的,默认情况下,如果Spring找不到处理程序映射,DispatcherServlet将自动创建BeanNameUrlHandlerMapping。”

user2319284,我认为您的问题可能是您在web.xml中声明的网址格式是url-pattern&gt; / ,但我认为它必须是 .htm所以当您输入浏览器URL“welcome.htm”模式映射到HelloWorldController类。

尝试使用此web.xml(来自与您的示例相同的站点):

    <web-app id="WebApp_ID" version="2.4" 
    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">

  <display-name>Spring Web MVC Application</display-name>

  <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>*.htm</url-pattern>
  </servlet-mapping>

</web-app>

And use URL http://localhost:8080/SpringMVC/welcome.htm