Search code examples
javaspringmodel-view-controllerwebsencha-touch-2

"no mapping found" error java spring MVC with no xml configuration


i'm new in Spring + MVC. i've found a script and i could run some part of this script. this script configuring spring mvc with no xml, inside java side. i put all the jars into WEB-INF/lib.

ControllerConfiguration .java

package org.java.springmvc.bootstrap;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "org.java.springmvc.controller")
public class ControllerConfiguration {

    @Bean
    public InternalResourceViewResolver configureInternalResourceViewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/views/");
        resolver.setSuffix(".jsp");
        return resolver;
    }

}

WebAppInitializer.java

package org.java.springmvc.bootstrap;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration.Dynamic;

import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

public class WebAppInitializer implements WebApplicationInitializer {

    @Override
    public void onStartup(final ServletContext servletContext) throws ServletException {
        final AnnotationConfigWebApplicationContext root = new AnnotationConfigWebApplicationContext();
        root.setServletContext(servletContext);
        root.scan("org.java.springmvc.bootstrap");
        root.refresh();

        final Dynamic servlet = servletContext.addServlet("spring", new DispatcherServlet(root));
        servlet.setLoadOnStartup(1);
        servlet.addMapping("/*");
    }

}

HomeController.java

package org.java.springmvc.controller;

import java.io.IOException;
import java.io.Writer;

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

@Controller
public class HomeController {

    @RequestMapping(value = "/")
    public void home(final Writer writer) 
            throws IOException  {
        writer.append("<h2>Welcome, XML Free Spring MVC!</h2>");
    }

    @RequestMapping(value = "/giris")
    public void giris(final Writer writer) 
            throws IOException  {
            writer.append("Giris");
    } 

}

FilmController.java

package org.java.springmvc.controller;

import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.java.springmvc.model.Film;
import org.java.springmvc.model.Film.FilmTurleri;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/film")
public class FilmController {

    @RequestMapping(value = "filmler")
    public void filmler(final Writer writer) 
            throws IOException  {
        writer.append("<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-9\"><title>...Filmler...</title>");
        writer.append("<script type=\"text/javascript\" src=\"/js/touch/sencha-touch-all.js\"></script>");
        writer.append("<script type=\"text/javascript\" src=\"/js/film/filmler.js\"></script>");
        writer.append("</head><body></body></html>");

    } 

    @RequestMapping (value = "/filmleriGetir", method = RequestMethod.GET)
    public @ResponseBody Map<String, List<Film>> FilmleriGetir() {
        List<Film> movies = new ArrayList<Film>();

        // For testing...
        movies.add(new Film(0, "Birinci Film", "Birinci Yönetmen", 2015, FilmTurleri.Aksiyon));
        movies.add(new Film(0, "İkinci Film", "İkinci Yönetmen", 2015, FilmTurleri.Komedi));
        movies.add(new Film(0, "Üçüncü Film", "Üçüncü Yönetmen", 2015, FilmTurleri.Aile));

        Map<String, List<Film>> resp = new HashMap<String, List<Film>>();
        resp.put("filmListesi", movies);
        return resp;
    }

}

Film.java

package org.java.springmvc.model;

public class Film {

    public int Id;
    public String FilmAdi, Yonetmen;
    public int CikisTarihi;
    public FilmTurleri Turu;

    public enum FilmTurleri {
        Aksiyon, Komedi, Aile, Korku, Savas;
    }

    public Film(){

    }

    public Film(int id, String title, String director, int yearOfRelease, FilmTurleri tur)
    {
        super();
        this.Id = id;
        this.FilmAdi = title;
        this.Yonetmen = director;
        this.CikisTarihi = yearOfRelease;
        this.Turu = tur;
    }
    //getter, settings method
}

i have two questions:

  1. if i write "http://localhost:8080/SpringMVC/", the page displays. But if i write "http://localhost:8080/SpringMVC/movies/index" i get this warning:

"WARNING: No mapping found for HTTP request with URI [/SpringMVC/WEB-INF/views/index.jsp] in DispatcherServlet with name 'spring'"

  1. if i add a JSP page(Giris.jsp) under WebContent, i cannot display this page. must all page has a mapping? how can i display simple jsp page?

WARNING: No mapping found for HTTP request with URI [/SpringMVC/Giris.jsp] in DispatcherServlet with name 'spring'

EDIT: i changed a little. My project structure like this:

enter image description here

i get this error:

Failed to load resource:

http://localhost:8080/js/film/filmler.js

http://localhost:8080/js/touch/sencha-touch-all.js

i thought a logic like that:

- there will be a jsp file including "*.js" files. (filmler.jsp)

- there are some methods returning json object in those *.js files. (FilmleriGetir method)

any advice for this logic?

Regards.


Solution

    1. In MovieController.java, you need to add '/' :

    @RequestMapping("/movies")

    1. You are using servlet.addMapping("/*"); which means your org.springframework.web.servlet.DispatcherServlet i.e Spring will intercept every request that comes to your application. Now, you don't have any RequestMapping for Giris.jsp in any controller, so Spring is throwing error as: No mapping found for HTTP request with URI [/SpringMVC/Giris.jsp]

    In order to show Giris.jsp page, your need to:

    A] Add entry in/ make new controller with RequestMapping for 'Giris.jsp', and set view as 'Giris'

    eg:

       @Controller
    public class MyController {
    
        @RequestMapping(value = "/Giris.jsp")
        public void home(final Writer writer) 
                throws IOException  {
                return 'Giris';
        }    
    }
    

    You would be better of using RequestMapping as /giris instead of /Giris.jsp, as it exposes that underlying technology is JSP.

    B] place Giris.jsp file under /WEB-INF/views/ folder.

    Understand how InternalResourceViewResolver works. Taking reference of your ControllerConfiguration, When a view name is returned for controller as 'Giris', InternalResourceViewResolver adds prefix and suffix as defined by you, and that page is shown. So, in case of view name 'Giris', page '/WEB-INF/views/'+ 'Giris' + '.jsp' will be rendered.


    • According to java naming convention, JSP (file) name should always begin with a lower-case letter. So use giris.jsp instead of Giris.jsp


    EDIT(For modified question):

    Failed to load resource: http://localhost:8080/js/film/filmler.js

    Understand that, as DispatcherServlet is mapped to /*, every request that comes to your web-app, is handled by DispatcherServlet i.e Spring.

    Whenever you application comes across url http://localhost:8080/js/film/filmler.js, it knows that DispatcherServlet will handle that url. DispatcherServlet checks if there is any RequestMapping for the url(in controller).

    Now, when you add url

    http://localhost:8080/js/film/filmler.js

    there is no RequestMapping that would handle such kind of url, so you are getting a url.

    For loading resources such as js files or image files, use mvc:resources.

    eg: For js files: Put all your js files in directory /WEB-INF/js/.

    Add mvc:resource mapping for js files in you configuration:

     <mvc:resources mapping="/js/**" location="/WEB-INF/js/" />
    

    Now you you will be able to access your js files. If Spring comes across url such as /js/film/filmler.js, it will know know from mvc:resource mapping, for where to look for that file.

    Goof mvc:resource for tutorials.