Pages

Monday, August 2, 2010

SPRING WEB MVC

Features of Spring Web MVC
Spring's web module provides a wealth of unique web support features, including:
  • Clear separation of roles - controller, validator, command object, form object, model object, DispatcherServlet, handler mapping, view resolver, etc. Each role can be fulfilled by a specialized object.
  • Powerful and straightforward configuration of both framework and application classes as JavaBeans, including easy referencing across contexts, such as from web controllers to business objects and validators.
  • Adaptability, non-intrusiveness. Use whatever controller subclass you need (plain, command, form, wizard, multi-action, or a custom one) for a given scenario instead of deriving from a single controller for everything.
  • Reusable business code - no need for duplication. You can use existing business objects as command or form objects instead of mirroring them in order to extend a particular framework base class.
  • Customizable binding and validation - type mismatches as application-level validation errors that keep the offending value, localized date and number binding, etc instead of String-only form objects with manual parsing and conversion to business objects.
  • Customizable handler mapping and view resolution - handler mapping and view resolution strategies range from simple URL-based configuration, to sophisticated, purpose-built resolution strategies. This is more flexible than some web MVC frameworks which mandate a particular technique.
  • Flexible model transfer - model transfer via a name/value Map supports easy integration with any view technology.
  • Customizable locale and theme resolution, support for JSPs with or without Spring tag library, support for JSTL, support for Velocity without the need for extra bridges, etc.
  • A simple yet powerful JSP tag library known as the Spring tag library that provides support for features such as data binding and themes. The custom tags allow for maximum flexibility in terms of markup code. For information on the tag library descriptor, see the appendix entitled Appendix D, spring.tld
  • A JSP form tag library, introduced in Spring 2.0, that makes writing forms in JSP pages much easier. For information on the tag library descriptor, see the appendix entitled Appendix E, spring-form.tld
  • Beans whose lifecycle is scoped to the current HTTP request or HTTP Session. This is not a specific feature of Spring MVC itself, but rather of the WebApplicationContext container(s) that Spring MVC uses. These bean scopes are described in detail in the section entitled Section 3.4.4, “The other scopes”

Spring Web MVC Flow
  1. Spring Web MVC is identical to Struts Framework
  2. In every web application framework one front controller must be configured in web.xml file
  3. The purpose of the configuring the front controller in web.xml file is to collect all the HTML form requests into a single servlet class that will manage the flow of execution for any number of HTML form. Hence using this feature instead of writing one servlet class for each HTML form; for the entire application only one single servlet will be configured. We can reduce many servlets with a single servlet.
  4. The front controller class name is org.springframework.web.servlet.DispatcherServlet in spring framework. In struts we have ActionServlet.
  5. And the url-pattern configured for that servlet is *.htm. In struts we have *.do
  6. The intension is every HTML form request want to be submitted to spring web MVC framework must submit action path ends with .htm extension. For instance suppose if a new customer information want to be submitted the HTML form must submit requesting using ./nc.htm
  7. Soon after the request comes to DispatcherServlet, DispatcherServlet reads disp-servet.xml file (Spring files we usually stores with the name web.xml) but in spring web MVC the spring configuration file must be  saved with the servlet name as prefix disp and -servlet.xml as suffix. (disp-serlvet.xml)
  8. In the disp-servlet.xml file the action path is mapped to spring controller class (in spring business logic is called as Contrller class).
  9. DispatcherServlet instantiates Controller class, injects if any dependent objects are required and invokes its handleRequest(HttpServletRequest,HttpServletResponse) method
  10. In spring web MVC the controller class must implement business logic implementation in handleRequest() method.
  11. The function must read all request parameters using HttpServletRequest object.
  12. Stores the record into DB using jdbcTemplate or HibernateTemplate.
  13. After return which JSP/HTML page want to be returned to browser using ModelAndView class object.
  14. ModelAndView class contains the response generated by the spring controller and response view name in a single object.
  15. Soon the ModelAndView object is returned to DispatcherServlet, DispatcherServlet stores the response in request scope and redirect the response view to browser.
  16. The response view retrieves the response from request scope and display's the out put on browser.
Example
The Flow of this application is almost nearby similar or Struts web application framework, except with some minor changes. The changes are FrontController class name is different, Spring config file name is different, the business component must implement the Controller interace, the method name is handleRequest() and return type is ModelAndView.


index.jsp

<html>
  <body>
   <form action="./nc.htm">
   <table>
   <tr>
   <td>Customer ID : </td>
   <td><input type="text" name="id"/></td>
   </tr>
   <tr>
   <td>Customer Name : </td>
   <td><input type="text" name="name"/></td>
    </tr>
    <tr>
   <td>Customer Age : </td>
   <td><input type="text" name="age"/></td>
    </tr>
   </table>
  <tr>
  <input type="submit" name="submit" value="Submit"/>
  </tr>
   </form>
  </body>
</html>


web.xml
<web-app version="2.4" 
xmlns="http://java.sun.com/xml/ns/j2ee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>disp</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
  <servlet>
    <servlet-name>CustomerController</servlet-name>
    <servlet-class>com.CustomerController</servlet-class>
  </servlet>
<servlet-mapping>
<servlet-name>disp</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
  <servlet-mapping>
    <servlet-name>CustomerController</servlet-name>
    <url-pattern>/servlet/CustomerController</url-pattern>
  </servlet-mapping>
 <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

disp-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean name="/nc.htm" class="com.CustomerController">
</bean>

</beans>

CustomeController.java
package com;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

public class CustomerController implements Controller {
public ModelAndView handleRequest(HttpServletRequest req,
HttpServletResponse res) throws Exception {
int id = Integer.parseInt(req.getParameter("id"));
String name = req.getParameter("name");
int age = Integer.parseInt(req.getParameter("age"));
System.out.println(" Name :: "+ name+ " Age :: "+age);
return new ModelAndView("/success.jsp","id",new Integer(id) );
}
}

Success.jsp
<html>
  <body>
      Customer ID is : <%=(Integer)request.getAttribute("id") %>
     </body>
</html>


No comments:

Post a Comment