当前位置: 首页 > 知识库问答 >
问题:

如何修复“HTTP状态404”Spring MVC

赵经国
2023-03-14

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" 
version="4.0">
<display-name>spring-mvc-example</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<display-name>spring-mvc-example</display-name>

<!-- Add Spring MVC DispatcherServlet as front controller -->
<servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring-servlet.xml</param-value>
        </init-param>
    <load-on-startup>1</load-on-startup>
   </servlet>

<servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>/*</url-pattern> 
</servlet-mapping>
</web-app>
spring-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc 
http://www.springframework.org/schema/mvc/spring-mvc.xsd
    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">

<!-- DispatcherServlet Context: defines this servlet's request-processing 
    infrastructure -->

<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />
<context:component-scan base-package="com.spring" />

<!-- Resolves views selected for rendering by @Controllers to .jsp resources 
    in the /WEB-INF/views directory -->
<beans:bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <beans:property name="prefix" value="/WEB-INF/views/" />
    <beans:property name="suffix" value=".jsp" />
</beans:bean>

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>spring-mvc-example</groupId>
<artifactId>spring-mvc-example</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>Spring MVC Example</name>
<description>Picco esempio</description>
<!-- Add Spring Web and MVC dependencies -->
<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>4.3.9.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>4.3.9.RELEASE</version>
    </dependency>
    <!-- Servlet -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>jsp-api</artifactId>
        <version>2.1</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>
</dependencies>
<build>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.0</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
    <plugin>
    <artifactId>maven-war-plugin</artifactId>
    <version>3.2.1</version>
    <configuration>
      <warSourceDirectory>WebContent</warSourceDirectory>
    </configuration>
  </plugin>
</plugins>

homeController.java

package com.spring.controller;

import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.spring.model.User;


@Controller

public class HomeController {

/**
 * Simply selects the home view to render by returning its name.
 */
@RequestMapping(value = "/", method = RequestMethod.GET)
public String home(Locale locale, Model model) {
    System.out.println("Home Page Requested, locale = " + locale);
    Date date = new Date();
    DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);

    String formattedDate = dateFormat.format(date);

    model.addAttribute("serverTime", formattedDate);

    return "home";
}

@RequestMapping(value = "/user", method = RequestMethod.POST)
public String user(@Validated User user, Model model) {
    System.out.println("User Page Requested");
    model.addAttribute("userName", user.getUserName());
    return "user";
}

}

home.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ page session="false"%>
<html>
  <head>
    <title>Home</title>
 </head>
  <body>
<h1>Hello world!</h1>
    <P>The time on the server is ${serverTime}.</p>
     <form action="user" method="post">
       <input type="text" name="userName"><br> 
       <input type="submit" value="Login">
 </form>
  </body>
</html>
%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd">
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>User Home Page</title>
  </head>
  <body>
    <h3>Hi ${userName}</h3>
  </body>
</html>

共有1个答案

羊舌炯
2023-03-14

两个问题:

  1. spring”/">更改为
  2. 更改为 (因为您的文件夹名为view)
 类似资料:
  • 我刚刚在spring tool suit中创建了一个示例spring mvc项目(没有进行任何修改),当我执行它时,我将按照index.jsp获得输出。但在此之后,我只在view文件夹中添加一个index.html页面,并进行了以下两个更改 > 在控制器类i中添加:返回“index”; 在Pivotal Server Developer Edition V3.1上执行更改后的代码时,我得到HTTP

  • 我正在学习Spring MVC,当我试图运行http://localhost:8080/firstspringmvc/welcome.html时,它会出现错误 HTTP状态404-/firstspringmvc/welcome.html

  • 我试图在Eclipse中制作一个spring hello world程序。这是代码 index.jsp HelloWorldController.java 当我使用像http://localhost:8080/springmvcbasic/hello这样的url时,我得到的是 我已经从这里拿走了代码

  • 我试图测试Spring MVC,并编写了以下代码: web.xml:http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd“version=”3.0“> Apache Tomcat/8.0.39 Tomcat控制台日志:

  • 我正面临着Tomcat404错误的确切问题,并尝试了所有的建议,但没有什么能帮助我修复这个问题。Apache Tomcat9.0和Eclipse Oxywood是我目前使用的,并遵循YouTube上的教程。

  • 问题内容: 我看到了一些类似的问题(像这样),但是没有一个问题专门解决这个问题。 因此,Google 现在支持SPA,并且大多数网络浏览器都支持HTML5 pushState 。 我的AngularJS(但可能是任何JS东西)网站都使用URL来确定API路由。然后执行API调用,然后相应地呈现内容。 但是,由于 每个URL都 返回HTTP 200状态代码(包括example.com/get-fre