1

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?

vzateychuk
  • 301
  • 1
  • 3
  • 12
  • Did you send a request to `/WEB-INF/views/welcome.tmpl` endpoint? You should send the request to the `/welcome` endpoint – Ali Dehghani Aug 15 '16 at 19:27
  • Ali, I'm trying to request endpoint: http://localhost:8080/welcome – vzateychuk Aug 15 '16 at 19:41
  • check you scanning the right package in @ComponentScan – Vaibs Aug 16 '16 at 06:06
  • the scanning package is controllers, so there is not a actual clue – vzateychuk Aug 16 '16 at 10:21
  • What is your project structure? Can you provide the location of your welcome.jsp file? – marians27 Aug 16 '16 at 10:42
  • There is application structure (I've trying to fix and renamed little bit): http://prnt.sc/c6f7sv . But now I guess the problem might be in the IDEA settings. Since I'm using the IDEA Community Version, probably there is not settings in IDEA to explain to DispatcherServlet to explain where the WEB-INF located. So, DispatcherServlet just unable to find out where the View "hello.html" located. – vzateychuk Aug 16 '16 at 13:49
  • Finilly it works, thank you all colleagues who help me to work it out. – vzateychuk Aug 17 '16 at 21:28

2 Answers2

10

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.

Community
  • 1
  • 1
marians27
  • 311
  • 1
  • 7
3

configure resolver configuration application.properties

spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp

spring boot will automatically configure your internal view resolver.

and you need to add jstl jars in pom

    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
    </dependency>

or

to get your view resolver to work add

@Override
public void configureDefaultServletHandling(
        DefaultServletHandlerConfigurer configurer) {
    configurer.enable();
}

spring-boot has a sample for you

kuhajeyan
  • 10,727
  • 10
  • 46
  • 71
  • Thank you, kuhajeyan, it finally works! But there is one thing that I was not expected: only *.jsp possible (not *.html or others) for VIews. – vzateychuk Aug 17 '16 at 21:27