• 社区居民
  • 忠实会员
  • 原创写手

Spring basic MVC app not working

更多 发布于:2016-05-12 14:42
I tried Spring MVC app as described in the book "Spring in Action 3rd Edition". Unfortunately, till now, I am not able to run the simplest app after wasting 6 hrs. I am always getting the "HTTP Status 404" error. I found many threads on this forum for "@RequestMapping not working in Spring" and now wondering whether I am following the right book or is there something very basic which has troubled so many fellow Spring learners. I tried running the MVC app in the book itself (Ch-7, spitter-web) but again, that app also didn't work. Looks like the author has not paid great attention in trying his own apps. For the sake of completeness, my web.xml is: <?xml version="1.0" encoding="UTF-8" ?>

<web-app xmlns=""

    <description>locations of the Spring configuration files</description>





 My application-servlet.xml is: <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""

   <!--<start id="spring_component_scan" />-->
   <context:component-scan base-package="com.truality.demo.web.controller" />
   <!--<end id="spring_component_scan" />-->

   <!--<start id="mvc_annotatedcontrollers" />-->
   <!--<end id="mvc_annotatedcontrollers" />-->

   <context:annotation-config />

   <!--<start id="mvc_resources"/>-->
   <mvc:resources mapping="/resources/**" location="/resources/" />
   <!--<end id="mvc_resources"/>-->

    <bean id="viewResolver"
        <property name="prefix">
        <property name="suffix">

 And my Controller class is: package com.truality.demo.web.controller; import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

public class HomeController {

    @RequestMapping(value={"/home.htm"}, method=RequestMethod.GET)
      public String showHomePage(Map<String, String> model) {
        return "home";
 I am getting the 404 error always when I try accessing - http://localhost:8080/demo-prop-1-1/home.htm OR
- http://localhost:8080/demo-prop-1-1/home.jsp
- http://localhost:8080/demo-prop-1-1/home
- http://localhost:8080/demo-prop-1-1
 I am down to the point where I will start evaluating some other technology like PHP or ... if I could not solve this trivial issue. Please help. Also, Can someone point me to good Spring book which give working examples so that some one can learn it fast? Thanks a lot in advance.You have this <servlet-mapping>

 It makes no sense for your DispatcherServlet, named application, to handle *.jsp requests. Spring, by default, and you, through configuration, have not provided a handler for JSP resources. Get rid of that servlet-mapping. You are then left with <servlet-mapping>
 where /* is a path mapping that matches everything. This means that, even though you removed the other <servlet-mapping>, this mapping will still be used to handle requests to any paths ending with .jsp (among others). This means that the resource resolved by your registered InternalResourceViewResolver, that is /WEB-INF/views/home.jsp, will also be handled by Spring's DispatcherServlet. But again, it does not have a handler for such a path. Instead, you need to let a Servlet container's JSP Servlet handle the request. You can do this by mapping your DispatcherServlet to / <servlet-mapping>
 which marks it as the default servlet. A default servlet will handle any requests that weren't matched by any other <servlet-mapping>. Most Servlet containers have a JSP Servlet mapped to *.jsp, so any requests for a resource with the extension *.jsp will be handled by that before your Spring DispatcherServlet even gets a shot at it. More details here: Spring: servlet-mapping -> url-pattern : /* working but can't display










喜欢0 评分0