【问题标题】:Spring MVC controller problem, HTTP Status 404Spring MVC 控制器问题,HTTP 状态 404
【发布时间】:2020-03-27 14:43:22
【问题描述】:

我正在学习 Spring MVC,但在运行我的 Web 应用程序时遇到了很大的麻烦。我的控制器不工作,所以我无法映射我的请求。这是我的错误:

HTTP 状态 404 - 未找到

类型状态报告

描述 源服务器没有找到目标资源的当前表示或不愿意透露存在。

在浏览器中输入同样的错误:

http://localhost:8081/showForm

http://localhost:8081/mvcapp/showForm

http://localhost:8081/processForm

我的控制器:

@Controller

    public class HelloWorldController {


        @RequestMapping("/showForm")
        public String showForm() {
            return "helloworld-form";
        }

        @RequestMapping("/processForm")
        public String processForm() {
            return "helloworld";
        }
    }

调度程序-servlet

<?xml version="1.0" encoding="UTF-8"?>
<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:mvc="http://www.springframework.org/schema/cache"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">


    <mvc:annotation-driven/>
    <context:component-scan base-package="mvcapp"/>




    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">

        <property name="prefix" value="WEB-INF/view/"></property>
        <property name="suffix" value=".jsp"></property>

    </bean>

</beans>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">

    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>


        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>WEB-INF/dispatcher-servlet.xml</param-value>
        </init-param>

        <load-on-startup>1</load-on-startup>

    </servlet>

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

文件树图:https://i.imgur.com/5cjfsBc.png

【问题讨论】:

  • 使用 Spring Boot,而不是旧配置。这将解决这个问题(这可能是你的模式不是/**)。
  • 我看到您使用的是非标准端口8081。有没有配置tomcat听8081? (默认为 8080)
  • 我只更改了 intellij 配置中的 8080 的 http 端口(编辑配置... >> Tomcat 服务器设置),因为我有错误“地址 localhost:8080 已在使用中”

标签: java spring spring-mvc


【解决方案1】:

在您的 bean-configuration dispatcher-servlet.xml 中, 尝试将 InternalResourceViewResolver bean 的名为 prefix 的属性更正为以 / 开头的相对路径:

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">

        <property name="prefix" value="/WEB-INF/view/"></property>
        <property name="suffix" value=".jsp"></property>

    </bean>

或者写成:

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

参见 mkYong (2010):Spring MVC InternalResourceViewResolver example

【讨论】:

    【解决方案2】:

    当你访问http://localhost:8081/showForm时,是否可以进入方法,你可以在return "helloworld-form";前加上System.out.println("showForm)来定位问题、dispather问题或jsp访问问题

    【讨论】:

      【解决方案3】:

      我扩展了 tomcat 输出,从内容中添加了 WEB-INF/classes/logging.properties:

      org.apache.catalina.core.ContainerBase.[Catalina].level=INFO

      org.apache.catalina.core.ContainerBase.

      [Catalina].handlers=java.util.logging.ConsoleHandler

      我发现了这个异常:

      2019 年 12 月 3 日 17:25:41.190 信息 [RMI TCP 连接 (3)-127.0.0.1] org.apache.catalina.core.ApplicationContext.log 标记 servlet [调度员] 不可用 2019 年 12 月 3 日 17:25:41.191 严重 [RMI TCP 连接(3)-127.0.0.1] org.apache.catalina.core.StandardContext.loadOnStartup Servlet Web 应用程序 [] 中的 [调度程序] 引发 load() 异常 java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet

      (...)

      所以,问题在于缺少调度程序类和其他库。

      我解决了这个问题,将所有 spring 库从 maven 添加到 libs 目录。 https://stackoverflow.com/a/24586000/12296902 非常感谢回复

      【讨论】:

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