Struts ===

Source: Internet
Author: User

Struts2 developed based on webwork2. Like struts1, struts2 also belongs to the MVC framework. However, it should be noted that, although the name differences between struts2 and struts1 are not very large, the Code style of struts2 and struts1 is almost different. So why sturts2?
1> in software design, struts2 does not have close coupling between servletapi and sturtsapi like struts1. The application of struts2 does not depend on servletapi and strutsapi. This design of struts2 is a non-intrusive design, struts1 is an intrusive design.
Public class orderlistaction extends action
{
Public actionforward execute (actionmapping mapping, actionform form, httpservletrequest request, httpservletresponse response) throws exception {

}
}
2> struts2 provides an interceptor that can be programmed with AOP to implement the permission Interception Function.
3> struts2 provides a type converter. We can convert special request parameters to the desired type. In struts1, if we want to implement the same function, you must register the type converter with the underlying implementation beanutil of struts1.
4> struts2 supports multiple presentation layer technologies, such as JSP, freemaiker, and velocity.
5> input validation of struts2 can verify the specified method, solving the long-term pain of struts1.
6> provides global resource file management for the scope of packages and actions.

Build struts2 Development Environment
When building the struts2 environment, we generally need to do a few steps.
1. Find the JAR file required to develop the struts2 application.
2. Compile the struts2 configuration file
3. Add sturts2 MVC Framework STARTUP configuration to Web. xml
Build the struts2 development environment-develop the JAR file on which the struts2 application depends
Struts1-core-2.x.x.jar: Core class library of struts2 framework

Xwork-2.x.x.jar: xwork class library, struts2 in its build
Ognl-2.6.x.jar: Object map navigation language, struts2 framework through its Read and Write Object Attributes
Freemarker-2.3.x.jar: struts2 UI tag template written using freemarker
Commons-logging-1.1.x.jar: A log package produced by ASF, which is used by the struts2 framework to support logging of log4j and jdk1.4
Commons-fileupload-1.2.1.jar: File Upload Component, which must be added after version 2.1.6
Configuration file of the struts2 Application
The default configuration file of struts2 is struts. XML, which must be stored in Web-INF/classes. The configuration template of this file is as follows:
Struts2 STARTUP configuration in the Web
In struts1.x, the Struts framework is started through servlet. In struts2, the Struts framework is started through filter. Its configuration in Web. XML is as follows:
In the init () method of strutsprepareandexecutefilter, the default configuration file struts. XML in the class path is read to complete initialization.
Note: struts2 reads struts. the XML content is stored in the memory in the form of JavaBean. In the future, struts2 will use the data in the memory for each request processing, instead of reading struts every time. XML file
Introduction to packages in struts. xml configuration
In the struts2 framework, packages are used to manage actions. packages are similar to class packages in Java. They are mainly used to manage actions that are first off a set of business functions. In actual applications, we should put a group of actions related to business functions. In actual applications, we should put a group of actions related to business functions under the same package.
When configuring a package, you must specify the name attribute. This attribute value can be named at will, but must be unique. It does not correspond to a Java class package. If other packages want to inherit this package, the namespace attribute of the package is used to define the namespace of the package. The namespace serves as a part of the path to access the action under the package, such as accessing the action in the preceding example, the access path is/test/helloworld. action. The namespace attribute can be left unspecified. In this example, if this attribute is not specified, the default namespace is "". (Null String)
Generally, each package should inherit the Struts-default package, because many core functions of struts2 are implemented by the interceptor, for example, encapsulating request parameters to actions in requests, file Upload and data verification are implemented through the Interceptor. In this case, the core functions provided by struts2 can be used only when the package inherits Struts-default, the Struts-default package is defined in the strust2-core-2.x.x.jar in the struts-default.xml file. The struts-default.xml is also the struts2 default configuration file. Struts2 automatically loads the struts-default.xml file each time.
A package can also be defined as an abstract package through abstract = "true". An abstract package cannot contain actions.

Search Order of action names
1. Obtain the URI of the Request Path. For example, the URL is http: // server/struts2/path1/path2/path3/test. Action.
2. First, find the package whose namespace is/path1/path2/path3. If the package does not exist, perform Step 3: If the package exists, search for the action named test in this package, when no action is found in the package, the action is directly searched in the package of the default namespace (the default namespace is an empty string ), if the action cannot be found in the package of the default namespace, the page prompts that the action cannot be found.

 

<Package name = "itcast" namespace = "/test" extends = "struts-Default">
<Action name = "helloworld" class = "cn. itcast. Action. helloworldaction" method = "execute">
<Result name = "success">/WEB-INF/page/Hello. jsp </result>
</Action>
</Package>
1. If no class is specified for action, actionsupport is used by default.
2. If no method is specified for the action, the execute () method in the action is executed by default.
3. If the name attribute of result is not specified, the default value is success.

Struts ===

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.