Learn Struts (2.3)

Source: Internet
Author: User

Learn Struts (2.3)
1. struts2.3 core jar package (if you contact me for the first time, you can skip it for the moment)
You can go to workshop. After download
Decompress the file, and the jar files needed to develop the struts2 application are in the lib folder of the decompressed directory. Different applications require different JAR packages. The following section describes the development process.
The minimum JAR required by Struts 2.
Struts2-core-2.x.x.jar: Struts 2 framework core class library
Xwork-2.x.x.jar: XWork class library, Struts 2 built on it, because Struts2 is extended by xwork some classes are still associated with the xwork class
Ognl-3.0.x.jar: Object Graph Navigation Language, supporting ognl expressions, struts2 framework through its Read and Write Object Attributes
Freemarker-2.3.x.jar: FreeMarker is a template engine, a common tool for generating text output based on templates, Struts 2 UI tag templates use
Writing FreeMarker
Commons-logging-1.1.x.jar: the log package produced by ASF, Struts 2 framework uses this log package to support Log4J and JDK 1.4 + logging.
Commons-fileupload-1.3.1.jar File Upload Component, which must be added after version 2.1.6
To integrate other frameworks, you need to add the appropriate xxx-plugin.jar such:
To integrate spring, import the jar package]. Struts2-spring-plugin.jar --- spring plug-in of struts2

Related Terms --------
1. jakarta: jakarta is the name of the open-source software for a set of Java solutions under the apache organization. It includes many sub-projects. Tomcat, ant, struts, and so on
Open-source projects under apache. It is also an associated project of jakarta.
2. OGNL: OGNL is a powerful Expression Language (Expression Language, EL for short). In webwork2 and Struts2.x, OGNL is used to replace the original
EL to bind interface data
3. xwork: XWork is implemented in a standard Command mode and completely detached from the web layer. Xwork provides many core functions: front-end interceptor
(Interceptor), form attribute verification during runtime, type conversion, powerful expression Language (OGNL-the Object Graph Navigation Language ),
IoC (Inversion of Control Inversion Control) containers.
4. command mode: Command mode is the behavior mode of the object [GOF95 ]. The command mode is also called Action mode or Transaction
) Mode. In command mode, a request or operation is encapsulated into an object .)
When developing a struts2 project, do not copy all the jar files under the struts2 framework lib to your project. If you are integrating other frameworks. This makes the project appear non-
Often bloated. In addition, it is useless to add. It does not play any role.

2. Build the Struts2 Environment
Tool: Use the built-in Tomcat for MyEclipse9

To build a Struts2 environment, follow these steps:
1) Find the jar file to be used to develop the Struts2 application.
Create a web project using MyEclipse and add the struts2 core jar package through MyEclipse (a core package is actually added here ).
2) Compile struts. xml, the configuration file of Struts2.
When MyEclipse is added to the struts2 jar package, the struts. xml configuration file is automatically created.
3) add struts2 MVC Framework STARTUP configuration to the web. xml file
When MyEclipse is added to the struts2 jar package, the struts2 STARTUP configuration is automatically added to web. xml, that is, the struts framework is started through Filter,

The init () method of the StrutsPrepareAndExecuteFilter class configured in web. xml reads the struts. xml configuration file to complete initialization.
Note: struts2 uses a filter to implement the distribution function.
4) Verify that the environment is successfully set up:
Start the created project. If no error is reported, it will succeed,

3. Entry Program: The first struts2.3 instance -- Hello Struts

Directory structure:

1) create an action class

Package cn. ac. siat. helloaction; import com. opensymphony. xwork2.ActionSupport; public class HelloAction extends ActionSupport {private String message; // struts2 default execution method public String execute () {System. out. println ("execute ()... "); message =" Hello Struts! "; Return" success ";} // get set methodpublic String getMessage () {return message;} public void setMessage (String message) {this. message = message ;}}

2) configure the action class in struts. xml

3) index. jsp Program

  $ {Message}

4) browser access
Http: // lzc: 8080/Struts2Test1/helloAction. action

4. Possible Errors
No result defined for action cn. ac. siat. helloaction. HelloAction and result Hello Struts!

There are five static constants in String:
For example, if ruturn SUCCESS is returned in excute;
Returns the return "success" string.
You must write the corresponding "success" in struts. xml"
/X. jsp

Of course, the EORROR corresponds to "error"; string
Of course, INPUT corresponds to "input"; string
Of course, LOGIN corresponds to "login"; string
Of course NONE corresponds to "none"; string

Otherwise, replace return SUCCESS with return "success ".
Make sure that you have defined the strings returned by excute in struts. xml.

Another reminder is:
If there is an error in validate verification and no return string is specified,
Struts2 returns return INPUT by default, that is, return "input"

Note: Adding namespace = "/" in the package tells the system to find from the root directory, such as http: // lzc: 8080/Struts2Test1/helloAction. action.

If namespace = "/user", you must add this path between the project name and action during access, for example, http: // lzc: 8080/Struts2Test1/user/helloAction. action.

5. Introduction to packages in Struts. xml configuration

/Index. jsp

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 related to a set of business functions. In

In the application, 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 the java class package. If other packages want to inherit the package, they must pass
This attribute is referenced. 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, as shown in the preceding example.
Sub-Action, access path:/test/helloAction. action. The namespace attribute can not be configured. In this example, if this attribute is not specified, the default namespace is "" (empty string ).
Generally, each package should inherit the struts-default package, because many core functions of Struts2 are implemented by the interceptor. For example, encapsulate Request Parameters in a request
Action, file upload, and data verification are all implemented through the interceptor. Struts-default defines the interceptor and Result types. This can be said: When the package
Only struts-default can be used to use the core functions provided by struts2. The struts-default package is defined in the struts2-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.
Note: The package can also be defined as an abstract package through abstract = "true". The abstract package cannot contain action.

6. Action)
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 this package exists, search for the action named test in this package, as shown in figure
If this package does not exist, go to step 3;
3. Find the package whose namespace is/path1/path2. If this package exists, search for the action named test in this package. If this package does not exist
Package, go to step 4;
4. Find the package whose namespace is/path1. If the package exists, search for the action named test in the package. If the package still does not exist
Package, go to the package of the default namaspace to find the action named test (the default namespace is an empty string "").
The action cannot be found.
Default values in Action configuration

/WEB-INF/page/hello. jsp

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.
Various forwarding types of result in Action

/WEB-INF/page/hello. jsp

Result configuration is similar to forward in struts1, but struts2 provides multiple result types. Common types include dispatcher (default), redirect,

RedirectAction and plainText.
The following is an example of the redirectAction result type. If the redirected action is under the same package:
If the redirected action is in another namespace:


Plaintext: displays the content of the original file. For example, this type can be used when the source code of the jsp file needs to be displayed as is.

/Xxx. jsp

You can also use the $ {attribute name} expression in result to access the attributes in action. The attribute names in the expression correspond to the attributes in action. As follows:
View. jsp? Id =$ {id}
Multiple actions share one view-global result Configuration
When the same view is used in multiple actions, we should define the result as a global view. Global forward is provided in struts1 and in struts2

Similar functions:

/Message. jsp

Inject a value to the Action property.
Struts2 provides the dependency injection function for the attributes in the Action. In the struts2 configuration file, we can easily inject values for the attributes in the Action. Note: Owner

The setter method must be provided.
Public class HelloWorldAction {
Private String savePath;
Public String getSavePath (){
Return savePath;
Public void setSavePath (String savePath ){
This. savePath = savePath;

/WEB-INF/page/hello. jsp

The aboveThe node injects "/images" into the savePath attribute of the action"

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.