【问题标题】:404 with spring 3404带弹簧3
【发布时间】:2010-12-21 08:05:04
【问题描述】:

嗨 我正在学习 Spring 3 的第一堂课。我在 Eclipse 中创建了一个具有以下结构的动态 Web 应用程序。

spring3mvc \src\my.spring.HelloWorldController.java
                \WebContent
                   |
                   |-----WEB-INF\jsp\hello.jsp                     
                   |-----index.jsp
                   |-----WEB-INF\web.xml
                   |-----WEB-INF\spring-servlet.xml
                   |-----WEB-INF\lib\...*.jar files

我创建了如下的 spring-servlet.xml

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

        <context:component-scan base-package="my.spring" />
        <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"
            p:prefix="/WEB-INF/jsp/"
        p:suffix=".jsp">

          <property name="contentType" value="text/html; charset=utf-8" />
        </bean>
      </beans>

并对控制器进行编码

package my.spring;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class HelloWorldController {
 @RequestMapping("/hello")
 public ModelAndView helloWorld() {
  String message = "Hello World, Spring 3.0!";
  return new ModelAndView("hello", "message", message);
 }
}

index.jsp 有一个指向 hello 视图的链接

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

</body>
</html>

我终于在 hello.jsp 中放了

<html>
<body>
${message}
</body>
</html>

我的 web.xml 有

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

当我在 Tomcat6 服务器(通过 Eclipse)上运行应用程序时,我可以在以下位置看到索引页面 http://localhost:8080/Spring3MVC/ 。它显示到 hello 页面的链接。当我点击它时(http://localhost:8080/Spring3MVC/hello.html),我得到一个 404 错误。

message /Spring3MVC/hello.html
description The requested resource (/Spring3MVC/hello.html) is not available.

知道如何解决这个问题吗?

谢谢

标记。

【问题讨论】:

  • 我有一个想法,请检查我的增强答案。
  • 添加后是否出现映射消息?
  • 感谢 Ralph 始终如一的帮助。我能够让应用程序运行.. xsi:schemaLocation 中的条目顺序存在一些问题,但我能够把事情做好。非常感谢到 org.life.java 获取详细的配置信息和建议..
  • 主要错误是 web.xml 位于 WebContent 文件夹中,而不是 WebContent/WEB-INF。这很愚蠢..在学习教程时应该更加小心

标签: java spring spring-mvc


【解决方案1】:

您需要配置ViewResolver

这是示例配置:

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

当你返回 new ModelAndView("hello", "message", message); 从上面的conf。它会尝试加载

prefix value + view name + suffix value  

这将是 jsp 所必需的。

您还需要按照以下方式在 web.xml 中映射您的 servlet

<servlet-mapping>
  <servlet-name>dispatcher</servlet-name>
  <url-pattern>*.htm</url-pattern>
</servlet-mapping>

【讨论】:

  • 我尝试使用 class="org.springframework.web.servlet.view.UrlBasedViewResolver" 以及 InternalResourceViewResolver 。另外,我将 viewResolver 的 viewClass 设置为 我的 web.xml 有 servlet-mapping spring*.html.Still我得到一个 404。不明白为什么。
【解决方案2】:

我相信问题不在于视图解析器(它会打印其他异常)。

仔细阅读错误信息,它告诉你问题是什么:

message /Spring3MVC/hello.html description 
The requested resource (/Spring3MVC/hello.html) is not available.

是找不到hello .html(handler),不是jsp。 ——但我不知道具体是什么问题呢。 -- 我试图重现错误,但没有得到完全相同的错误消息。

已添加——发现问题

当您启动服务器时,它会在日志文件中打印到控制器的所有映射。在您的情况下,必须有类似

INFO : org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping - Mapped URL path [/hallo] onto handler 'halloController'

如果您没有这样的语句,那么您的上下文扫描有问题,或者您忘记启用注释驱动的 MVC @Controller 编程模型。这可以通过添加来启用:

<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />

【讨论】:

  • 你可以检查这个,如果你添加一些 system.out.println 消息到你的控制器方法中。
  • 我添加了 System.out.println("message="+message);在控制器的 helloWorld() 方法中。但是,我在运行应用程序时找不到该输出。我已将 Eclipse 的控制台输出粘贴到此 url pastebin.com/nkduFui4
  • 所以问题分析是正确的——你把请求映射到你的控制器有问题。
  • 原谅我的无知..但它是 还是 ?后来我试过了。错误仍然存​​在。(我已经更新了有问题的spring-servlet.xml)..
  • 我是,mvc是xml语言前缀。如果 springframework.org/schema/mvc 您将成为 xml 文档的“默认”命名空间,您可以跳过前缀。但在你的情况下,“默认”是“springframework.org/schema/beans”——所以你必须写
【解决方案3】:

我遇到了同样的问题。

重建步骤

  1. 在 Spring Tool Suite 中打开仪表板
  2. 点击“弹簧模板”
  3. 仪表板“创建”区域中的“项目”
  4. 在 STS 的 Package Explorer 窗格中右键单击新创建的项目
  5. 选择运行方式 > 在服务器上运行
  6. 选择 Tomcat 作为您的服务器,然后单击完成

预期结果
浏览器应该启动并显示由 HomeController > home 操作和 home.jsp 视图页面定义的 Hello World 主页。

实际结果
404错误

解决方案
1. 打开 HomeController.java
2. 在文件的任意位置添加一个空格。是的。空间。按空格键。这就是解决方案。哈哈
3. 像以前一样再次运行项目。

这会给你预期的结果。

我的假设
HomeController 类最初没有编译,所以组件扫描方法没有找到它;有必要对文件进行更改,然后保存。这会强制编译类,从而使 home 控制器和 /home 操作可被发现。

我一直在搞乱配置文件,直到我把头发拉出来。我什至不能告诉你是什么让我想到尝试这个。 ;-) 很高兴我明白了。希望 Spring MVC 3 团队能够对此进行研究,因为它很容易为进入一个很酷的框架设置严重障碍。

【讨论】:

    猜你喜欢
    • 2019-05-07
    • 2019-05-03
    • 1970-01-01
    • 2013-05-06
    • 2015-07-04
    • 2014-06-27
    • 2013-12-25
    • 2016-02-16
    • 2011-05-03
    相关资源
    最近更新 更多