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

我的 Spring Web 应用程序有问题:它显示 Apache Tomcat/4.0.6 - HTTP Status 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>


长风秋雁
浏览 156回答 2
2回答

斯蒂芬大帝

两个问题:更改<context:component-scan base-package="com.spring" />为&nbsp;<context:component-scan base-package="com.spring.*" />更改<beans:property name="prefix" value="/WEB-INF/views/" />为&nbsp;<beans:property name="prefix" value="/WEB-INF/view/" />&nbsp;(因为您的文件夹被命名为视图)

智慧大石

将 web.xml servlet 映射更改为<servlet-mapping>&nbsp; &nbsp; <servlet-name>spring</servlet-name>&nbsp; &nbsp; <url-pattern>/</url-pattern>&nbsp;</servlet-mapping>然后在您的 contextConfigLocation 文件中/WEB-INF/spring-servlet.xml配置如下:<beans xmlns="http://www.springframework.org/schema/beans"&nbsp; &nbsp; xmlns:context="http://www.springframework.org/schema/context"&nbsp; &nbsp; xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&nbsp; &nbsp; xsi:schemaLocation="&nbsp; &nbsp; &nbsp; &nbsp; http://www.springframework.org/schema/beans&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; http://www.springframework.org/schema/beans/spring-beans-3.0.xsd&nbsp; &nbsp; &nbsp; &nbsp; http://www.springframework.org/schema/context&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; http://www.springframework.org/schema/context/spring-context-3.0.xsd">&nbsp; &nbsp; // to scan for annotation controllers, beans or configurations&nbsp; &nbsp; <context:component-scan base-package="com.spring" />&nbsp; &nbsp; <bean&nbsp; &nbsp; &nbsp; &nbsp; class="org.springframework.web.servlet.view.InternalResourceViewResolver">&nbsp; &nbsp; &nbsp; &nbsp; <property name="prefix">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <value>/WEB-INF/view/</value>&nbsp; &nbsp; &nbsp; &nbsp; </property>&nbsp; &nbsp; &nbsp; &nbsp; <property name="suffix">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <value>.jsp</value>&nbsp; &nbsp; &nbsp; &nbsp; </property>&nbsp; &nbsp; </bean></beans>您没有正确打包和部署您的应用程序。我建议您将应用程序打包到适当的 WAR 文件中,将其放入 /webapps 或 /WebContent 文件夹,然后启动 Tomcat。如果包名为 spring-mvc-example.war 或 [any-name].war,您的 URL 将是:http://localhost:8080/spring-mvc-example/
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java