Spring MVC – Part 1


Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/.pomey/justpropose/solutionhacker.com/wp-content/plugins/attachment-manager/wp-attachment-manager.php on line 402

Introduction of Spring MVC

I have heard that Spring MVC is cleaner and well-design than Struts 1.x. I found it no surprise as Spring already wins the heart of most J2EE developers like us. I decide to take a look at Spring MVC and see how I can logically map my concept from Struts to Spring MVC. After I go through the framework, I feel that they are pretty close logically. However, the code I write in Spring MVC gives 2 main advantages: Portable and Testable.

The request flow of Spring MVC

  1. The Client requests for a Resource in the Web Application.
  2. The Spring Front Controller, which is implemented as a Servlet (DispatcherServlet), will intercept the Request and then will try to find out the appropriate Handler Mappings.
  3. The Handle Mappings is used to map a request from the Client to its Controller object by browsing over the various Controllers defined in the Configuration file. With the help of Handler Adapters, the Dispatcher Servlet will dispatch the Request to the Controller.
  4. The Controller processes the Client Request and returns the Model and the View in the form of ModelAndView object back to the Front Controller.
  5. The Front Controller then tries to resolve the actual View (which may be Jsp, Velocity or Free marker) by consulting the View Resolver object.
  6. Then the selected View is rendered back to the Client.

springmvc1.JPG

How Spring MVC renders the response?

In controller:

View pdfView =Map modelData = new HashMap(); ModelAndView mv1 = new ModelAndView(pdfView, modelData); OR ModelAndView mv1 = new ModelAndView("myView", someData); //pass string for logical view name

In configuration ([dispatcher-servlet-name]-servlet.xml) :

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix"><value>/WEB-INF/</value></property> <property name="suffix"><value>.jsp</value></property> </bean>

Note: If we put the resolver above in the configuration xml, myView.jsp in the WEB-INF folder will be used to render the view. One of the dis-advantage of using Internal Resource View Resolver is that the name of the View file (whether it is a Jsp File or the Pdf File) must be present in the Web Application Context.

<bean id="beanNameResolver" class="org.springframework.web.servlet.view.BeanNameViewResolver"/> <bean id = "myView" class = "MyPdfGenerator"/>

Note: If we use BeanNameViewResolver, the framework will search for a bean name "myView" to render the response. MyPdfGenerator could extend AbstractPdfView, so the view would be dynamically generated in pdf.

Why Spring MVC is more portable?

The Struts design is based on concrete inheritance, meaning that each custom action has to be in an inheritance hierarchy of the Struts Action component. Because Spring controllers are interfaces, any component can play the role of the controller. This gives application designers more flexibility in the design of components. At the framework component level, Struts requires use of Struts-specific objects, such as Form Beans (static or dynamic), Actions, Action Mappings, Action Forwards, and Request Processors. Spring MVC is more flexible, as all its major components are defined as interfaces. "Design by interface" makes MVC code less coupled with the framework.

Why Spring MVC is more testable?

public void final testGettingToDetails throws Exception{ MyController myController = new MyController(); myController.setDetailsView( detailsViewName ); MockHttpServletRequest request = new MockHttpServletRequest(); MockHttpServletResponse response = new MockHttpServletResponse(); request.setMethod("POST"); request.addParameter("viewDetails", "true"); ModelAndView modelAndView = myController.handleRequest(request, response); assertEquals("Incorrect view name", detailsViewName, modelAndView.getViewName()); }

You can find the detail of how to unit test your spring mvc code here

Migrate from Struts to Spring MVC?

  1. Action -> Controller
  2. ActionForm -> POJO (request maps directly to POJO!)
  3. ActionForward -> ModelAndView
  4. struts.xml -> Spring bean definition xml (by default: [dispatcher-name]-servlet.xml)

Reference

http://spring.javabeat.net/articles/2007/06/spring-mvc-web-framework-introduction/

Attached Files:

Leave a comment

0 Comments.

Leave a Reply

You must be logged in to post a comment.