springMVC和struts2的区别

Apache Struts 2 and SpringMVC, these two are the most popular and much talked about Java web frameworks today. Many of you might have worked with both of these frameworks, but which is one is better to use? What are the basic differences between both of these frameworks?

阿帕奇 Struts2和SpringMVC,是目前最受欢迎和最常提及的Javaweb框架。你们大多已经和这两个框架打交道了,但是,谁才是最佳的?他们间最基本的不同是什么?


Well, Apache Struts 2 is an elegant and extensible framework that is used for creating enterprise-level Java web applications. It is designed to streamline the development cycle, starting from building to deployment and maintenance of the application. In Struts, the object that is taking care of a request and routes it for further processing is known as “Action”.
好,阿帕奇 Struts2 是一个元素和可扩展的框架,经常用于创建企业级Javaweb应用。他被设计来使开发周期简单化,开始于建立配置和维护应用,在Struts中,这个东西用来照顾请求和发送来获取更多的进程,我们最熟悉的是“action”;

On the other hand, Spring MVC is a part of a huge Spring framework stack containing other Spring modules. This means that it doesn’t allow developers to run it without Spring, but the developers can run the Spring Core without Spring MVC. The Spring MVC (Model View Controller) is designed around a DispatcherServlet, which dispatches the requests to handler with configurable handler mappings, view resolution and theme resolution.
一方面,SpringMVC是巨大的Spring框架堆中联系其他Spring组件的一部分。这意味着,她不允许开发者不通过Spring来启动,但是开发者可以不通过SpringMVC来启动Spring核心文件。SpringMVC(Model,view,Controller)被围绕DispatcherServlet设计,这个发送请求来处理配置了的处理器映射,视图分辨率和主题分辨率;

While the objects responsible for handling requests and routing for processing in Struts called an Action, the same object is referred as Controller in Spring Web MVC framework. This is one of the very first differences between Spring MVC and Struts2. Struts 2 Actions are initiated every time when a request is made, whereas in Spring MVC the Controllers are created only once, stored in memory and shared among all the requests. So, Spring Web MVC framework is far efficient to handle the requests than Struts 2.
在struts中,当目标负责处理请求和发送进程的被称为Action,在SpringMVC框架中,同一个目标被归类于Controller(控制器)。这是SpringMVC和Struts2的第一个不同点;Struts2 的Action是每当一个请求产生时,就被初始化一次;然而在SpringMVC中,Controller(控制器)只被创建一次,然后缓存起来,并且在所有的request中共享;所以,SpringWeb MVC框架比Struts2在处理请求上面更有效;

If we talk about the features, Struts 2 and Spring MVC framework caters different level of business requirements. Let’s take a look at features offered by both of these frameworks.
如果我们谈到特点,Struts2和SpringMVC框架满足不同的商业需求的需要。让我们看看这两者间的特点;

Struts 2 features
Struts2特点:


  1. Configurable MVC components, which are stored in struts.xml file. If you want to change anything, you can easily do it in the xml file.(配置MVC组件,存储于Struts.xml文件,如果你想改变些东西,你可以很容易在xml中操作)
  2. POJO based actions. Struts 2 action class is Plain Old Java Object, which prevents developers to implement any interface or inherit any class.(POJO基于action。Struts2的action类是简单地,旧的Java 对象,即POJO,他不允许开发者实现任何借口或继承任何类)
  3. Support for Ajax, which is used to make asynchronous request. It only sends needed field data rather than providing unnecessary information, which at the end improves the performance.(支持Ajax,这是用于产生异步请求。这仅仅需要文件数据而不是提供不必要的信息,这最终提高了其表现力)
  4. Support for integration with Hibernate, Spring, Tiles and so on.(支持Hibernate,Spring,Tiles等的集成)
  5. Whether you want to use JSP, freemarker, velocity or anything else, you can use different kinds of result types in Struts 2.(无论你们是否使用JSP,freemarker,Velocity等,再struts2中你可以使用不同种类的resultType)
  6. You can also leverage from various tags like UI tags, Data tags, control tags and more.(你可以同时利用各种标签,例如UI标签,Data标签,控制标签等)
  7. Brings ample support for theme and template. Struts 2 supports three different kinds of themes including xhtml, simple and css_xhtml.(为主题和模板带来丰富的支持,Struts 2支持三种不同的主题,包括简单的xhtm,css_xhtml)
  8. On the other hand, Spring MVC framework brings totally different set of features.(另一方面,SpringMVC框架提供了完全不同的特色集合)


Spring MVC features(Spring MVC特点



  1. Neat and clear separation of roles. Whether it is controller, command object, form object or anything else, it can be easily fulfilled with the help of a specialized object.(整洁和干净的角色分离。无论她是不是控制器,命令对象,构造对象或其他,她可以轻易的在特别对象的帮助下被填满)
  2. Leverage from the adaptability, non-intrusiveness and flexibility with the help of controller method signature.(在控制器方法信号的帮助下,非常适应的,非干扰性和流畅的利用)
  3. Now use existing business objects as command or form object rather than duplicating them to extend the specific framework base class.(如今,使用退出商业对象,作为命令或者构造对象而不是复制他们来扩大特定的基于类的框架)
  4. Customizable binding and validation will enable manual parsing and conversion to business objects rather than using conventional string.(可定制的绑定和批准会使人为的解析和转换成业务对象,而不是使用传统的String)
  5. Flexible mode transfer enables easy integration with the latest technology.(流畅的模式转换使其能够轻易地集成最新的技术)
  6. Customizable locale and theme resolution, support for JSPs with or without Spring tag library for JSTL and so on.(可定制的本地和主题解决,支持JSPs(可使用Spring的JSTL等标签库))
  7. Leverage from the simple, but powerful JSP tag library known as Spring tag library. It provides support for various features like data binding and themes.(利用简单但是有效的JSP标签库,即Spring标签库,它提供大量具有特色的支持,例如数据绑定和主题)
  8. Of course, Struts is one of the most powerful Java application frameworks that can be used in a variety of Java applications. It brings a gamut of services that includes enterprise level services to the POJO. On the other hand, Spring utilizes the dependency injection to achieve the simplification and enhance the testability.(当然,Struts是一个非常有力的Java应用框架,其可以被用于各种Java应用。他提供了一个全领域的服务,包括企业级和加强了可测试性)


Both of these frameworks have their own set of pros and cons associated with it.(两种框架都拥有各自的有缺点)


Struts framework brings a whole host of benefits including:(Struts框架提供一个全主机的好处:)


  1. Simplified design(易于设计)
  2. Ease of using plug-in(插件使用方便)
  3. Simplified ActionForm & annotations(简单的Action构造和注解)
  4. Far better tag features)(更好的标签功能)
  5. OGNL integration(OGNL集成)
  6. AJAX Support(AJAX支持)
  7. Multiple view options and more(多视角的操作)
However, the only drawback with Struts 2 framework is that it has compatibility issues and poor documentation.

(然而,唯一的缺点是Struts框架有兼容性问题和很少的技术文档)
On the other hand, Spring MVC provides benefits like:
(另一方面,SpringMVC提供好多优点:)

  1. Clear separation between controllers, JavaBeans models and views that is not possible in Struts.(在控制器,JavaBean组件和视图中,具有Struts没有的,清晰的分离机制)
  2. Spring MVC is more flexible as compared to the Struts.(SpringMVC相对更流畅)
  3. Spring can be used with different platforms like Velocity, XLST or various other view technologies.(Spring可以在不同的平台使用,例如Velocity,XLST或多种其他的技术)
  4. There is nothing like ActionForm in Spring, but binds directly to the domain objects.(没有比ActionForm更好的来直接绑定领域对象的了)
  5. Code is also more testable as compared to the Struts.(SpringMVC的代码可测性更强)
  6. It is a complete J2EE framework comprising of seven independent layers, which simplifies integration with other frameworks.(J2EE框架包含7中独立的层,这些是可以和其他框架简单整合)
  7. It doesn’t provide a framework for implementing the business domain and logic, which helps developers create a controller and a view for the application.(他没有为了应用商业领域和逻辑提供一个框架,这可以帮助开发者为应用来创建一个控制器和视图)

However, like any other technologies or platforms, Spring MVC too suffers from several criticisms related to the complexity of the Spring framework.(然而,像其他所有的技术和平台一样,SpringMVC也遭受了许多批评,例如太过于复杂)


Final Verdict


Either framework is a great choice. However, if you’re looking for the stable framework, Struts 2 is the right choice for you. On the other hand, if you’re looking for something robust, SpringMVC is perfect. Ensure that you review your exact requirements before choosing the framework! (尽管框架是一个很棒的选择,然而,如果你想寻找一个稳定的框架,Struts2是一个明智的选择。另一方面,如果你更倾向于健壮性,SpringMVC相对更好。在选择框架钱,请明确你的需求)