Powered By Blogger

Monday, December 19, 2016

Spring MVC (Model View Controller) -

Key Points - 
-----------------------------------------------------------------------------------------------------------------------

MVC is a design pattern where an application is can be divided into 3 layers so that each layer has exactly one responsibility and does not know much about other layers.

Model        - Looks into application's business logic and interactions with database.

View           - Looks into presentation part on how to display. Also, view technologies can be changed easily or upgraded as view is not dependent on other layers.

Controller - Acts as a middleman in which additional logic can be added for controlling role based access (example). Also, takes command from View layer ( add data, edit data, delete data, etc) and gets the work done from Model layer.(request/response)
------------------------------------------------------------------------------------------------------------------------

Dispatcher Servlet -

1. Web application works with client-server architecture. Client sends a request for some resource and server responds with a resource.

2. We need a servlet which process request with the help of other classes (data + business logic) and send response.

3. DispatcherServlet handles all requests in a central place and sends request to the controller which in turn processes (store/load) data based on the business rules.

4. DispatcherServlet uses FrontController design pattern which means handling of an application will be done at a central place.

5. Example web.xml

    <web-app>
     <servlet>
          <servlet-name>example</servlet-name>
         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
         <load-on-startup>1</load-on-startup>
     </servlet>

     <servlet-mapping>
         <servlet-name>example</servlet-name>
         <url-pattern>*.form</url-pattern>
     </servlet-mapping>
    </web-app>
    (Taken from http://docs.spring.io/spring-framework/docs/2.0.x/reference/mvc.html)

6. URLs ending with .form will be handled by example dispatcher servlet.

7. Spring framework looks for a file named [servlet-name]-servlet.xml in the WEB-INF directory of web application. Also, creates beans defined in this xml file.
    As per above point 5 - file name will be example-servlet.xml

   <context:component-scan base-package="com.xyz.xyz" />
 
   <!-- Configuration defining views files -->
 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
          <property name="prefix">
              <value>/WEB-INF/views/</value>
          </property>
          <property name="suffix">
              <value>.jsp</value>
         </property>
 </bean>

8. If you want to give different servlet name or different location (other than default location i.e.,WEB-INF directory, then add following details in the web.xml file

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/spring/example-servlet.xml          
        </param-value>
    </context-param>

No comments:

Post a Comment