Spring MVC
From Kb
Contact Article Author | Blog of Article Author | FirstPartners.net Home | LinkedIn profile of Author
Contents |
See also
- Customising Spring Property File Loading
- Spring Framework
- Spring Webflow
- Struts 2 Appfuse Dojo and Ajax
- Struts 2 Web Framework
External Documentation on the Spring MVC (Model View Controller) Web Framework
- Spring Documentation Homepage : http://www.springframework.org/documentation
- Manning 'Spring in Action' book , section 3
Flow of control in Spring Web app
- User makes request to [myserver]/[myapp]/somepage.htm
- web.xml maps this to (one of) the Spring dispatcher servlets
- This looks up the url in the Spring app context (e.g. springApp-servlet.xml)
- Normally have a urlMapping bean (e.g. Spring SimpleUrlHandlerMapping implements HandlerMapping) that says which Spring bean is called
- Spring bean implements Controller has method public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
- Spring controller akin to Struts Action class.
- handleRequest method called, does it's stuff , returns ModelAndView class
- Within Spring file , instance of ViewResolver (e.g. ResourceBundleViewResolver - uses property file to resolve to class) is configured - maps to the bean which Tells Spring what / how to find the view.
- If forward to JSP use (as simple example) InternalResourceViewResolver - takes name in ModelAndView class and adds (configurable) prefix / suffix to come up with jsp name to display.
- Other ViewResolvers available
- Can display view via JSP (e.g. as per Struts) , or completely in Java Code within the Spring view bean (e.g. like traditional Servlets)
- JSP uses {beanName.internalBean.method} type syntax to display results from the model
- Can manipulate these values using standard JSTL Tags.
- Spring tagLib provides access to other Spring values (sample on page 315 of manning book)
Spring Tag Libraries
Spring form: Tag Library
- Bundled with Spring 2
- Spring Tags Reference
- To install
- Make sure tld definition is included in web.xml
- reference tag at top of your jsp page
- (Here spring core, spring core and standard JSTL tags are imported)
<!-- Standard Spring tags --> <%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%> <!-- Spring tags to bind objects to HTML input / output values --> <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%> <!-- Standard JSTL taglib --> <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt"%>
<!-- Spring MVC tag for form - commandName is the Spring Bean that the values will be bound to--> <!-- 'name' (myForm) is what it will be rendered as in HTML i.e. if we want to refer to values in Javascript we go Document.myForm.submit() --> <form:form commandName="mySpringBeanName" name="myForm" method="post"> <!-- Other Fields--> <!-- Path refers to some bean available in the Session / Flow. It does not include the form bean (as it is defined by the form tag). The samples below would ties to myFormBean.getNameOfVarOnMySpringBean().getValueWithGetAndSetMethodsOnVar() etc --> <form:label id='someLabel' path="nameOfVarOnMySpringBean.someSubGetMethod"/> <form:input id='someInput' path="nameOfVarOnMySpringBean.valueWithGetAndSetMethodsOnVar"/> <!-- IMPORTANT note that for JSTL TAGS myBeanName must be added to the path --> <!-- You need to use JSTL as Spring does not supply a simple text output method--> <c:out value="${myFormBean.nameOfVarOnMySpringBean.someSubGetMethod}"/> <!-- Button to submit form --> <input type="submit" value="OK" /> </form:form>
Java Standard Tag Library (JSTL)
<c:forEach items="${myFormBean.customerList}" var="cust" varStatus="status"> <c:out value="${customerList.customerName}" /> <form:checkbox path="customerList[${status.index}].customerPremiumPaid"/> </c:forEach>
Spring MVC and Apache Tiles
General
- Quick Start on Apache Tiles
- Apache Tiles Tutorial
- Spring Documentation on Integrating Tiles and Spring MVC
Apache Tiles Integration with Spring MVC
- org.springframework.web.servlet.view.tiles.TilesConfigurer
- Loads Tiles Configuration as part of Spring Context (means that we don't need to load the Tiles Servlet (via the Web.xml) , which would normally do this.
- Use of a custom Spring View Resolver (extending Springs AbstractCachingViewResolver). org.springframework.web.servlet.view.tiles.TilesView is the Spring View that allows Spring to pass control to tiles.
- Flow of control is as per Section above; when the time comes to display a view, The program resolves to Tiles , which then does it's stuff.
Alternative Views
AbstractXsltView
Spring MVC and Spring Webflow
- Entry point from MVC to Webflow:
- Url (e.g. /webflow) is mapped in SpringContext file (name of context file is related to name of Servlet). Spring looks up the bean associate with this. For Webflow , this is an instance of the org.springframework.webflow.executor.mvc.FlowController, takes another bean as a param
- flow:executor - refers (using shortcuts) to the registry
- flow:registry - refers (using shortcuts) to the Spring Webflow config files
- Url (e.g. /webflow) is mapped in SpringContext file (name of context file is related to name of Servlet). Spring looks up the bean associate with this. For Webflow , this is an instance of the org.springframework.webflow.executor.mvc.FlowController, takes another bean as a param
- Exit point from Webflow to MVC:
- The output of the Webflow is always a view state. This viewState is bound to a SpringViewResolver to that Spring MVC knows what page / view to display once the flow enters a wait state.
Comparing Spring MVC to Struts and Struts 2
- Both implementations on Model View Controller
- Spring controller akin to Struts 2Action class. Both POJO's , unlike Struts 2.
- Config done in standard Spring context, using Standard beans. Struts (2) has Struts(-config).xml , carries out most of the URL -> Bean -> view mapping functionality.
- (Slightly) easier to unit test Spring MVC, use Standard Spring unit testing tools. Would tend to test Struts + Spring combo at the Spring level anyway.
- Possibility of using Spring ThrowawayController - similar to Struts 2 Action + ActionForm combo.

