SSH之Struts2 VS. SpringMVC



Struts 2 vs SpringMVC - Battle of the Frameworks - Isos Technology

Over the years in my work as a consultant, I’ve worked with several companies that were looking to convert their legacy systems to a web-based solution. Once they selected Java (great choice!) as their language, their next question is usually, “What Java framework should we use?”

In my experience, it usually comes down to Struts 2 or SpringMVC. I have worked extensively with both of these frameworks, so I will compare and contrast them for you. Keep in mind that there really isn’t a definitive “correct” framework to choose. You should look at your options, the dynamics of your development team and what you are trying to accomplish before you choose a framework. I’m hoping that this will help guide you in your decision making.

SpringMVC

Spring is basically a Java framework that allows you to create complex web applications through simple components and configurations. There are other aspects of Spring that you can use independently of SpringMVC, such as dependency injection and aspect-oriented programming. For the purposes of this article, we don’t need to talk about those because they can be used with SpringMVC or Struts 2. In my experience, they work really well with either.

SpringMVC is part of the Spring framework that is used for handing and processing web-based requests. It follows the basic MVC (Model-View-Controller) pattern. Spring-created JavaBeans are the model. The controller is the DispatcherServlet, which handles all requests and passes them to the corresponding controller. A nice feature is that you can use annotations to handle this requirement instead of using the infamous application-context.xml file. Anyone who used Spring in the early days knows that this file can get large quickly. Breaking this context file into smaller files just makes it more complicated. Anyway, the last piece is the view, which is usually composed of JSPs. However, if you are developing webservices the view could be a number of things. We will come back to webservices later, but this is your basic layout of Spring and its components.


Struts 2

Struts 2 is also a Java request-based framework that allows you to create web applications. The Struts 2 framework follows the basic MVC pattern. The controller is called the ActionController. It is backed by mappings in a struts.xml file that allows requests to be forwarded to the appropriate action. In my experience, most people use EJBs or even Spring JavaBeans as their model. However, the view is important here because Struts 2 has its own view language. This is an advantage over SpringMVC in that you can just learn Struts 2 and you can build a Java framework from beginning to end, which helps with your development team’s learning curve. I have found that companies looking to ramp up quickly will go the Struts 2 route because it is easier to get up and running.

Now that we have a basic understanding of the frameworks, let’s compare and contrast the frameworks using a few of the web components and possible issues you would come across while learning and developing using these frameworks.


Ajax Support

Ajax has become a good technology for displaying dynamic content or field validation for web-based applications. In my work as a consultant, I have been fortunate to work with Ajax in both SpringMVC and Struts 2 environments. Struts 2 has built-in Ajax support so there is no need to import a third-party library. So if you are keeping score, Struts 2 has its own view language, controller and Ajax support. Again, once your learn Struts 2 you can pretty much develop a web application from beginning to end without the assistance of almost any other framework.

Although SpringMVC does not have native Ajax support, this just gives a more seasoned developer the ability to choose its Ajax library. This is especially important since your development team may already have knowledge or a preference for an Ajax library, allowing you to leverage that knowledge. For example, in one company that was switching to a SpringMVC environment, many of the developers were familiar with the Dojo Ajax library. So instead of switching to Struts 2 and learning a new Ajax library, they were able to use all the knowledge of Dojo and make the full learning curve of switching to SpringMVC a little easier. I would give the edge to Struts 2 here because it’s easier to hit the ground running, provided that your developers do not have any experience with Ajax libraries.


RESTful Webservices Support

In any request-based web application, it is becoming increasing popular to use RESTful webservices because they are easier to develop and maintain and startup costs are minimal. In a world where IT is seen more as a cost than an expense, this is pretty important. Most developers I have come across don’t know how RESTful webservices work; they just know how to write them. It has really become that simple.

In Struts 2, I found it a little difficult to set up RESTful webservices when I first learned it. We essentially needed 2 separate xml files (one for RESTful URLs and the re-direct and the second for the struts action mapping). Another hassle is that in your Java Action you don’t necessarily know that this is a RESTful webservice or if it is just a plain old Struts Action. This can be confusing to some less experienced developers.

In SpringMVC, RESTful webservices take advantage of annotations which not only makes webservices easier to identify, it also makes them easier to develop and maintain. The majority of your changes all happen in the Java class and you just have to touch one xml file once as opposed to Struts 2 where you need to edit 3 different places for adding or removing webservice. I really like the RESTful webservice support in SpringMVC with an assist from annotations because everything is one place and therefore development time is shorter.
Continuing Maintenance and Evolving Framework

Java in general is constantly evolving and its frameworks are no different. They are both open source so improvements and efficiencies are always on the horizon. I find that Struts 2 is a more mature technology in that the changes to the basic framework have been minimal. SpringMVC, and along with it Spring, is constantly changing and getting better. The issue is that your developers will have to constantly keep up with the technology in order to improve the application as Java grows, web browsers change and other improvements happen around the webspace. It only sounds daunting because most IT firms do not take this into account. They only care about fixing bugs and enhancing the application. Eventually, old technologies will die off and new technologies and techniques that make web applications perform more efficiently will replace them. In this regard, SpringMVC applications must be constantly monitored for improvements and upgrades are a must. This is less important for Struts 2 because the underlying framework is not changing much. So, something else to think about when choosing between SpringMVC and Struts 2 is the maintenance costs and how much your department can afford.


In Conclusion

These are just some of the things you can use when deciding between SpringMVC and Struts 2. Like I said in the beginning, there isn’t really a definitive answer to which one is better. However, in my opinion, I would say if you are looking for a stable framework you should choose Struts 2. If you are looking for a more robust framework, you should choose SpringMVC.



----------------------------------------------------------------------

The Difference between STRUTS 2 and SPRING MVC


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?

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”.

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.

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.


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.

Struts 2 features

    - 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.

    - POJO based actions. Struts 2 action class is Plain Old Java Object, which prevents developers to implement any interface or inherit any class.

    - 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.

    - Support for integration with Hibernate, Spring, Tiles and so on.

    - Whether you want to use JSP, freemarker, velocity or anything else, you can use different kinds of result types in Struts 2.

    - You can also leverage from various tags like UI tags, Data tags, control tags and more.

    - Brings ample support for theme and template. Struts 2 supports three different kinds of themes including xhtml, simple and css_xhtml.


On the other hand, Spring MVC framework brings totally different set of features.

Spring MVC features

    - 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.

    - Leverage from the adaptability, non-intrusiveness and flexibility with the help of controller method signature.

    - Now use existing business objects as command or form object rather than duplicating them to extend the specific framework base class.

    - Customizable binding and validation will enable manual parsing and conversion to business objects rather than using conventional string.

    - Flexible mode transfer enables easy integration with the latest technology.

    - Customizable locale and theme resolution, support for JSPs with or without Spring tag library for JSTL and so on.

    - 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.


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.

Both of these frameworks have their own set of pros and cons associated with it.

Struts framework brings a whole host of benefits including:


    - Simplified design

    - Ease of using plug-in

    - Simplified ActionForm & annotations

    - Far better tag features

    - OGNL integration

    - AJAX Support

    - Multiple view options and more

However, the only drawback with Struts 2 framework is that it has compatibility issues and poor documentation.



On the other hand, Spring MVC provides benefits like:

    - Clear separation between controllers, JavaBeans models and views that is not possible in Struts.

    - Spring MVC is more flexible as compared to the Struts.

    - Spring can be used with different platforms like Velocity, XLST or various other view technologies.

    - There is nothing like ActionForm in Spring, but binds directly to the domain objects.

    - Code is also more testable as compared to the Struts.

    - It is a complete J2EE framework comprising of seven independent layers, which simplifies integration with other frameworks.

    - 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.


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!


















-
http://www.cygnet-infotech.com/blog/struts-2-vs-springmvc






-

猜你喜欢

转载自lixh1986.iteye.com/blog/2355845