Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to analyze Struts2

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces you how to analyze Struts2, the content is very detailed, interested friends can use for reference, hope to be helpful to you.

Introduction to Struts2

Although Struts2 claims to be a completely new framework, it is only relative to Struts 1. Struts 2 does have a lot of revolutionary improvements over Struts 1, but it is not a newly released new framework, but it is based on another famous framework: WebWork. To some extent, Struts2 does not inherit the bloodline of Struts 1, but inherits the bloodline of WebWork. In other words, WebWork derived Struts2, rather than Struts 1 derived Struts2. Because Struts2 is an upgrade of WebWork, not a brand-new framework, stability, performance and other aspects are well guaranteed. Moreover, it absorbs the advantages of both Struts 1 and WebWork, so it is a framework to look forward to.

Apache Struts2 is an elegant and extensible JAVA EE web framework. The goal of framework design runs through the whole development cycle, from development to release, including the whole process of maintenance.

Apache Struts 2 is known as WebWork 2. After several years of respective development, the WebWork and Struts communities decided to merge into one, known as Struts 2.

The difference between Struts2 and Struts1

Action class:

◆ Struts1 requires the Action class to inherit an abstract base class. A common problem with Struts1 is programming with abstract classes rather than interfaces.

The ◆ Struts 2 Action class can implement an Action interface as well as other interfaces, making optional and customized services possible. Struts2 provides an ActionSupport base class to implement common interfaces. The Action interface is not required, and any POJO object identified by execute can be used as an Action object for Struts2.

Thread mode:

◆ Struts1 Action is singleton and must be thread-safe because there is only one instance of Action to handle all requests. The singleton policy limits what Struts1 Action can do, and you have to be very careful when developing. Action resources must be thread-safe or synchronized.

The ◆ Struts2 Action object generates an instance for each request, so there are no thread safety issues. (in fact, the servlet container produces many disposable objects for each request and does not cause performance and garbage collection problems.)

Servlet dependencies:

◆ Struts1 Action depends on Servlet API because HttpServletRequest and HttpServletResponse are passed to the execute method when an Action is called.

◆ Struts 2 Action does not depend on the container, allowing Action to be tested separately from the container. If necessary, Struts2 Action can still access the original request and response. However, other elements reduce or eliminate the need for direct access to HttpServetRequest and HttpServletResponse.

Testability:

One of the main problems with ◆ testing Struts1 Action is that the execute method exposes servlet API (which makes the test dependent on the container). A third-party extension-Struts TestCase-- provides a set of Struts1 mock objects (for testing).

◆ Struts 2 Action can be tested by initializing, setting properties, and calling methods, and dependency injection support makes testing easier.

Capture input:

◆ Struts1 uses the ActionForm object to capture input. All ActionForm must inherit a base class. Because other JavaBean cannot be used as ActionForm, developers often create redundant classes to capture input. Dynamic Bean (DynaBeans) can be used as an option for creating a traditional ActionForm, but developers may be redescribing (creating) an existing JavaBean (which can still lead to redundant javabean).

◆ Struts 2 directly uses the Action attribute as the input attribute, eliminating the need for the second input object. The input property may be a rich object type with its own (child) properties. The Action property can be accessed through taglibs on the web page. Struts2 also supports ActionForm mode. Rich object types, including business objects, can be used as input / output objects. This ModelDriven feature simplifies taglib's reference to POJO input objects.

Expression language:

◆ Struts1 integrates JSTL, so it uses JSTL EL. This EL has basic object graph traversal, but weak support for collection and index properties.

◆ Struts2 can use JSTL, but it also supports a more powerful and flexible expression language, "Object Graph Notation Language" (OGNL).

Bind values to the page (view):

◆ Struts 1 uses the standard JSP mechanism to bind objects to the page for access.

◆ Struts 2 uses "ValueStack" technology to enable taglib to access values without having to bind your view to objects. The ValueStack policy allows pages to be reused (view) through a series of attributes with the same name but different types.

Type conversion: the ◆ Struts 1 ActionForm attribute is usually of type String. Struts1 uses Commons-Beanutils for type conversion. One converter per class is not configurable for each instance.

◆ Struts2 uses OGNL for type conversion. Provides converters for basic and commonly used objects.

Validation: ◆ Struts 1 supports manual verification in the validate method of ActionForm, or verification through an extension of Commons Validator. The same class can have different validation contents, but not child objects.

◆ Struts2 supports validation through the validate method and the XWork verification framework. The XWork validation framework uses the checksum content checksum defined for the attribute class type to support the control performed by the chain validation child attribute Action: ◆ Struts1 supports a separate Request Processors (life cycle) for each module, but all Action in the module must share the same life cycle.

◆ Struts2 supports the creation of a different life cycle for each Action through the interceptor stack (Interceptor Stacks). The stack can be used with different Action as needed.

On how to analyze Struts2 to share here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report