【问题标题】:spring mvc framework hello world shows error 404spring mvc框架hello world显示错误404
【发布时间】:2013-10-24 15:51:32
【问题描述】:

我是 spring mvc 框架的新手,并遵循了这个 site 的 hello world 教程

错误:请求的资源不可用

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
    <display-name>HelloWorldExampleWithSpring3MVCInEclipse</display-name>
    <servlet>
        <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
                /WEB-INF/app-config.xml
            </param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
        <url-pattern>*.htm</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

app-config.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:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
    <!-- Scans the classpath of this application for @Components to deploy as beans -->
    <context:component-scan base-package="com.raistudies" />
    <!-- Configures the @Controller programming model -->
    <mvc:annotation-driven />
    <!-- Resolves view names to protected .jsp resources within the /WEB-INF/views directory -->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

HelloWorldAction.java

package com.raistudies.actions;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class HelloWorldAction {

    @RequestMapping(value="/hello",method=RequestMethod.GET)
    public ModelAndView sayHello(Model model){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("hello");
        model.addAttribute("HelloMessage", "Hello World from My First Spring 3 mvc application");
        return mv;
    }
}

hello.jsp

<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ page session="true" %>
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Hello World with Spring 3 MVC</title>
    </head>
    <body>
        <h1>Welcome! Spring MVC is working well.</h1><br />
        ${HelloMessage}
    </body>
</html>

【问题讨论】:

  • 您的代码和配置似乎正确。您尝试从浏览器访问哪个 URL?你在访问http://localhost:8080/HelloWorldExampleWithSpring3MVCInEclipse/hello.htm吗?
  • 是的,我使用的是相同的 URL,即使我创建了 index.jsp,它也会抛出相同的错误
  • 我在 web inf 中没有 HelloWorldAction 控制器,但在 src 文件夹中。有什么区别吗
  • 您可以将HelloWorldAction.java 放到src 文件夹中。 HelloWorldAction.class 使用默认动态 Web 项目设置在 WEB-INF/classes 文件夹中生成。您可以在项目的属性 -> 部署程序集中检查此设置。
  • 如果一切正常,错误的原因可能是什么

标签: spring-mvc spring-webflow


【解决方案1】:

web.xml中,当我们将“*htm”替换为“/”时,解决了resource not available的错误。

【讨论】:

    【解决方案2】:

    最初的问题是因为您将所有以 *.htm 结尾的请求发送到 Spring:

    <servlet-mapping>
        <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
        <url-pattern>*.htm</url-pattern>
    </servlet-mapping>
    

    但是你的控制器被映射到/hello,没有扩展名。

    将控制器中的@RequestMapping 更改为:

    @RequestMapping(value="/hello.htm",method=RequestMethod.GET)
    

    【讨论】:

      【解决方案3】:

      如果您访问的是http://localhost:8080/HelloWorldExampleWithSpring3MVCInEclipse/,则需要在您的项目中创建一个WebContent/index.jsp 以使该URL 起作用。

      index.jsp:

      <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
         pageEncoding="ISO-8859-1"%>
      <jsp:forward page="/hello.htm"></jsp:forward>
      

      查看示例代码here

      【讨论】:

        猜你喜欢
        • 2017-10-11
        • 1970-01-01
        • 1970-01-01
        • 2012-11-20
        • 1970-01-01
        • 2013-02-22
        • 2012-05-18
        • 2016-07-14
        • 2015-11-07
        相关资源
        最近更新 更多