Struts2 tutorial 3: Common struts. xml configuration Parsing

Source: Internet
Author: User

This article describes the common configurations and precautions of Struts. XML files.

1. Use the <include> tag to reuse the configuration file

A default struts. xml file is provided in struts2. However, if many configurations such as package, action, and interceptors are involved, it is not easy to maintain them in a single struts. xml file. Therefore, you need to divide the Struts. xml file into multiple configuration files, and then use the <include> tag in the Struts. xml file to reference these configuration files. The advantages of this method are as follows:
(1) The structure is clearer, making it easier to maintain configuration information.
(2) configuration files can be reused. If similar or identical configuration files are used in multiple web programs, you can use the <include> label to reference these configuration files, which reduces the workload.

Suppose there is a configuration file named newstruts. xml. The Code is as follows:

<? XML version = "1.0" encoding = "UTF-8"?>
<! Doctype struts public
"-// Apache Software Foundation // DTD struts configuration 2.0 // en"
Http://struts.apache.org/dtds/struts-2.0.dtd>
<Struts>
<Package name = "Demo" extends = "struts-Default">
<Action name = "Submit" class = "Action. moresubmitaction">
<Result name = "save">
/Result. jsp
</Result>
<Result name = "print">
/Result. jsp
</Result>
</Action>
</Package>
</Struts>

Then the code for struts. XML to reference the newstruts. xml file is as follows:

<? XML version = "1.0" encoding = "UTF-8"?>
<! Doctype struts public
"-// Apache Software Foundation // DTD struts configuration 2.0 // en"
Http://struts.apache.org/dtds/struts-2.0.dtd>
<Struts>
<Include file = "newstruts. xml"/>
<Package name = "test" extends = "struts-Default">
......
</Package>
</Struts>

Note that the XML file referenced by <include> must also be configured with struts2. In fact, <include> the referenced XML file is parsed separately, rather than inserting the referenced file into the Struts. xml file.

2. Alias of action

By default, struts2 calls the execute method of the executor class. However, sometimes we need to process different actions in a category. That is, when a user requests different actions, different methods in the actions class are executed. To achieve this purpose, you can use the method in the <action> label to specify the method name of the category class of the row to be specified, in addition, different sub-names (also called aliases) need to be initiated for different actions ). As shown in the following code:

<? XML version = "1.0" encoding = "UTF-8"?>
<! Doctype struts public
"-// Apache Software Foundation // DTD struts configuration 2.0 // en"
Http://struts.apache.org/dtds/struts-2.0.dtd>
<Struts>
<Package name = "Demo" extends = "struts-Default">
<Action name = "test" class = "Action. myaction">
......
</Action>
<Action name = "my" class = "Action. myaction" method = "my">
......
</Action>
</Package>
</Struts>

The class attribute of the two actions in the code above points to the same class. name represents two action aliases for this class: Test and my. In action my, the method attribute is used to specify the name of the method to be run: My.
The my method must be available in the myaction class. The Code is as follows:

Package action;

Import com. opensymphony. xwork2.actionsupport;

Public class myaction extends actionsupport
{
......
Public String execute () throws exception
{
// Code for processing the test action
}
Public String my () throws exception
{
// Code for processing my actions
}
......
}

In addition to configuring aliases in struts. XML, you can also use request parameters to describe specific actions (you do not need to configure aliases in struts. XML ). The request parameter format is as follows:
Http: // localhost: 8080/contextpath/actionname! Method. Action

For details about the actions specified by the request, see the author's struts2 tutorial 2: process multiple submit of a form.

3. Specify parameters for action

In struts2, you can also specify one or more parameters for an action. Do you still remember how struts1.x sets the action parameter? In struts1.x, you can use the parameter attribute of the <action> tag to specify an action parameter. To specify multiple parameters, you can only use commas (,) or other delimiters separate different parameters. In struts2, you can use the <param> label to specify any number of parameters. The Code is as follows:

<Action name = "Submit" class = "Action. myaction">
<Param name = "param1"> value1 </param>
<Param name = "param2"> value2 </param>
<Result name = "save">
/Result. jsp
</Result>
......
</Action>

Of course, reading these parameters in action is also very simple. You only need to define the corresponding setter method in the action class just like getting the request parameters (generally, the getter method is not required ). The following code reads the values of param1 and param2:

Package action;

Import com. opensymphony. xwork2.actionsupport;

Public class myaction extends actionsupport
{
Private string param1;
Private string param2;

Public String execute () throws exception
{
System. Out. println (param1 + param2 );
}


Public void setparam1 (string param1)
{
This. param1 = param1;
}
Public void setparam2 (string param2)
{
This. param2 = param2;
}
......
}

Before struts2 calls execute, the values of param1 and param2 are already the values of the corresponding parameters. Therefore, param1 and param2 can be directly used in the execute method.

4. Select the result type

By defaultTypeThe attribute value is "dispatcher" (actually forward, forward ). Developers can specify different types as needed, such as redirect and stream. As shown in the following code:

<Result name = "save" type = "Redirect">
/Result. jsp
</Result>

This result-type can be found in the struts2-core-2.0.11.1.jar package or in the struts-default.xml file in struts2 source code, where the <result-types> tag, all result-types are defined in it.

5. Global result

Most of the time a <result> is used in many <action> operations, you can use the <global-Results> label to define a global <result>. The Code is as follows:

<Struts>
<Package name = "Demo" extends = "struts-Default">
<Global-Results>
<Result name = "print">/result. jsp </result>
</Global-Results>
<Action name = "Submit" class = "Action. moresubmitaction">
......
</Action>
<Action name = "my" class = "Action. moresubmitaction" method = "my">
......
</Action>
</Package>
</Struts>

If <action> does not contain the corresponding <result>, struts2 uses the Global <result>.

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.