【问题标题】:404 error in simple Spring project简单 Spring 项目中的 404 错误
【发布时间】:2014-08-31 16:47:40
【问题描述】:

我刚刚开始使用 Spring MVC 框架。我正在尝试做helloworld 示例,但我检查的每个教程都有一些我不知道这意味着什么的问题! 例如,当我单击 sayhello 时,该项目出现 404 错误: 我使用 eclipse Kepler 和 tomcat 7.0 和 Spring 4.0 ; 在构建路径中添加的所有 Spring .jar 文件。 控制器:

    package net.spring3.controller;

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

@Controller
public class HelloWorldController {

    @RequestMapping(value = "/hello")
    public ModelAndView helloWorld() {

        String message = "Hello World";
        return new ModelAndView("hello", "message", message);
    }
}

spring-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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    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">

    <context:component-scan base-package="net.spring3.controller" />

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

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>Spring3MVC</display-name>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

    <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>*.html</url-pattern>
    </servlet-mapping>
</web-app>

WEB-INF/jsp/hello.jsp:

    <html>
<head>
<title>Spring</title>
</head>
<body>${message}
</body>
</html>

index.jsp:

    <html>
<head>
<title></title>
</head>
<body>
    <a href="hello.html">Say Hello</a>
</body>
</html>

【问题讨论】:

    标签: java eclipse spring jsp spring-mvc


    【解决方案1】:

    改变

    @RequestMapping(value = "/hello")
    

    @RequestMapping(value = "/hello.html")
    

    DispatcherServlet 注册的默认DefaultAnnotationHandlerMapping 不处理路径扩展映射。

    您也可以指定

    <mvc:annotation-driven />
    

    在您的 XML 配置中,使用适当的命名空间。这将注册一个RequestMappingHandlerMapping,它将您的处理程序映射到匹配扩展映射,如*.html

    本教程似乎有问题。

    【讨论】:

    • 谢谢...你能推荐一个学习Spring MVC的好教程吗?
    • @amir 我不喜欢回答这个问题,因为这让我对我无法控制的事情负责。我建议你阅读 Spring MVC 文档,它非常详细。然后编写自己的代码,尝试文档中详述的各种选项。我就是这样做的。
    【解决方案2】:

    我之前也遇到过同样的问题,如果你的服务器引导你正确的路径 WEB-INF/jsp/hello.jsp 但是那个页面是 404,它一定是 tomcat 阻止了你的页面。

    所以我所做的就是将该页面移动到 /webapp/WebContent/jsp/hello.jsp 并将 internalResuorceViewResolver 的路径更改为 /WebContent/jsp

    这应该可以解决问题。

    【讨论】:

      猜你喜欢
      • 2022-08-10
      • 2013-04-05
      • 2019-12-13
      • 1970-01-01
      • 2018-11-12
      • 1970-01-01
      • 2012-11-12
      • 2016-05-13
      • 2018-10-04
      相关资源
      最近更新 更多