【问题标题】:spring mvc HTTP Status 404 -spring mvc HTTP状态404 -
【发布时间】:2017-08-22 18:39:38
【问题描述】:

我正在尝试在 Eclipse 中制作一个 spring hello world 程序。这是代码

index.jsp

<a href="hello.html">click</a>

HelloWorldController.java

package com.javatpoint;
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 SPRING MVC HOW R U";
        return new ModelAndView("hellopage", "message", message);
    }
}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app 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">
 <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>

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="com.javatpoint" />
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>
</beans>

hellopage.jsp

Message is: ${message}

当我转到http://localhost:8080/SpringMVCBasic/ 时,我正确打开了 index.jsp 页面

但是当我点击页面上的链接时,即 url http://localhost:8080/SpringMVCBasic/hello.html,我得到了错误

HTTP Status 404 -

当我使用http://localhost:8080/SpringMVCBasic/hello 这样的网址时,我得到了这个

HTTP Status 404 - /SpringMVCBasic/hello

我已从here获取代码

【问题讨论】:

  • 您应该将链接从“hello.html”更改为“hello”,就像在控制器中定义的那样。
  • 尝试将 URL 模式更改为 /*.html
  • 你的上下文路径是什么?您使用的是 Eclipse 还是 Intellij Idea。您在控制台中看到的任何内容。

标签: java spring jsp spring-mvc


【解决方案1】:

您为以下对象提供了映射:

@RequestMapping("/hello")

但你必须这样做:

@RequestMapping("/hello.html")

【讨论】:

  • 你把hellopage.jsp放到WEB-INF/jsp/目录了吗?
  • 是的,上面我提到了我下载整个项目并尝试按原样运行的站点。
  • 有没有添加Spring Core jar文件和Spring Web jar文件
【解决方案2】:

我下载了 eclipse 项目并在 jboss 上运行它。 需要包含以下罐子:

  • 弹簧上下文
  • 弹簧芯
  • 弹簧网
  • spring-webmvc
  • spring-aop
  • 春豆
  • 弹簧表达式

如果我之后使用 Java 8 运行 jboss,一切似乎都可以正常工作。

【讨论】:

    【解决方案3】:

    在 spring-servlet.xml 中启用 Spring MVC @Controller 编程模型,如下所示:

    <mvc:annotation-driven />
    

    【讨论】:

      【解决方案4】:

      在你的 index.jsp 中而不是写

      href="hello.html">
      change to
      href="./hello.html">
      

      和

      在HelloworldController.java而不是写

      @RequestMapping("/hello")
      

      改为:-

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

      【讨论】:

        【解决方案5】:

        请将所有这些 jar 添加为外部 jar 或将这些 jar 添加到您的 lib 文件夹中

        Spring Jar

        【讨论】:

        • 我已经通过eclipse的属性添加了所有spring framework 4.1.1 JAR文件。
        猜你喜欢
        • 2023-04-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-04-13
        • 2015-11-25
        • 1970-01-01
        • 1970-01-01
        • 2014-10-24
        相关资源
        最近更新 更多