Saturday, June 22, 2019

Basic spring boot setup and running simple hello world page in Intellij Community Edition Spring boot MVC

1) Install Spring Assistant Plugin for intellij.
2) Create Web project  with Spring Web Starter.
3) add dependency in pom file:
     <dependency>
   <groupId>org.apache.tomcat.embed</groupId>
   <artifactId>tomcat-embed-jasper</artifactId>
   <scope>compile</scope>
</dependency>
<dependency>
   <groupId>javax.servlet</groupId>
   <artifactId>jstl</artifactId>
</dependency>

4) add following in application.properties file in src/main/resources
       spring.mvc.view.prefix: /WEB-INF/jsp/
       spring.mvc.view.suffix: .jsp

5) Create controller class named SpringJava4sController.java


       import org.springframework.stereotype.Controller;
       import org.springframework.ui.Model;
       import org.springframework.web.bind.annotation.*;
       import org.springframework.web.servlet.ModelAndView;

       @RestController       public class SpringJava4sController {

      @RequestMapping(value="/test", method=RequestMethod.GET)
       public ModelAndView showLoginPage(Model model) {
       model.addAttribute("message", "Welcome to Java4s Spring Boot Tutorials");

       return new ModelAndView("welcomePage");
       }
     }


6) Make sure we have class with main as 
     import org.springframework.boot.SpringApplication;
     import org.springframework.boot.autoconfigure.SpringBootApplication;

     @SpringBootApplication     public class DemoApplication {

     public static void main(String[] args) {
      SpringApplication.run(DemoApplication.class, args);
    }


7) create webapp/WEB-INF/jsp folder within src/main
8) place jsp file welcomePage inside jsp as:

    <html  xmlns:th="http://www.thymeleaf.org">
     <body>

   
    ${message}

   
    </body>
   </html>


9) go to browsrer and see localhost:8080/test


 

No comments:

Post a Comment