Spring's Web MVC architecture mode

zhaozj2021-02-16  61

Web MVC with the Spring Frameworkspring Web MVC architecture mode

Juergen Hoeller

1. Introduction: Spring the Application Framework1. Introduction: Spring Application

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 Difference to struts and webwork

When you first see Spring, you will say: "Oh, another web architecture". This article will tell you that Spring is clearly different 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 Dend on Web Controllers. Major Goals Are Reusability and Testability: Unnecessary Container OR Framework Dependencies Can Be ConsideRed Avoidable Evils.

On the contrast between Struts and Webwork, Spring is an Application Framework serving all levels: provides the BEAN's configuration base, AOP support, JDBC's extraction framework, abstract transaction support, and more. 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 framework web applications can be run on any container, such as Tomcat or Resin.

Note that Spring does not generally aim to compete with existing solutions. It rather fosters seamless integration with standards like Servlet, 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.

It is worth noting that Spring is not competing with the existing solutions. We encourage combination of standard technologies such as servlet, JSP, JTA, JNDI, JDBC, And JDO, and very matched tools, such as Hibernate, Velocity, Log4j, And caucho's hessian / burp. The design idea of ​​this framework is to make some technology choices when your application needs to be improved: For example, if you need distributed transaction processing, you may need to use Spring JTA TransactionManager implements JTA services. Or, use DataSourceTransactionManager or HibernateTransactionManager to achieve a wonderful perfect single database exchange. 2. Web MVC: The Design of Spring's Web FrameworkWeb MVC: Spring Web Framework Design Thoughts

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 don't have a form, you don't need a formController. This is a major Difference to strut.

The Spring framework is designed by configuring the SERVLET - DispatcherServlet designed by configuring the resolution, localization, and template integration. The default operation is a very simple control interface, he only provides ModlandView HandleRequest (Request, Response) 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 that you can choose A 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 implement an interface or derive 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 Action Required Bases Like Action And ActionForm - for Every Type of action. You can operate any objects by commands: this does not require interface tools or a basic class drive. .Spring's data bond is very flexible. For example, it describes the input mechanism with check errors within the application, but is not a system error. So you don't have to copy your business in your Form object. The String property of the object, only operates an error, or simply converts String. In other words, it is usually a good direct bonding your business object. This is also the Struts surrounded by the basic class Action and Actionform (each Action operation types) Establish a main difference.

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 part of your form, but just by making them bean properties of the respective Action class. Finally, the same Action instance .

Compare WebWork, SPING is the object character: SPING support controller's sense, an operation command or FORM object, and getting data passes to the view. This mode usually contains commands and FORM objects, but sometimes anything Reference data. In other words, a WebWork Action combines all of these roles to a separate object .WebWork allows you to use the existing business object as part of your form, but only generate the bean properties of their respective Action. Finally, the Action of the operation request Examples have paid and form population in a view. However, reference data also needs to be simulated as an attribute as an action. There are too many roles in a class to be discussed .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.

About view: Spring's viewing solution is very flexible. A controller executes You can write to a view directly to a view directly. In normal conditions, a ModlandView instance combines a view and a Model Map, including Bean name and communication object (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 ViewResolver. This abstract model map allows you The view level completes the extraction, there is no argument: JSP, Velocity, or other, each of them can be used directly. This Model Map can simply get the appropriate formatted data conversion, like the JSP request attribute or velocity template mode .

3. Integration: Using a Different Web Framework with Spring Integration: Spring 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 development teams will be project or new for their existing projects or new The project has been obtained and the tool is investing. 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 for the development of the Web layer. Of course, other applications are the same.

If you do not 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 start a spring root application context very simple through ContextLoaderListener, and pass Struts or Webwork's Action uses servletContext properties (or Spring Helper method) to access it. It is worth noting that there is no "plugins" called, so there is no special integration: view from the web layer, you can simple 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 do not, 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 your registered bean and Spring Services can be easily mastered without Spring's Web MVC. Spring is not competing with struts, webwork, which will be applied as a pure web framework in many fields because it does not need to configure data storage. And business operations. So, you can use the middle-level and data storage layers provided by Spring to enrich your app, or even, you want to use JDBC or Hibernate transaction abstraction.

4. Feature Check List Features Checklist

IF Just Focussing On The Web Support, Some of Spring's Unique Features Are:

If focused on Web support, some of Spring's significant features are:

. 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, e.g. from web controllers to business objects and validators.

Powerful and direct frameworks and application configurations as JavaBeans, including simple reference and application content, such as from the web controller to business objects and data validations.

.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-in: No matter what 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 use existing business objects as command or form objects instead of mirroring them in special ActionForm subclasses reuse business logic code, you do not need to copy: you can use existing business objects as command Or, the Form object replaces the reflective 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.

Customized Bonds and 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.

.Customizable handler mapping, customizable view resolution: flexible model transfer via name / value Map, handler mapping and view resolution strategies from simple to sophisticated instead of one single way.

Customized operations, customized view: Flexible models are delivered through Name / Value Map, using a simple method from simple to complex operation mapping and viewing solution policies.

. Customizable Locale and Theme Resolution, Support for JSPS with and $ it Spring Tag Library, Support for JSTL, Support for Velocity Without The need for extra bridges, etc.

Local custom and theme solution: Spring does not have taglib, fully supports JSPS, JSTL, you can support Velocity, etc. you don't need additional connections.

. Simple But Powerful Tag Library That Avoids HTML Generation At Any Cost, Allowing for Maximum Flexibility In Terms of Markup Code.

Simple but powerful Tag Library avoids chaos generated by HTML, maximizing flexible extension marker code.

LINKS connection

Original: http://www.springframework.org/docs/web_mvc.html

Spring framework Website: http://www.springframework.org

Web MVC Support (CVS): http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/springframework/main/src/org/springframework/web/servlet

Translator's Homepage: http://xglw.51.net/5team

Yangery-ge@263.net

转载请注明原文地址:https://www.9cbs.com/read-28028.html

New Post(0)