What is the difference between struts2 and strut1? What are the advantages of struts2?

Source: Internet
Author: User
Tags i18n xslt

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 struts 2 Action class can implement an action interface or other interfaces to make optional and customized services possible. Struts2 provides an actionsupport base class to implement common interfaces. The action interface is not required. Any pojo object with the execute identifier can be used as the action object of struts2.

Thread mode:
• Struts1 action is a singleton mode and must be thread-safe, because only one instance of action is used to process all requests. The Singleton policy limits what struts1 actions can do and requires caution 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. (In fact, the servlet container generates many discarded objects for each request without causing performance and garbage collection problems)

Servlet dependency:
• The struts1 action depends on the servlet API, because when an action is called, httpservletrequest and httpservletresponse are passed to the execute method.
• Struts 2 action does not depend on the container, allowing the action to be tested independently from the container. If necessary, struts2 action can still access the initial request and response. However, other elements reduce or eliminate the need to directly access httpservetrequest and httpservletresponse.

Testability:
• A major problem in testing Struts1 Action is that the execute method exposes the servlet API (which makes the test dependent on the container ). A third-party extension, Struts TestCase, provides a set of Struts1 simulated objects for testing ).
• Struts 2 Action can be tested through initialization, setting attributes, and calling methods. "dependency injection" also makes testing easier.

Capture input:
• Struts1 uses the ActionForm object to capture input. All actionforms must inherit a base class. Because other JavaBean cannot be used as an ActionForm, developers often create redundant class capture inputs. Dynamic beans (DynaBeans) can be used as an option to create a traditional ActionForm. However, developers may re-describe (create) the existing JavaBean (which will 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 attribute may be a rich object type with its own (sub) attribute. The Action attribute can be accessed through taglibs on the web page. Struts2 also supports the ActionForm mode. Rich object type, including business objects, which can be used as input/output objects. This ModelDriven feature simplifies taglib's reference to POJO input objects.

Expression Language:
• Struts1 integrates JSTL and therefore uses jstl el. This kind of EL has basic object graph traversal, but the support for set and index attributes is weak.
• Struts2 can use JSTL, but also supports a stronger and more flexible Expression Language-"Object Graph Notation Language" (OGNL ).

Bind the value to the page (view ):
• Struts 1 uses the standard JSP mechanism to bind objects to pages for access.
• Struts 2 uses the "ValueStack" technology to enable taglib to access values without binding your page (view) and objects. The ValueStack policy allows you to reuse a page (view) through a series of attributes with the same name but different types ).
 
Type conversion:
• The Struts 1 ActionForm attribute is generally of the String type. Struts1 uses Commons-Beanutils for type conversion. One converter for each class is not configurable for each instance.
• Struts2 uses OGNL for type conversion. Provides Converters for basic and common objects.

Verification:
• Struts 1 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 through the validate method and XWork verification framework. The XWork verification framework uses the checksum defined for the attribute class type to support the chain checksum subattribute.

Control of Action execution:
• Struts1 supports a separate Request Processors (lifecycle) for each module, but all actions in the module must share the same lifecycle.
• Struts2 supports creating different lifecycles for each Action through Interceptor Stacks. The stack can be used with different actions as needed.
Struts. action. extension
The URL extension to use to determine if the request is meant for a Struts action
Use the URL extension to determine whether the request is used as a Struts action, that is, to set the action suffix, such as the 'do 'word of login. do.

Struts. configuration
The org. apache. struts2.config. Configuration implementation class
Org. apache. struts2.config. Configuration Interface Name

Struts. configuration. files
A list of configuration files automatically loaded by Struts
A list of configuration files automatically loaded by struts

Struts. configuration. xml. reload
Whether to reload the XML configuration or not
Whether to load xml configuration (true, false)

Struts. continuations. package
The package ining actions that use Rife continuations
Complete continuous package name containing actions

Struts. custom. i18n. resources
Location of additional localization properties files to load
Load the additional international property file (excluding the. properties suffix)

Struts. custom. properties
Location of additional configuration properties files to load
Location of the attached configuration file

Struts. devMode
Whether Struts is in development mode or not
Struts development mode?

Struts. dispatcher. parametersWorkaround
Whether to use a Servlet request parameter workaround necessary for some versions of WebLogic
(For weblogic of some versions) whether to use a servlet Request Parameter workspace (PARAMETERSWORKAROUND)

Struts. enable. DynamicMethodInvocation
Allows one to disable dynamic method invocation from the URL
Allow dynamic method calls

Struts. freemarker. manager. classname
The org. apache. struts2.views. freemarker. FreemarkerManager implementation class
Org. apache. struts2.views. freemarker. FreemarkerManager Interface Name

Struts. i18n. encoding
The encoding to use for localization messages
International Information internal code

Struts. i18n. reload
Whether the localization messages showould automatically be reloaded
Whether to automatically load International Information

Struts. locale
The default locale for the Struts application
Default International region information

Struts. mapper. class
The org. apache. struts2.dispatcher. mapper. ActionMapper implementation class
Org. apache. struts2.dispatcher. mapper. ActionMapper Interface

Struts. multipart. maxSize
The maximize size of a multipart request (file upload)
Maximum size of multipart request information (for file upload)

Struts. multipart. parser
The Org. Apache. struts2.dispatcher. multipart. multipartrequest parser implementation for a multipart request (File Upload)
Org. Apache. struts2.dispatcher. multipart. multipartrequest parser interface for multipart request information (for file upload)

Struts. multipart. savedir
The directory to use for storing uploaded files
Set the directory folder for storing uploaded files

Struts. objectfactory
The com. opensymphony. xwork2.objectfactory implementation class
Com. opensymphony. xwork2.objectfactory interface (spring)

Struts. objectfactory. Spring. autowire
Whether spring shoshould autowire or not
Automatically bound to spring?

Struts. objectfactory. Spring. useclasscache
Whether spring shocould use its class cache or not
Whether spring should use its own Cache

Struts. objecttypedeterminer
The com. opensymphony. xwork2.util. objecttypedeterminer implementation class
Com. opensymphony. xwork2.util. ObjectTypeDeterminer Interface

Struts. serve. static. browserCache
If static content served by the Struts filter shocould set browser caching header properties or not
Whether the static content provided by the struts filter should be cached by the browser in the header attribute

Struts. serve. static
Whether the Struts filter shocould serve static content or not
Does struts filter provide static content?

Struts. tag. altSyntax
Whether to use the alterative syntax for the tags or not
Can I replace tags with an alternative syntax?

Struts. ui. templateDir
The directory containing UI templates
UI templates directory folder

Struts. ui. theme
The default UI template theme
Default UI template topic

Struts. url. http. port
The HTTP port used by Struts URLs
Set http port

Struts. url. https. port
The HTTPS port used by Struts URLs
Set https Port

Struts. url. includeParams
The default includeParams method to generate Struts URLs
Generate the default includeParams in the url

Struts. velocity. configfile
The velocity configuration file path
Velocity configuration file path

Struts. Velocity. contexts
List of velocity context names
Context list of Velocity

Struts. Velocity. Manager. classname
Org. Apache. struts2.views. Velocity. velocitymanager implementation class
Org. Apache. struts2.views. Velocity. velocitymanager Interface Name

Struts. Velocity. toolboxlocation
The location of the Velocity toolbox
Location of the velocity toolbox
Struts. xslt. nocache
Whether or not XSLT templates shocould not be cached
Whether the XSLT template should be cached
In Struts2, The getXXX () in action is executed only once when the server is started. The data displayed on a refresh page is completely lost. The solution to this problem is:
When configuring struts2.0 in web. xml,
<Filter-mapping>
<Filter-name> action2 </filter-name>
<Url-pattern>/* </url-pattern>
</Filter-mapping>
Change/* to *. ation.
1. Struts2 configuration file
Configuration files related to struts2 include web. XML, Struts. XML, Struts. properties,
Struts-default.xml, velocity. properties, struts-default.vm. Among them, Web. xml and Struts. XML are required, and other configuration files can be selected. Their functions in web applications are as follows:
Web. xml: contains the Web deployment descriptor of all required framework components.
Struts. xml: configure the main configuration file of struts2 that contains the result/view type, Action ing, interceptor, and so on.
Struts. properties: configure the framework attributes of struts2.
Struts-default.xml: In the struts-action-x.x.jar, the file is the default configuration that should be included in struts. xml.
Welocity. properties: overwrites the velocity configuration file.
Struts-default.vm: Default Configuration relative to velocity.

Ii. struts2 configuration element
The core configuration file of struts2 is the default struts. xml.
When necessary, the default configuration file can contain other configuration files. The struts file can be placed in the jar file and automatically inserted into the application, in this way, each module can contain its own configuration file and be automatically configured. In the freemarker and velocity modules, templates can also be loaded from classpath, so the entire module can be included as a simple jar file.
The Struts. xml configuration file can contain interceptor, action class, and results.
Struts. xml configuration element description:

1. Packages
Packages: packages actions, results, results types, interceptors
And interceptor-stacks are assembled into a logical unit. In terms of concept, packages are like an object, which can be written by other sub-packages and have its own independent parts.
The Name attribute is a mandatory element of packages. It is referenced by the package behind the keyword. The extends element is optional and allows the package to expand one or more packages defined by the front. Note,
The struts. xml file is processed in the top-down mode. All extended packages must be defined before the extended packages.
Abstract is optional. It can declare a configuration file that does not contain actions.

2. Namespace
The Namespace element segments actions into logic modules. Each namespace has its own
Prefix (prefix), namespace avoids name conflicts between actions. Currently, when these tags are suffixed with Uris, they are all namespace aware ("namespace aware "), therefore, these namespace prefixes do not need to be embedded into forms or connections.
The Default namespace is an empty string "". If no action is found in the specified configuration file, the Default namespace is also searched. The Local/global policy allows the application to have a global action configuration out of the action "extends" element hierarchy. The default namespace cannot be stated in the package.
Namespace prefix can be registered as java declarative security to ensure that authorized users can access namespace resources.
Root namespace ("/") is also supported. root is the namespace when the context path is directly requested.

[Personal understanding: the usage of namespace is the same as that of path in struts1.x, except that it specifies the path in the package and declares the action name in the action.]

3. Include
The Include element allows the framework to apply "divide and conquer" to the configuration file. Include
Each configuration file must be in the same format as struts. xml. A large project can organize program modules in this way.
The Include element can also appear in turn with the package. The framework loads the configuration file in sequence.

4. Interceptor configuration
Interceptor allows applications to define and execute code before and after the Action method is executed,
Interceptor is very important in application development. There are many use cases for Interceptor, such as validation, property population, security, logging, and profiling.
Interceptor is defined as a Java class. Interceptor can also be assembled into Interceptor-stack, which will be executed in the defined order.
Some default Interceptor-stacks are defined in the struts-default.xml so that the framework can run well.

5. Action
Action is the "unit of work" of the framework ". Action can specify an Interceptor-stack,
Return type of a sequence and exception handling of a sequence, but only the name attribute is required.

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.