0

I found similar questions asked here but I tried those answers and here I am after spending a day. I dont know what is wrong here I am stuck with my first spring mvc application. If anyone can help me then that will be highly appreciated. I am getting error 404- Not found. Console error - "No mapping found for HTTP request with URI [/FirstSpringMVC/welcome] in DispatcherServlet with name 'mvc-dispatcher'"

Here is my entire code.

pom.xml

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.thomsontech</groupId>
  <artifactId>FirstSpringMVC</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>FirstSpringMVC Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.3.8.RELEASE</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
        <version>4.3.8.RELEASE</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>4.3.8.RELEASE</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>

  </dependencies>
  <build>
    <finalName>FirstSpringMVC</finalName>
  </build>
</project>

web.xml

        <!DOCTYPE web-app PUBLIC
     "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
     "http://java.sun.com/dtd/web-app_2_3.dtd" >

    <web-app>
      <display-name>Archetype Created Web Application</display-name>

      <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
            <servlet-class>
                 org.springframework.web.servlet.DispatcherServlet
            </servlet-class>
      </servlet>
      <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
            <url-pattern>/</url-pattern>
      </servlet-mapping>
    </web-app>

mvc-dispatcher-servlet.xml

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

        <bean id="HandlerMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />

        <context:component-scan base-package="com.thomsontech.controller" />

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

    </beans>

HelloWorldController.java

    package com.thomsontech.controller;

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

    @Controller
    public class HelloWorldController {

        @RequestMapping("/welcome")
        public ModelAndView helloWorld() {

            ModelAndView model = new ModelAndView("HelloWorldPage");
            model.addObject("msg", "hello world");

            return model;
        }
    }

HelloWorldPage.jsp

        <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
    <html>
    <body>
        <h1>Spring MVC Hello World Example</h1>

        <h2>${msg}</h2>
    </body>
    </html>
Thomson Mathew
  • 419
  • 1
  • 9
  • 28

1 Answers1

0

Well, have done below changes to your code to fix the 404 issue.

@Controller
public class HelloWorldController {

    @RequestMapping(value = {"/welcome","/"}, method = RequestMethod.GET)
    public ModelAndView helloWorld() {

        ModelAndView model = new ModelAndView("HelloWorldPage");
        model.addObject("msg", "hello world");

        return model;
    }

}

It is almost same as yours except it answers to the requests received on root page("/"). I've just added it to directly test the homepage.

I don't know the reason, the combination of @Controller and <context:component-scan base-package="com.thomsontech.controller" /> weren't creating the helloWorldController bean. So, I had to explicitly create bean by adding <bean id="helloWorldController" class="com.thomsontech.controller.HelloWorldController"></bean> in mvc-dispatcher-servlet.xml file. I also removed <bean id="HandlerMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/> from the same file as I couldn't see that bean being used anywhere.

So, your final mvc-dispatcher-servlet.xml would be :

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

    <context:component-scan base-package="com.thomsontech.controller" />

    <bean id="helloWorldController" class="com.thomsontech.controller.HelloWorldController"></bean>

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

</beans>

In your web.xml, have added <load-on-startup>1</load-on-startup> to </servlet> tag, so as to load mvc-dispatcher servlet on startup. Also, you will have to upgrade of web-app version 2.3 as you are using jstl. JSTL doesn't works only with 2.4+ versions. However, this shouldn't stop you from testing 404 issue. ${msg} in HelloWorldPage jsp will not render "hello world" string returned by the HelloWorldController controller.

Your final web.xml would be :

<web-app>
    <display-name>Archetype Created Web Application</display-name>
    <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

After making these changes, do a cleanup of tomcat/jetty work directory followed by maven clean install and then run this app on the server.

Hope this helps and good luck!!

harshavmb
  • 3,404
  • 3
  • 21
  • 55
  • First of all thank you very much for helping me. I changed the codes the way you suggested. Now I dont have Mapping error but I am getting " java.lang.ClassNotFoundException: com.thomsontech.controller.HelloWorldController." Where would be possibly wrong ? can you please help me ? – Thomson Mathew Jun 06 '17 at 19:48
  • Okay. Controller class might be missing in tomcat/jetty working directory. Can you check the working directory? Clear this project inside tomcat deployment directory, refresh this project in eclipse workspace, run mvn clean install and again check tomcat deployment directory for classes folder. – harshavmb Jun 07 '17 at 02:33