-3

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="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"
    version="2.4">

<context-param>
    <description>locations of the Spring configuration files</description>
    <param-name>contextConfigLocation</param-name>
    <param-value>
    </param-value>
</context-param>

<servlet>
    <servlet-name>application</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>application</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

<servlet-mapping>
    <servlet-name>application</servlet-name>
    <url-pattern>*.jsp</url-pattern>
</servlet-mapping>

<listener>
    <listener-class>
        org.springframework.web.context.ContextLoaderListener
    </listener-class>
</listener>


</web-app>

My application-servlet.xml is:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:util="http://www.springframework.org/schema/util"
 xmlns:mvc="http://www.springframework.org/schema/mvc"
 xmlns:p="http://www.springframework.org/schema/p"
 xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

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

   <!--<start id="mvc_annotatedcontrollers" />-->
   <mvc:annotation-driven/>
   <!--<end id="mvc_annotatedcontrollers" />-->

   <context:annotation-config />

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

    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/views/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>


 </beans>

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;

@Controller
public class HomeController {

    @RequestMapping(value={"/home.htm"}, method=RequestMethod.GET)
      public String showHomePage(Map<String, String> model) {
        model.put("name","Google");
        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.

AAgg
  • 494
  • 1
  • 3
  • 19
  • 1
    It's not a Spring problem. You aren't deploying the web app properly. Sounds like you need Java web app 101. Not Spring's fault. And go ahead and investigate PHP or other technologies. No Spring developer will care what you choose. Have a look at this: http://www.srccodes.com/p/article/24/spring-3-mvc-framework-based-hello-world-web-application-example-using-maven-eclipse-ide-and-tomcat-server – duffymo Sep 01 '14 at 18:31
  • https://spring.io/guides/gs/spring-boot/ https://www.youtube.com/watch?v=p8AdyMlpmPk – Vaelyr Sep 01 '14 at 19:02
  • Is asking a question a bad thing here? Do you think I have not tried enough? Things are not straight forward and easy hence new comers like me face challenges. And when authors provide untested code, what can you do? I expect more help from this forum and not negative votes on my questions. You can see several others have faced issues here and this is not that straight forward. – AAgg Sep 01 '14 at 19:32
  • Asking a question is not a bad thing here. Someone might think you have not tried enough. You should not have expectations without basis. You should read the Help Center and understand exactly what is expected of _you_ and what type of questions to ask about and how. **Asking for off site resources is off-topic.** The last three paragraphs of your question are pointless and don't add anything of value. You can remove them. When you do, your question reads like a debug request where you haven't shown any attempts to solve the problem. Do you understand why someone might choose to downvote you? – Sotirios Delimanolis Sep 02 '14 at 02:44
  • I agree with comments above, this forum is for legit questions not essentially threatening to leave just because a framework is not cooperating, no one really cares you are on your own in the end. If you finding 6 hrs frustrating, then maybe you should walk away now because in this industry you can encounter problems that will take weeks to identify. Lastly i seriously doubt 6 hrs have been invested here, because that would mean you have thoroughly investigated your server setup alongside your application setup. What is the application context your server is deploying the war file? – Aeseir Sep 02 '14 at 03:04

3 Answers3

1

You have this

<servlet-mapping>
    <servlet-name>application</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

<servlet-mapping>
    <servlet-name>application</servlet-name>
    <url-pattern>*.jsp</url-pattern>
</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>
    <servlet-name>application</servlet-name>
    <url-pattern>/*</url-pattern>
</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>
    <servlet-name>application</servlet-name>
    <url-pattern>/</url-pattern>
</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

Community
  • 1
  • 1
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
1

Finally, I found that my war file didn't contain my controller's class due to which the request was not getting mapped properly. Reason of this was that I was not following directory structure as per Gradle war plugin's expectations.

I fixed the XML files as per the suggestions; all the changes I made to them didn't bear any results since problem was lying elsewhere.

Although, it was a problem on my part, it would have been nice on Gradle's part to give at least a warning or remark that my java files were not getting compiled. Is there a way to enable this in Gradle?

Thanks for your help.

AAgg
  • 494
  • 1
  • 3
  • 19
0

@Sotirios Delimanolis Thanks for clarification.

If I make servlet mapping as

<servlet-mapping>
    <servlet-name>application</servlet-name>
    <url-pattern>*.jsp</url-pattern>
</servlet-mapping>

and make RequestMapping as

@RequestMapping(value={"/home.jsp"}, method=RequestMethod.GET)

then it would be handled by spring DispatcherServlet and call to http:///demo-prop/home.jsp would succeed

Dipika
  • 1
  • 1