Archives for 

Spring Framework

Spring MVC Tutorial

    Overview to MVC

MVC or Model View Controller is a controller  is a design pattern . This design pattern involves a Model(manages data and logic ), a view (interacting with user) and a controller(which is in between the model and view and controls  the commands between model and view).Spring MVC is a module in spring framework as we discussed earlier.This chapter gives a very simple Spring MVC Tutorial . We are discussing a simple Spring MVC example

    Concept of dispatcher servlet in Spring MVC

It is the central sevlet in Spring MVC which dispatches the requests from view to controller and vice versa. It does even more than that.It is integrated with Spring IoC container so that we can use every other feature that spring has.

Tools Used:

2)Apache Tomcat 8.0


1)Open Eclipse in suitable work space and create a new maven project

2)Select web app archetype while creating the project3_webapp_archtype
3)Give group id , artifact id version and  package properly and press Finish


4)Create a new server instance and add the project to it


5)Right click on the project and select the configure build path option , if the project is having errors after creation


6)Select Add Library option


7)Add server run time libraries. We are using Apache Tomcat


8)Edit the pom.xml

9)We are developing a login form with an annotated controller. The index.jsp is the welcome file. From the index page , it is redirecting to the /login of the controller   and from there to loginpage.jsp.JSTL is using to do the redirect from index.jsp . The jsp page  loginpage.jsp  has the login form. When user submits the form ,the /login with  POST method is invoking  and the successpage.jsp will be rendered.



The servlet file defines the package where the container needs to look for dependencies . Also it provides the viewResolver bean which injects the view pages.



Our controller class has two methods .

1)Method login() :- To control the redirect from the index.jsp

It returns a ModelAndView object.Also it is setting a User object as model attribute. This model attribute properties are mapped to the input fileds in the loginpage.jsp

2) Method doLogin() :- It is a POST operation.It accepts the userId and password in the already set model attribute.And it renders a login success page.



10)Build and run the application.Login form will be rendered.Upon submitting the form success page will be rendered.



See More:

Spring Framework overview
Spring MVC Validation Example