Web MVC with the Spring Framework Spring's web MVC architectural pattern Juergen Hoeller 1. Introduction: Spring the Application Framework 1. Introduction: Spring application framework When first confronted with the Spring Framework, one might be tempted to think: "Oh no, not yet another web framework ". This article will outline why Spring is not particularly a web framework but a generic lightweight application framework with dedicated web support, and show the architectural differences to Struts and WebWork Spring when you first see when you I will say: "Oh, another web architecture". This article will tell you that Spring is clearly distinguished from other lightweight Application Framework, which will focus on the web support, with Struts and WebWork have a significant difference. In contrast to Struts or WebWork, Spring is an application framework for all layers: It offers a bean configuration foundation, AOP support, a JDBC abstraction framework, abstract transaction support, etc. It is a very non-intrusive effort: Your application classes do not need to depend on any Spring classes if not necessary, and you can reuse every part on its own if you like to From its very design, the framework encourages clean separation of tiers, most importantly web tier and business logic:. eg the validation framework does not depend on web controllers Major goals are reusability and testability:. Unnecessary container or framework dependencies can be considered avoidable evils on and struts and webwork contrast, Spring is an application framework for all levels: provides the bean Configure the foundation, AOP support, JDBC extraction framework, abstract transaction support, and so on. It has a very significant feature: at a level if you don't need Spring support, you can use the string Class, only the features of some part of it. From its design concept, you can see String helps you achieve a true logic layer and a Web layer separation: for example. A check application will not rely on Controllers. Such goals are better reuse and easy test: it will not be able to achieve this unnecessary container and framework.
Of course, Spring's own web support is nicely integrated with the framework's general patterns. Nevertheless, replacing the web solution with Struts, WebWork, or the like is easy. Both with Spring's web support or a different one, Spring allows for building a true dedicated middle tier in the web container, with the option to reuse exactly the same business logic in test environments or standalone applications And within J2EE, your business logic will not unnecessarily depend on container services like JTA or EJB -. allowing complex, well-architected web Applications to run in a "Simple" Container Like Tomcat or Resin. Spring's own Web Support and usual Framework mode is complete. However, Spring replaces Struts, webwork or other web scenarios very easy. For Spring's Web Support or different places, Spring allows you to create an intermediate layer in the web container, set up your business logic in your test environment or standard independent app. And in the J2EE environment, your business logic does not have to rely on the container Services, like JTA, EJB support. Good architecture web applications can run on any container, such as Tomcat or Resin. Note That Spring Doesn't Generally Aim to Compete With Existing Solutions. It Rather Fosters Seamless Integration with Standards Like Servle . T, JSP, JTA, JNDI, JDBC, and JDO, and well-suited tools like Hibernate, Velocity, Log4J, and Caucho's Hessian / Burlap The framework is designed to grow with the needs of your applications, in terms of technology choice: for example, you will probably use JTA via Spring's JtaTransactionManager if you need distributed transactions - but only then, as there are perfect replacements for single databases, like DataSourceTransactionManager or HibernateTransactionManager worth noting, Spring not and solutions that already exist to compete. We encourage combination of standard technology, such as servlet, jsp, jta, jndi, jdbc, and jdo, and very matched tools, such as Hibernate, Velocity, Log4j, and Caucho '
S Hessian / Burlap. The design idea of this framework is to improve when your application needs to be improved: For example, if you need a distributed transaction processing, you may need to use Spring's JTA TransactionManager to implement JTA service. Or use DataSourceTransactionManager or HibernateTransactionManager to achieve a wonderful perfect single database exchange.
2. Web MVC: The Design of Spring's Web Framework Web MVC: design concept Spring web framework Spring's web framework is designed around a DispatcherServlet that dispatches requests to handlers, with configurable handler mappings, view resolution, and locale and theme resolution The default. handler is a very simple Controller interface, just offering a "ModelAndView handleRequest (request, response)" method. This can already be used for application controllers, but you will prefer the included implementation hierarchy, consisting of AbstractController, AbstractCommandController, MultiActionController, SimpleFormController, .. AbstractWizardFormController Application controllers will typically be subclasses of those Note that you can choose an appropriate base class: If you do not have a form, you do not need a FormController This is a major difference to Struts Spring frame by a configuration. Operating mappings, display resolution, localization, and template integration around servlet - DispatcherServlet, the default operation is a very simple control interface, he only provides ModlandView HandleRequest (Request, Re) Sponse) method. This will be used for an application control, however, if you want to include multiple hierarchical controls, AbstractController, AbstractCommandController, MultiactionController, SimpleFormController, AbstractWizardFormController will help you complete. Application control will represent those subsystems. Note, you You can choose an appropriate base class: If you don't have web form, you don't have to use formcontroller. This is the biggest difference with Struts. You can take any Object As Command OR Form Object: there's no need to import an interface or deerive from a base class. Spring's data binding is highly flexible, eg it treats type mismatches as validation errors that can be evaluated by the application, not as system errors. So you do not need to duplicate your business objects'
properties as Strings in your form objects, just to be able to handle invalid submissions, or to convert the Strings properly. Instead, it's often preferable to bind directly to your business objects. This is another major difference to Struts which is built around required base Classes Like Action and ActionForm - For Every Type of Action. You can operate any object through a command or Form object: This does not require an interface tool or a basic class drive. The data of Spring is very flexible. For example It describes the input mechanism with check errors within the application, but is not a system error. So in your form object you don't have to copy your business object's string property, just operate an error, or appropriate Convert String. In other words, it is usually a good direct bonding your business object. This is also one of the main differences with Struts to establish the basic class action and actionform (each Action operation type). Compared to Webwork, Spring has more differentiated object roles: It supports the notion of a Controller, an optional command or form object, and a model that gets passed to the view The model will normally include the command or form object but also arbitrary reference data Instead,.. A WebWork Action Combines All Those Roles Into One Single Object. Webwork Does Allow You To Use Existing Business Objects as PA rt of your form, but just by making them bean properties of the respective Action class. Finally, the same Action instance that handles the request gets used for evaluation and form population in the view. Thus, reference data needs to be modelled as bean properties Of the action Too. These are arguably Too Many Roles in One Object. Comparison Webwork, Sping More Differences Differences between Object Roles: SPING Support Controller's Implicit, an Operation Command or Form Object, and a mode to be delivered to the view. This mode usually contains commands and FORM objects, but sometimes contains any reference data. In other words, a webwork action combines all of these roles to a separate object .WebWork allows you to use existing business objects as part of your form, However, only the BEAN properties of their respective Actions. Finally, the Action instance of the operation request is paid to the Form Population in a view. However, the reference data also needs to be simulated as an attribute as an action. There are too many roles in a class worth it. Discussion. Regarding Views: Spring '
s view resolution is extremely flexible. A Controller implementation can even write a view directly to the response, returning null as ModelAndView. In the normal case, a ModelAndView instance consists of a view name and a model Map, containing bean names and corresponding objects ( like a command or form, reference data, etc). View name resolution is highly configurable, either via bean names, via a properties file, or via your own ViewResolver implementation. The abstract model Map allows for complete abstraction of the view technology, without any hassle: Be it JSP, Velocity, or anything else - every renderer can be integrated directly the model Map simply gets transformed into an appropriate format, like JSP request attributes or a Velocity template model on view:.. Spring's view resolution is extremely Flexible. A controller executes You can write directly to the ModelAndView object null, you can write directly to a view. In the usual condition, a ModlandView instance combines a view and a Model Map, which contains bean name and communication objects (like command Or Form, Reference Data, etc.). The VIEW name is very high-end configuration, not through Bean Name, a Properties file is through your own viewresolve r. This abstract model map allows you to complete your extraction in the view level, no argument: JSP, Velocity, or others, each of them can be used directly. This Model Map can simply get appropriate formatted data. , like JSP request attributes or a Velocity template model 3. integration:. using a different web framework with Spring integration: Spring with a different web framework Many teams will try to leverage their investments in terms of know-how and tools, both for existing projects and for new ones. Concretely, there are not only a large number of books and tools for Struts but also a lot of developers that have experience with it. Thus, if you can live with Struts' architectural flaws, it can still be a Viable Choice for the Web Layer. The Same Applies to Webwork and Other Web Frameworks. Many development teams will invest for their existing projects or new projects have been gains. There is no large number of books and tools like Struts. ,
But we have a large number of developers with Spring development techniques. However, if you are willing to live in the Struts architecture, he will be a good choice in the Web layer. Of course, other applications are also the same. If you do 't want to use Spring's web MVC but intend to leverage other solutions that Spring offers, you can integrate the web framework of your choice with Spring easily. Simply start up a Spring root application context via its ContextLoaderListener, and access it via its ServletContext attribute (or Spring's respective helper method) from within a Struts or WebWork action Note that there are not any "plugins" involved, therefore no dedicated integration:. from the view of the web layer, you'll simply use Spring as a library, With the root application context instance as entry point. If you don't want to use Spring's Web MVC, you want to borrow Spring to marry other solutions, you can inherit your own web framework very simple through Spring. You can very easy to pass ContextLoaderListener Start a Spring root application context, and use the ServletContext property (or Spring Helper method) by Struts or WebWork. It is worth noting that there is no "plugins" called, so there is no special integration: View from the web layer, you can simply use Spring as a class library that manages the Application Context instance entry point. All Your registered beans and all of Spring's services can be at your fingertips even without Spring's web MVC. Spring does not compete with Struts or WebWork in this usage, it just addresses the many areas that the pure web frameworks don '
t, from bean configuration to data access and transaction handling. So you are able to enrich your application with a Spring middle tier and / or data access tier, even if you just want to use eg the transaction abstraction with JDBC or Hibernate. All you Registering Bean and Spring services can be easily mastered without Spring's Web MVC. Spring is not competing with struts, webworks, which will be applied to a lot of fields as a pure web framework because it is not You need to configure data storage and transaction operations. So you can use Spring to enrich your app, or even, you want to use JDBC or Hibernate to perform business abstraction. 4. Feature Check List Checklist List IF JUST Focussing on the web support, some of spring '
s unique features are: If the focus on the web support, a number of salient features are Spring: .Clear separation of roles: controller vs validator vs command object vs form object vs model object, DispatcherServlet vs handler mapping vs view resolver, etc. clear role separation: controller, validator, command object, form object, model object, and DispatcherServlet, handler mapping vs view resolver, etc. .Powerful and straightforward configuration of both framework and application classes as JavaBeans, including easy in-between referencing via an application context EG from Web Controllers to Business Objects and Validators. Powerful and direct frameworks and application configurations as JavaBeans, including simple reference and application content, for example, from the web controller to business objects and data checks. .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 Action / ActionForm for everything adaptability plug: whatever controller You need to get everything that replaces the action / actionform (simple, command, form, example, multiple action, or custom one) .reusable business code, no need for duplication: you can US E Existing Business Objects As Command OR Form Objects INSTEAD OF Mirroring The in Special ActionForm Subclasses. Restable business logic code, no copy: You can use the existing business logic object as a command or the Form object replaces the reflex specific Actionform subclass. .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 can be customized and bonding. Data Check: As an error check, fixed date, and digital bonding of the application level, for example, replace a single String by manually checking and converting business objects.