0

I am stuck at this point, unable to find the exact of reason.However,I found many similar question but still my issue doesn't seems to be resolved.I am running it in sts 4 and added a tomcat server 9.0 to run it locally.Most near question which I found is (org.springframework.web.servlet.PageNotFound noHandlerFound No mapping found for HTTP request with URI) but still it doesn't seems to be working for me.Below is my code.

web.xml

<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="3.0">
  <display-name>My Demo App</display-name>
  <servlet>
    <servlet-name>MyDemoApp</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/config/myDemoApp-servletConfig.xml</param-value>
    </init-param>
  </servlet>

  <servlet-mapping>
    <servlet-name>MyDemoApp</servlet-name>
    <url-pattern>*.html</url-pattern>
  </servlet-mapping>
</web-app>

***myDemoApp-servletConfig.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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    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.1.xsd
                    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd     
                    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
                    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
 <mvc:annotation-driven/>

 <context:component-scan base-package="com.demo.controllers.*"/>
 <bean  class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix ="WEB-INF/jsp/" p:suffix =".jsp"/>

</beans>


Controller



package com.demo.controllers

import org.springframework.ui.Model;
import org.springframework.stereotype.*;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class MyDemoController {

    private String[] quotes= {"To be or not to be - Shakespeare",
                              "Spring in nature's way of syaing Let's Party",
                              "The time is always right to do what is right"}


    @RequestMapping(value="/getQuote.html")
    public String getRandomQuote(Model model) {



        int rand=new Random().nextInt(quotes.length);
        String randomQuote=quotes[rand];
        model.addAttribute("randomQuote",randomQuote);
        return "quote";
    }

}


  [1]: https://i.stack.imgur.com/Z5SN3.jpg
Pratyush Mayank
  • 173
  • 1
  • 1
  • 10

1 Answers1

0

So, actually I didn't work in Eclipse, but do you have two src folders? If it so, you need hold all classes and resources under the one src folders, something like this:

-src 
   -main  
      -java (Your controllers)
          -other packages under java folder
      -resources (spring configuration file)
      -webapp (jsp, web.xml etc.)

In addition to this, I guess line is incorrect

<context:component-scan base-package="com.demo.controllers.*"/>

Maybe it's been allowed in earlier versions of spring. But if you will see official documentation, I didn't see using version with '*' symbol. And I tried to use this locally, it didn't work for me. So use com.demo.controllers

Anthony
  • 571
  • 3
  • 11