2

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?

Jonik
  • 80,077
  • 70
  • 264
  • 372
  • You haven't defined a welcome file for your application. – Luiggi Mendoza Oct 19 '13 at 14:21
  • Where do you get 404 when the view is rendered ? – RicoZ Oct 19 '13 at 14:55
  • @o-richie-nal when OP tries to access `http://localhost:8080/app/`. It is stated at the bottom of the question. – Luiggi Mendoza Oct 19 '13 at 14:55
  • @Luiggi I asked because he says the view is rendered – RicoZ Oct 19 '13 at 14:59
  • 1
    By rendered i mean that i see code which is responsible for rendering (when debugging) executed without errors. When i pu @ResponseBody on the controller method i got the result so i gues it is sometthing about the jsp – Spravce Uctu Oct 19 '13 at 15:13
  • What version of Spring is this? HTML 404 error is page not found. I would think that your controller mapping to the jsp isn't setup properly. – James Drinkard Oct 19 '13 at 17:53
  • Curious. I just tried your config and it works for me. What servlet/JSP container are you running this on? (Tangential: a while ago I had a [problem with similar symptoms](http://stackoverflow.com/questions/16383091/how-to-get-a-trivial-case-of-spring-mvc-view-jsp-resolving-to-work), but the fix isn't applicable here (your servlet-mapping is fine already).) – Jonik Oct 19 '13 at 21:37
  • Op, isn't your controller method signature missing some code? I had to add a method name, a pair of parens, and an opening bracket so as not to get a syntax error.. – wSchmidt Oct 19 '13 at 22:54
  • Have you checked that your jsp file is correctly deployed and compiled ? (In case of tomcat you can look into the work directory of your application) – RicoZ Oct 21 '13 at 08:02

1 Answers1

-1

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.

Alex Fruzenshtein
  • 2,846
  • 6
  • 32
  • 53