【问题标题】:Keep getting 404 error when testing Spring MVC测试 Spring MVC 时不断收到 404 错误
【发布时间】:2014-07-07 01:34:19
【问题描述】:

我使用 IntelliJ 生成 Spring MVC 应用程序,并使用 Spring 4.0.5.RELEASE 进行学习。我按照书本教程进行操作,并认为我已经按照书中的说明配置了所有内容,但我不断收到令人沮丧的 404 错误。我将我的 src/main/webapp/WEB-INF/web.xml 内容发布如下:

<web-app version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <display-name>SpringMVCTest</display-name>


    <!--context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext-security.xml</param-value>
    </context-param>
    <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping-->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

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

    <welcome-file-list>
        <welcome-file>pages/hello.jsp</welcome-file>
    </welcome-file-list>
</web-app>

我的 mvc-dispatcher-servlet.xml 内容是:

<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" xmlns:mvc="http://www.springframework.org/schema/mvc"
       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 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <context:component-scan base-package="com.rcholic.news"/>
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/"/>
        <property name="suffix" value=".jsp"/>

    </bean>

</beans>

而我的 applicationContext.xml 几乎是空的:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       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.xsd">

</beans>

我的控制器是:

@Controller
public class HomeController {
    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public String printWelcome(ModelMap model) {
        model.addAttribute("message", "Hello world!");
        return "pages/hello";
    }
}

【问题讨论】:

  • 发布您的确切 HTTP 请求和服务器日志,并注意您的 XML 使用的是过时版本的命名空间。
  • 您能否进入您的项目设置,看看您是如何部署您的项目并将其粘贴进去。我刚开始时也遇到过类似的问题,而且它们恰好是一些愚蠢的小问题。

标签: java spring spring-mvc servlets


【解决方案1】:

您尚未在 mvc-dispatcher-servlet.xml 中启用您的 MVC 堆栈

<mvc:annotation-driven />

所以您的 @Controller 处理程序方法都没有注册。

记得添加适当的mvc 命名空间声明。

另请注意,您当前的 InternalResourceViewResolver 具有

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

以及您的处理程序方法返回的视图名称

return "pages/hello";

资源将在中查找

/WEB-INF/pages/pages/hello.jsp

这对我来说似乎是错误的。

【讨论】:

    猜你喜欢
    • 2017-09-06
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    • 2019-07-18
    • 2012-08-03
    • 1970-01-01
    • 1970-01-01
    • 2018-07-28
    相关资源
    最近更新 更多