#4 Working with Spring MVC

MVC stands for Model-View-Controller. MVC in spring required:

  1. Some JSP pages ( views) which need to be displayed.
  2. Controller which used to access views
  3. Model to set for views

So lets start with MVC. Before starting I hope you have idea of basic REST api development. If not then you can follow steps here.

For MVC, we need to follow steps below:

1. Create a ModelController class. Add annotation @Controller on top ( with @RestController it will not work).



2. Create JSP page, let say index.jsp. Please check hierarchy in folder structure for index.jsp page as we will require to tell our application that from where JSP pages need to pick.


3. Create HelloConfig.java to register InternalResourceViewResolver
InternalResourceViewResolver is used at application level to tell application path of JSP/CSS files.



I have set path to "/WEB-INF/pages" and added suffix as .jsp. So now I won't require to specify .jsp explicity. To access jsp page now I can simply write name of page.

4. Add Apache dependency in pom.xml to display JSP pages.

5. Finally write method in controller class:


In Spring boot we can simply pass jsp page name to display. 

Before Spring boot app, we were using ModelAndView object to return JSP page.

So lets try to call api after starting server:


So finally we are able to display our jsp page.

Now lets move to next step here. We will be learning how to pass arguments in ModelAndView from one page to another page.






 

Comments

Popular posts from this blog

#2 Simple Spring Rest Api with JDBC connection

#1 My First Rest API With Spring Boot Freamwork

Swift Initialization