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 learn the new version of struts

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces you how to learn the new version of struts, the content is very detailed, interested friends can refer to, hope to be helpful to you.

The first thing to note is: don't abuse struts tags in web development-try to use html tags if you can use html tags, such as some basic page objects such as button,radio,label,text,textarea,table,form,select,submit, etc. Unless there are things that html tags can't do, such as logical judgment, traversal, comparison, etc., you need to use struts tags. In addition, to populate the view with data, you must also use struts tags, such as:

< bean:write name="" property="" />

(this is the label in the struts1.x version. I don't know what is in struts2.x? ).

In this way, if you need to migrate from the struts1.x version to the struts2.x version, there is no need to make significant changes in the page. In fact, I've developed so many websites that I haven't found anything that html tags can't do (except for some things that need to be logically handled in the background, but as long as they are simply used for display, html tags are enough! ).

This point of view also has a certain guiding significance for the learning of struts: the speed of struts upgrading is also very fast, if we have to learn everything, don't we have to pay attention to it all day? Therefore, for struts learning, you only need to pay attention to the following five aspects:

(1) study of tag library: as mentioned above, there are dozens of tags in struts tag library. We don't need to learn one by one, we just need to learn struts tags that need to be logically processed by the server (or the so-called background). For those tags that can be directly interpreted by the browser (that is, the so-called foreground), such as button,text,font,table,radio,form,select... If you don't need the struts tag instead, don't use it. In this way, the learning of struts tags can be reduced to the amount of learning of 4max 5!

(2) Action class learning: Action has always been the core of the struts framework, it is responsible for page rendering, so you need to deal with middleware, and therefore, it is the hub to connect the struts framework and other layers. With the foundation of struts1.x, learning the Action of struts2.x will be easy to master. Therefore, for programer with struts foundation, this part of learning is quite easy and easy to understand at a glance.

It is important to note that if you learn struts2.x directly, you may not know the formbean components in struts1.x. It is recommended that you try to understand these as much as possible, which will help you gain a deeper understanding of how the struts framework works.

(3) struts-config.xml configuration file: this is the core of struts-controller. Page jump and view data transmission all rely on it to control and direct. In struts1.x, you need to focus on understanding

< form-beans>

And

< action-mapping>

The principle, while using global forwarding, on the OK! But in struts2.x, because the view data is encapsulated in action, I don't know how it needs to be configured. But as long as you have mastered the configuration of struts1.x, struts2.x is not a problem, you can see it at a glance.

(4) collaboration between struts and spring: it seems that Ioc is built into struts2.x, but as its publisher said: if you can implement Ioc with spring, use spring as much as possible. Therefore, no matter how struts is updated, spring must be mastered.

(5) collaboration between struts and hibernate: as a classic combination of SSH, using struts and spring, how can you leave hibernate? As long as the programer who have previously configured hibernate, no matter how struts is upgraded or disguised, the configuration of hibernate is almost the same, and it will not be difficult because of changes in struts.

To sum up, when we learn about the new version of struts, we only need to know its tag library, Action, and configuration, and only a few tags with logical functions are enough in the tag library. Of course, if you give up HTML completely and insist on using the struts tag, take a little more time to take a look.

The difference between struts2.0 and struts1.x

However, in addition to the above three core and basic needs to master, there are some other improvements to the new version of struts. I found the following content on the Internet. It makes some comparisons between struts2.0 and struts1.x. If you take a look, you will understand why struts has to be upgraded (nothing more than to make it better):

I. A brief introduction to Struts2

* * although struts2 claims to be a brand new framework, it is only relative to struts1.

* * Struts2 does have a lot of revolutionary improvements compared to struts1, but it's not a newly released framework. Second, it is developed on the basis of another framework WebWork.

* * Struts2 does not inherit the blood of struts1, but inherits the blood of webWork.

* * struts2 is an upgrade of WebWork, not a new framework. Stability and performance are well guaranteed.

* * and absorbs the advantages of both struts1 and WebWork, so it is a very promising framework.

two。 Apache Struts2 is an extensible Java EE WEB framework: the goal of framework design runs through the development cycle.

three。 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 Struts2 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, any POJO with an execute logo

Objects can be used as Action objects 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 policy of the singleton limits

What Struts1 Action can do, and 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 is no thread safety problem.

* * Servlet dependency:

-Struts1 Action depends on Servlet API, because when an Action is called, HttpServletRequest and HttpServletResponse are passed to the execute () method

-Struts2 Action does not depend on the container, allowing Action to be tested separately from the container. Struts2 Action can still access the original request and response if needed

However, other elements reduce or eliminate the need for direct access to HttpServletRequest and HttpServletResponse

* * testability:

One of the main problems with Struts1 Action testing is that the execute method exposes Servlet API (making the test dependent on the container). An extension of a third party

Struts TestCase-provides a set of Struts1 mock objects (for testing)

-Struts2 Action can test dependency injection support by initializing, setting properties, and calling methods, which also makes testing easier.

* * capture input:

-Struts1 Action 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 that will result in redundant JavaBean)

-Struts2 Action directly uses the Action attribute as the input attribute, eliminating the need for the second input object. The Action property can be accessed through the

Taglibs visit. Struts2 also supports ActionForm mode

* * expression language:

-Struts1 integrates JSTL, so it uses JSTL EL. The basic object graph traversal of this EL, but the support for collection and index properties is weak.

-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):

-Struts1 uses the standard JSP mechanism to bind objects to the page for access

-Struts2 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 with attributes of the same column name but different types (view)

* * conversion of types:

The-Struts1 ActionForm attribute is usually of type String. Struts1 uses Commons-Beanutils for type conversion

One converter per class, which is not configurable for each instance

-Struts2 uses OGNL for type conversion. Provide converters for basic and commonly used objects

* * check:

-Struts1 supports manual verification in ActioNForm's validate method, or through Commons Validator extensions.

The same class can have different verification contents, but not child objects

Struts2 supports verification 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 chain checksum child attributes

* * controls performed by Action:

-Struts1 supports a separate Request Processors (life cycle) for each module; but all Action in the module must function for the same life cycle.

Struts2 supports creating a different life cycle for each Action through the interceptor stack (Interceptor Stacks).

The stack can be used with different Action as needed

Import the struts framework in the project:

Next, you need to know how to import the struts package in eclipse (myeclipse). The following is also how I copied it from the Internet to import struts2.0:

* * commons-logging-1.0.4.jar: log package of Struts2 framework

* * freemarker-2.3.8.jar: template for the UI tag of Struts2

* * ognl-2.6.11.jar: object navigation language pack

* * struts2-core.2.0.11.jar: core package of the Struts framework

* * xwork-2.0.4.jar: XWork class library, based on which Struts2 is built

Learn to use the new version of struts by doing a simple project

After learning the above, you have already understood it, but you may find something new in the project by deepening your impression through a project. The project is to create a simple login project based on Struts2:

1: import the Struts2 core support package

2: configure FilterDispatcher in web.xml

3: write the page displayed in the foreground

4: develop DAO

5: develop action

6: create struts.xml

Build a development platform

Of course, before developing this project, you also need to build a development platform:

1: install JDK (* is the green version) and create environment variables

2: install tomcat (* * is the green version) and create environment variables

3: install eclipse or myeclipse; if you install the former, you also need to download a tomcat-plugin and lomboz-plugin;, while the latter does not need

4: go back to the above project step *-import the struts core package (Note: if you install myeclipse, don't be fooled by the so-called built-in struts package! When developing a project, if the view layer uses the struts framework, you must import the struts package in the project's WEB-INF/lib/ directory)

On how to learn the new version of struts 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