【问题标题】:How to fix "HTTP Status 404" Spring MVC如何修复“HTTP 状态 404”Spring MVC
【发布时间】:2019-06-17 23:03:31
【问题描述】:

我的 Spring Web 应用程序有问题:它显示 Apache Tomcat/4.0.6 - HTTP 状态 404 - /spring-mvc-example/(请求的资源 (/spring-mvc-example/) 不可用。 ) 我被这个错误弄疯了,我不知道该怎么办。 我正在使用 STS。 谢谢您的帮助! 这就是文件夹的组织方式。 https://i.stack.imgur.com/ded48.png

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>

user.jsp

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

【问题讨论】:

    标签: java spring apache spring-mvc jsp


    【解决方案1】:

    两个问题:

    1. &lt;context:component-scan base-package="com.spring" /&gt; 更改为 &lt;context:component-scan base-package="com.spring.*" /&gt;
    2. &lt;beans:property name="prefix" value="/WEB-INF/views/" /&gt; 更改为 &lt;beans:property name="prefix" value="/WEB-INF/view/" /&gt; (自从 你的文件夹被命名为视图)

    【讨论】:

    • 这些也是我注意到的。
    • 尝试导航到http://localhost:{your_port_num}/spring-mvc-example/user 路径
    • 我真的不明白问题出在哪里。我也尝试从不同的网站下载一个项目并尝试运行它们,但我有同样的错误,这真的很奇怪。
    • 观察你正在尝试的 url 是一个 POST 操作。尝试通过 any rest-client(Postman、Advanced REST Client 等)访问它
    【解决方案2】:

    将 web.xml servlet 映射更改为

    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>/</url-pattern> 
    </servlet-mapping>
    
    

    然后在 /WEB-INF/spring-servlet.xml 的 contextConfigLocation 文件中进行如下配置:

    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        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">
    
        // to scan for annotation controllers, beans or configurations
        <context:component-scan base-package="com.spring" />
    
        <bean
            class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix">
                <value>/WEB-INF/view/</value>
            </property>
            <property name="suffix">
                <value>.jsp</value>
            </property>
        </bean>
    
    </beans>
    
    

    您没有正确打包和部署您的应用程序。

    我建议您将您的应用程序打包到一个适当的 WAR 文件中,将其放在 /webapps 或 /WebContent 文件夹中,然后启动 Tomcat。

    如果包名为 spring-mvc-example.war 或 [any-name].war,您的 URL 将为:

    http://localhost:8080/spring-mvc-example/

    【讨论】:

    猜你喜欢
    • 2023-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-13
    • 2015-11-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多