Struts-Struts is being upgraded. As a programmer, how can we keep it unchanged?

Source: Internet
Author: User

Struts is being upgradedProgramClerk, how should I keep it unchanged?

How to learn the new version of struts?

Today, I accidentally looked at struts2.0, and found that it has changed significantly with struts1.1. It can be said that it has become completely different. As a programmer, We must master the latest technology. Therefore, as soon as it is upgraded, we have to learn it! However, the flood of water and the Earth are standing in the dark. You have the 18 ops of the dragon, and I have a great game to move. Let's look at the moves to keep them unchanged. Whether you are a mountain or a spider, in my eyes, it's just a bunch of bones!

First, it should be noted that in web development, it is best not to misuse struts labels-HTML tags should be used whenever possible, such as some of the most basic page objects such as button, radio, label, text, textarea, table, form, select, submit, etc. Unless HTML tags cannot do things, such as logical judgment,Struts labels are required only for traversal and comparison. In addition, you must use the struts tag to fill the data in the view. For example, <Bean: write name = "" property = ""/> (this is the tag in struts1.x, I don't know what is in struts2.x ?).

In this way, if you want to migrate from struts1.x to struts2.x, you do not need to make any significant changes on the page. In fact, I have developed so many websites that no HTML tag can do anything.

Things (except for some things that require background logic processing, but as long as it is used only for display, HTML tags are sufficient !) .

This viewpoint also has certain guiding significance for struts's learning: struts is also updated quickly. If we want to learn everything, isn't it necessary to pay attention to it all day long? Therefore, you only need to pay attention to the following five aspects for struts learning:

(1) learning the tag Library: As mentioned above, there are dozens of tags in the struts tag library. We don't have to learn them one by one. We just need to learn about the servers (or the so-called backend) struts labels for logical processing, and labels that can be directly interpreted by browsers (the so-called front-end), such as button, text, Font, table, radio, form, select ......, Instead of using struts labels, you 'd better not use them. In this way, Struts labels can be learned less than 4/5!

(2) Learning Action: Action has always been the core of the Struts framework. It is responsible for page presentation, so it needs to deal with middleware. Therefore, it is the hub connecting the Struts framework with other layers. With the foundation of struts1.x, learning struts2.x actions is easy to understand. Therefore, for the programer with the struts Foundation, this part of learning is quite easy to understand.

Note that if you directly learn struts2.x, you may not know the formbean component in struts1.x. We recommend that you take a look at this as much as possible to help you better understand the working principles of the Struts framework.

(3) struts-config.xml configuration file: This is the core of Struts-controller. Page navigation and view data transmission all rely on it for control and command. In struts1.x, you need to understand the principles of <form-beans> and <action-mapping> and use global forwarding! However, in struts2.x, View data is encapsulated in action, so I don't know how to configure it. But as long as you have mastered the configuration of struts1.x, struts2.x is not a problem. You can understand it at a glance.

(4) struts and spring collaboration: It seems that struts2.x has built-in IOC, but just as its publisher said: If you can use spring to implement IOC, try to use spring. Therefore, no matter how struts is updated, spring must be mastered.

(5) Collaboration between struts and Hibernate: As a classic SSH combination, how can I leave hibernate with Struts and spring? As long as the programer who has previously configured hibernate, no matter how struts is upgraded in disguise, the configuration of Hibernate is almost the same, it will not become difficult because of struts changes.

To sum up, when learning the new version of struts, we only need to know its tag library, action, and configuration, what you need to know in the tag library is that only a few tags with logical functions are enough. Of course, if you give up HTML completely and have to use the struts tag, you should spend more time looking at it.

Differences between struts2.0 and struts1.x

However, in addition to the above three most basic core needs to be mastered, for the new version of struts, you also need to know some other improvements, the following content I found on the Internet, it makes some comparisons between struts2.0 and struts1.x. Let's take a look at why struts has been upgraded (nothing more than making it more complete ):

1. Introduction to struts2
** Although struts2 is a brand new framework, it is only relative to struts1.
** Compared with struts1, struts2 does have a lot of revolutionary improvements, but it is not a new framework released. Second, it is developed based on the webwork of another framework.
** Struts2 does not inherit the lineage of struts1, but inherits the lineage of webwork.
** Struts2 is a webwork upgrade, rather than a brand-new framework. It guarantees stability and performance.
** It also absorbs the advantages of struts1 and webwork. Therefore, it is a very promising framework.
Ii. Apache struts2 is a scalable Java EE Web Framework : Framework Design goals run through the entire development cycle
Iii. Differences between struts2 and struts1
** Action class:
-Struts1 requires the action class to inherit an abstract base class. A common problem with struts1 is the use of abstract class programming rather than interfaces.
-The struts2 action class can implement an action interface or other interfaces, making optional and customized services possible.
-Struts2 provides an actionsupport base class to implement common interfaces. Action interfaces are not mandatory and any pojo with the Execute id
All objects can be used as struts2 action objects
** Thread mode:
-Struts1 action is a singleton mode and must be thread-safe, because only one instance of action can process all requests. The Singleton policy limits
What struts1 action can do, and be especially careful during development. Action resources must be thread-safe or synchronized.
-The struts2 action object generates an instance for each request, so there is no thread security problem.
** Servlet dependency:
-Struts1 action depends on the servlet API, because when an action is called, httpservletrequest and httpservletresponse are passed to the execute () method.
-The struts2 action does not depend on the container and allows the action to be tested independently from the container. If necessary, the struts2 action can still access the initial request and response.
However, other elements reduce or eliminate the need to directly access httpservletrequest and httpservletresponse.
** Testability:
-A major problem in struts1 action testing is that the execute method exposes the servlet API (so that the test depends on the container). A third-party Extension
Struts testcase -- provides a set of struts1 simulated objects for testing)
-The struts2 action can be used to test the "dependency injection" support by initializing, setting properties, and calling methods, which makes the test easier.
** Capture input:
-The struts1 Action uses the actionform object to capture the input. All actionforms must inherit a base class, because other JavaBean cannot be used
Actionform, developers often create redundant classes to capture input. dynamic beans (dynabeans) can be used to create traditional actionform options. However, developers may re-describe (create) the existing JavaBean, leading to redundant JavaBean)
-Struts2 action directly uses the action attribute as the INPUT attribute, eliminating the need for the second input object. The action attribute can be passed through
Taglibs access. struts2 also supports the actionform Mode
** Expression language:
-Struts1 integrates jstl. Therefore, the basic object graph traversal of El is used, but the support for set and index attributes is weak.
-Struts2 can use jstl, but also supports a more powerful and flexible Expression Language-"object graph notation language" (ognl)
** Bind the value to the page (View ):
-Struts1 uses the standard JSP mechanism to bind objects to pages for access
-Struts2 uses the "valuestack" technology to enable taglib to access values without binding your page (View) to objects.
The valuestack policy allows you to reuse a page (View) with the same column name but different types of attributes)
** Type conversion:
-The struts1 actionform attribute is usually string type. struts1 uses commons-beanutils for type conversion.
One converter for each class is not configurable for each instance
-Struts2 converts data types using ognl. It provides basic and common object converters.
** Verification:
-Struts1 supports manual verification in the validate method of actionform, or through commons validator extension.
The same class may have different verification content, but it cannot verify the child object.
-Struts2 supports verification by using the validate method and xwork verification framework. The xwork verification framework uses the validation content defined for the attribute type.
To support the chain checklist subattribute.
** Control of action execution:
-Struts1 supports a separate request processors (lifecycle) for each module. However, all actions in the module must have the same effect on the same lifecycle.
-Struts2 supports creating different lifecycles for each action through interceptor stacks.
Stack can be used with different actions as needed

Import the Struts framework to the project:

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

** Commons-logging-1.0.4.jar: log package for struts2 framework
** Freemarker-2.3.8.jar: struts2 UI tag Template
** Ognl-2.6.11.jar: Object navigation Language Pack
** Struts2-core.2.0.11.jar: Struts framework core package
** Xwork-2.0.4.jar: xwork class library, struts2 built based on this

Learn to use the new struts version by creating a simple project

I have understood all the above, but it is best to deepen my impression through a project practice. Maybe you will find new discoveries in the project! This project is: create a simple login project based on struts2:

1: import the struts2 core support package
2: Configure filterdispatcher in Web. xml
3: Compile the front-end display page
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 (preferably in the green version) and create environment variables;

2: install Tomcat (preferably in 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;

4: Go back to the first step of the above project-import the struts core package (Note: If myeclipse is installed, do not be fooled by its so-called built-in struts package! When developing a project, if the view layer uses the Struts framework, you must import this struts package in the project's WEB-INF/lib/directory)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.