测试Spring MVC时继续遇到404错误

时间:2014-07-07 01:34:19

标签: java spring spring-mvc servlets

我使用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";
    }
}

1 个答案:

答案 0 :(得分:4)

您尚未使用

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

这对我来说似乎不对。