简体   繁体   中英

Why am I getting a 404 error with my Spring MVC project?

I have very basic setup for a spring mvc web application. Here is my web.xml mapping below:

<servlet>
    <servlet-name>test</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:spring-config.xml</param-value>
    </init-param>
</servlet>
<servlet-mapping>
    <servlet-name>test</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

and spring-config.xml

<mvc:default-servlet-handler/>

<mvc:annotation-driven />

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

<context:annotation-config/>

and this is my directory structure for the jsp pages

`-- web
    `-- WEB-INF
        |-- pages
        |   |-- css
        |   |   |-- foundation.css
        |   |   `-- normalize.css
        |   |-- images
        |   |-- index.jsp
        |   `-- js
        `-- web.xml

and my controller is declared below:

@Controller
public class TestController {


    @RequestMapping(value = "/")
    public String 
        return "index";
    }
}

However, when i try to access localhost:8080/app/ I got 404 error. In the log I have 2013-10-19 16:12:09,326 () DEBUG DispatcherServlet - Successfully completed request

and if i debug the app everything seems ok, controller is called, view is rendered, but i am still getting 404. What do you think?

Forget a xml-based configurations for Spring projects. This approach is really obsolete. Here you can find a well explained step by step tutorial for simple Spring annotation based application.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM