No mapping found for HTTP request with URI [/WEB-INF/views/welcome.jsp] in DispatcherServlet with name 'dispatcherServlet'

vzateychuk picture vzateychuk · Aug 15, 2016 · Viewed 9.2k times · Source

I configured the Application and code the "DispatcherServlet" to viewResolver like this:

@Configuration
@EnableWebMvc
@ComponentScan ({"controllers"})
@EnableAutoConfiguration
@SpringBootApplication
public class Application {

@Bean
public InternalResourceViewResolver viewResolver() {
    InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
    viewResolver.setViewClass(JstlView.class);
    viewResolver.setPrefix("/WEB-INF/views/");
    viewResolver.setSuffix(".jsp");
    return viewResolver;
}
public static void main(String[] args){
        SpringApplication.run(Application.class, args);
}
}

Controller class to handle requests looks this:

@Controller
public class HelloControllerImpl {

@RequestMapping(value= "/welcome", method= RequestMethod.GET)
public String getWelcomePage(ModelMap model) {
    model.addAttribute("message", "Spring 3 MVC - Hello World");
    model.addAttribute("name", "vzateychuk");
    return "welcome";
}
}   

The view file: \WEB-INF\views\welcome.jsp

<html>
<body>
    <h1>Hello, : ${name}</h1>
    <h2>Message : ${message}</h2>
</body>
</html>

The application structure: Welcome application structure

I think that something is missing in the configuration files, but I can not see. Could you guest what is wrong and what means: "No mapping found for HTTP request with URI [/WEB-INF/views/welcome.jsp]"? Should I provide xml configuratin like dispatcher-servlet.xml or something like that? Thank you in advance.

Update: I guessing that my DispatcherServlet unable to find the appropriate view. I have tryed to completely delete /WEB-INF directory, but nothing changes. Probably something wrong with this code:

    public InternalResourceViewResolver viewResolver() {
    InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
    viewResolver.setViewClass(JstlView.class);
    **viewResolver.setPrefix("/WEB-INF/views/");**

.... Can anybody guess what can be wrong? (May it be if the anotation @EnableAutoConfiguration not allow to define viewResolver's prefix?

Answer

marians27 picture marians27 · Aug 16, 2016

I did simple project similiar to yours. You can check on my github

What you have to do is:

  1. Rename hello.html to hello.jsp
  2. Check that you have all dependencies in your pom.xml. I didn't see it so I am not sure if it is wrong. Make sure you have these two dependencies:
    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <scope>provided</scope>
    </dependency>
    
    For that point you will find the explanation here
  3. Indeed, you may have a problem with launching it with IDEA Community Version. I have experienced that problem as well. What you can do is first check it using command line and maven. Execute following command:
    mvn spring-boot:run
    You may also configure your IDEA to run that command. Go to Run->Edit Configuration, click green plus sign on the left side and choose Maven. Then in the "Command line" field write "spring-boot:run", press ok. And run this configuration.
  4. (Optional) You also have some redundant annotations on your Application class. You can remove:
    • @Configuration, because @SpringBootApplication already has it
    • @EnableWebMvc, because Spring Boot adds it automatically when it sees spring-webmvc on the classpath
    • @EnableAutoConfiguration, because @SpringBootApplication already has it

Note that you need @ComponentScan ({"controllers"}), because of your package structure - you have Application class in different package than your controller.