TonyW
TonyW

Reputation: 18895

Keep getting 404 error when testing Spring MVC

I use IntelliJ to generate a Spring MVC app and am using the Spring 4.0.5.RELEASE for this learning. I followed a book tutorial, and think I've configured everything as the book says, but I keep getting the frustrating 404 error. I am posting my src/main/webapp/WEB-INF/web.xml content as below:

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

my mvc-dispatcher-servlet.xml content is:

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

and my applicationContext.xml is almost empty:

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

My controller is:

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

Upvotes: 0

Views: 1235

Answers (1)

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 280174

You haven't enabled your MVC stack in mvc-dispatcher-servlet.xml with

<mvc:annotation-driven />

so none of your @Controller handler methods are registered.

Remember to add the appropriate mvc namespace declarations.

Note also that with your current InternalResourceViewResolver, which has

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

and the view name returned by your handler method

return "pages/hello";

the resource will be look for in

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

That seems wrong to me.

Upvotes: 4

Related Questions