2

I am getting an warnning in springmvc as No mapping found for HTTP request with URI [/FitnessTracker/] in DispatcherServlet with name 'fitTrackerServlet'

INFO: FrameworkServlet 'fitTrackerServlet': initialization completed in 2194 ms
Feb 26, 2017 9:43:08 AM org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/FitnessTracker/] in DispatcherServlet with name 'fitTrackerServlet'

When i press url http://localhost:8080/fitnessTracker/ I am getting 404 error. Thanks

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
  <servlet>
    <servlet-name>fitTrackerServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/config/servlet-config.xml</param-value>
    </init-param>
  </servlet>
 <servlet-mapping>
   <servlet-name>fitTrackerServlet</servlet-name>
   <url-pattern>/*</url-pattern>
</servlet-mapping>
  <display-name>Archetype Created Web Application</display-name>
</web-app>

and my servlet-config.xml

<?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: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.2.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">

  <mvc:annotation-driven />
  <context:component-scan base-package="com.pluralsight.controller"></context:component-scan>

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

  <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
  p:prefix="/WEB-INF/jsp/" p:suffix=".jsp"/>

</beans>

and my controller

@Controller
public class HelloController {

    @RequestMapping(value = "/greeting")
    public String sayHello (Model model) {
        System.out.println("Test");

        model.addAttribute("greeting", "Hello WorldX");
        return "hello"; 
    }
}
pankaj12
  • 183
  • 2
  • 3
  • 9
  • My recommendation is to use Spring-boot stack which is popular and going to be the future where you can avoid the traditional web.xml configurations. Please follow this link to get started. http://stackoverflow.com/questions/39245732/java-lang-noclassdeffounderror-org-springframework-core-env-configurableenviron/39246493#39246493 – Praveen Kumar K S Feb 26 '17 at 03:54
  • @PraveenKumarKrishnaiyer Thanks for suggesting do you have this solution. – pankaj12 Feb 26 '17 at 04:03
  • try http://localhost:8080/greeting/ – Monzurul Shimul Feb 26 '17 at 05:42
  • Using spring boot doesn't teach people the configuration that is going on in the application. What is your package in your controller? – bh5k Mar 12 '17 at 17:27
  • Add following lines to each JSP -> <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <%@ page isELIgnored="false" %> – Iroshan Aug 05 '20 at 04:05

3 Answers3

0

Does your component-scan base-package match the package where your HelloController is in? If not, that is the problem.

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

Configures component scanning directives for use with @Configuration classes. Either basePackageClasses() or basePackages() (or its alias value()) may be specified to define specific packages to scan.

Spring does not find your HelloController and so doesn't map anything.

Otherwise you could also follow these steps.

Yoshua Nahar
  • 1,304
  • 11
  • 28
0

I am wondering why you have you application configured with XML. With this tool you can start a maven project with a Spring boot application that will be, by default, already configured to build exactly what you want.

Then you only need a class with:

@RestController
@RequestMapping("/fitnessTracker")
public class HelloController {

    @RequestMapping(value = "/greeting")
    public String sayHello (Model model) {
        System.out.println("Test");

        model.addAttribute("greeting", "Hello WorldX");
        return "hello"; 
    }
}

And that's it, Spring will do the rest for you ;)

  • Because just using Spring Boot doesn't teach people what is going on under the hood. You don't learn the configuration of what is happening with the application if you just use their kick starter. – bh5k Mar 12 '17 at 17:25
0

In my case the problem was that i was using this url-pattern

<url-pattern>/*</url-pattern>

instead of this

<url-pattern>/</url-pattern>

and also i had to add

@RequestMapping(value = "/fitnessTracker")

under the

@Controller

annotation.

I found the solution at : https://www.baeldung.com/spring-mvc-404-error for the pattern.

Lnoldori
  • 1
  • 2