java - Spring MVC 项目中的 HTTP 服务器状态 404 错误

标签 java spring tomcat model-view-controller

我正在遵循与本 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>

源项目

enter image description here

那么如何才能在填完表单后成功得到“display.jsp”页面。

最佳答案

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

你的 Controller 方法应该是这样的

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

关于java - Spring MVC 项目中的 HTTP 服务器状态 404 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59210476/

相关文章:

java - 使用 JAXB XMLAnyElement 类型的样式返回动态元素名称

java - Struts 测试 JUnit4

java - 在 Spring 3 中禁用架构验证

java - 对 Java 代码的更改未反射(reflect)在更新的 Web 项目 WAR 中

java - 如何用java程序杀死一个java程序

java - 如何在子类型上使用为通用父类(super class)型声明的 Guava 函数?

java - 尝试使用 Parse 中的对象在 Android 中填充 ListView

java - 在面向服务的架构中何时使用 Web api 而不是依赖注入(inject)

string - Spring 3.0 基于注解的 Autowiring

java - 如何使用 JSESSIONID 设置特定 session ?