【问题标题】:HTTP Server Status 404 Error in Spring MVC ProjectSpring MVC 项目中的 HTTP 服务器状态 404 错误
【发布时间】:2020-03-31 06:57:15
【问题描述】:

我遵循与404 Error - Java Spring MVC using Maven in Eclipse from Tutorial 相同的教程,填写表格后我收到 404 错误

index.jsp

<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    <form action="add">
    <input type="text" name="t1"><br>
    <input type="text" name="t2"><br>
    <input type="submit">   
    </form>
</body>
</html>

web.xml

    <!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  
  <servlet>          
    <servlet-name>telusko</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    
  </servlet>
 
  <servlet-mapping>
    <servlet-name>telusko</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

AddController.java

package com.telusko;

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

@Controller
public class AddController {
    @RequestMapping("/add")
    public String add() {
        return "display.jsp";
    }
}

telusko-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:ctx="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-2.5.xsd ">
    
    
    <ctx:annotation-config></ctx:annotation-config>
    <ctx:component-scan base-package="com.telusko"></ctx:component-scan>
</beans>

display.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
Hello World
</body>
</html>

源项目

那么我怎样才能在填写表单后成功获得“display.jsp”页面。

【问题讨论】:

    标签: java spring tomcat model-view-controller


    【解决方案1】:

    您的 telusko-servlet.xml 中缺少 InternalResourceViewResolver bean

    <!-- Define Spring MVC view resolver -->
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/view/" />
        <property name="suffix" value=".jsp" />
    </bean>
    

    将您的 jsp 页面放在 WEB-INF/view 文件夹中而不是其外部也是一个很好的做法。 添加该bean后,您不会返回 显示.jsp

    你的控制器方法应该是这样的

    @RequestMapping("/add")
    public String add() {
        return "display";
    }
    

    另外,避免使用 bean 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:tx="http://www.springframework.org/schema/tx"
    xmlns:mvc="http://www.springframework.org/schema/mvc"   
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx.xsd">
    

    【讨论】:

      【解决方案2】:

      你可以像这样调用“/addController/add”

      @Controller
      @RequestMapping("/addController ")
      public class AddController {
          @RequestMapping("/add")
          public String add() {
              return "display";
          }
      }
      

      【讨论】:

      • 你可以这样调用"/addController/add"
      【解决方案3】:

      它对我有帮助

      我刚刚创建了一些更改,并成功删除了 HTTP 404 添加RequestMapping Annotation后仍然报错

      我刚刚将我的包和类移动到 src/main/java 而不是 进入我必须手动创建的 src/main/resources,以及 这应该可以解决映射错误。

      新项目结构

      【讨论】:

        猜你喜欢
        • 2014-10-24
        • 2017-02-24
        • 2017-03-28
        • 1970-01-01
        • 2017-03-10
        • 2023-04-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多