Struts2 Learning (2), struts2 Learning

Source: Internet
Author: User

Struts2 Learning (2), struts2 Learning
1. Struts2 configuration file 1. Configure multiple configuration files

In most applications, as the application scale increases, the number of actions in the system will also increase significantly, resulting in a very bloated struts. xml configuration file.

To avoid struts. xml files are too large and bloated to improve struts. for the readability of xml files, we can set a struts. the xml configuration file is divided into multiple configuration files, and then in struts. the xml file contains other configuration files.

The following struts. xml uses the <include> element to specify multiple configuration files:

<struts>       <include file="struts-part1.xml"/>       <include file="struts-part2.xml"/></struts>
2. Six configuration files in Struts2

The Struts2 framework loads the struts2 configuration in the following order:

1. default. properties the file is saved in the org. apache. struts2 package in the struts2-core-2.3.7.jar: contains the default constant configuration of Struts2.

2. struts-default.xml the file is saved in the struts2-core-2.3.7.jar that contains the object configuration and result type, interceptor and other configurations for the Framework dependency.

3. struts-plugin.xml the file is saved in the Struts2 framework plug-in: The struts-Xxx-plugins-2.3.7.jar is provided by the plug-in.

Note:The above three files are provided by the framework and cannot be modified. They can only be used.

4. struts. xml: the default struts configuration file for web applications.Important: Configure custom actions and other information.

5. struts. properties this file is the default configuration file of Struts. You can modify the constant configuration of default. properties.

6. web. xml: the configuration file of the Web application.

Note:The above three files can be modified.

If the same struts2 constant is configured for multiple files, the constant value configured in the latter file overwrites the constant value configured in the previous file.

Generally, we only configure constants in struts. xml.

<constant name="struts.action.extension" value="action,itsource,do,,"/>
3. Common constant configurations

1. Specify the default sequence set, which is equivalent to the setCharacterEncoding method of HttpServletRequest and the output of freemarker and velocity:

<constant name="struts.i18n.encoding" value="UTF-8"/>

2. This attribute specifies the request suffix to be processed by Struts 2. The default value of this attribute is action, that is, all requests matching *. action are processed by Struts2. If you need to specify multiple request suffixes, multiple suffixes are separated by commas.

<constant name="struts.action.extension" value="action,,"/>

3. Set whether the browser caches static content. The default value is true (used in the production environment). It is best to disable it during development.

<constant name="struts.serve.static.browserCache" value="false"/>

4. After the struts configuration file is modified, the system automatically reloads the file. The default value is false (used in the production environment ),It is best to open it in the development stage.

<constant name="struts.configuration.xml.reload" value="true"/>

5. Use in development mode to print more detailed error information.

<constant name="struts.devMode" value="true" />

After modifying struts. xml, you do not need to restart Tomcat.

6. default view topic.

<constant name="struts.ui.theme" value="simple" />

7. Whether dynamic method call is supported.

<constant name="struts.enable.DynamicMethodInvocation" value="false"/>
4. package, action, result configuration 1. <package> elements:

Is a child element of the <struts> root element ..

<package name="" extends="" namespace="" abstract=""></package>

Used to manage multiple <action> elements by category,It has nothing to do with the package in Java.

Common attributes:

Name: indicates the name of the <package>, but make sure that the names of different <package> elements are different. This name can be referred to by other packages.

Extends: indicates the <package> inherited <package>, which is generally struts-default. Struts-default is actually the name of the <package> element in the struts-default.xml. After inheriting struts-default, all resources defined by <package> are available. (Return result type, Interceptor ..)

Namespace: indicates the namespace. Generally, it starts with "/" and is named by the module name, for example,/hello,/oa. and <action> name determine the access path of an Action class.

Abstract: The default value is false.If a <package> abstract = "true", the <action> element cannot be defined in the <package> and can only be inherited..

There can be multiple modules in the application. Multiple modules share the sameBasic Configuration.

<Package name = "basePkg" extends = "struts-default" namespace = "/" abstract = "true"> <! -- Common basic configuration information --> </package> <package name = "oa" extends = "basePkg" namespace = "/oa"> <! -- OA-related configuration --> </package> <package name = "station" extends = "basePkg" namespace = "/station"> <! -- CRM-related configuration --> </package>
2. <action> elements:

Is a child element of the <package> element. Used to configure Action objects.

<action name="" class="" method=""/>
Common attributes:

Name: action name. In the same <package>, the action name must be unique,Together with the namespace of <package>, the access path of an Action class is determined..

Note:The name value of an action cannot start.

Class: fully qualified name of an Action class. Default Value: ActionSupport class.

Method: The method used to access the current Action. Default Value: execute.

<Action name = "hello" class = "com. struts2.action. HelloAction" method = "execute"> <! -- Result set, that is, view returned successfully in action --> <result name = "success">/HelloWorld. jsp </result> <result name = "error">/AccessDenied. jsp </result> </action>
3. <result> element:

Configuration result view.

<Result name = "" type = ""> path </result>

Partial result view: <result> is defined in <action>.

Global result view: <result> is defined in <global-results>, while <global-results> is in <package>.

<package name="oa" extends="basePkg" namespace="/oa">    <global-results>        <result></result>    </global-results></package>
Common attributes:

Name: name of the logical view returned by the Action method. Default Value: success

Type: The jump type of the result. The value of this type is predefined in the struts-default.xml. Default Value: dispatcher.

Common type values (result type ):

Dispatcher: forward an Action request to the page (JSP ).

Redirect: Redirection from Action to page (JSP ).

Chain: forward an Action request to another Action.

RedirectAction: Redirection from Action to another Action.

Stream: indicates the returned stream. Used for file download.

<Param name = ""> parameter: Default Value: location (address ).

5. Three writing methods of the Action Class 1. The first method is to use the public POJO class as the Action. Provides a public, non-parameter Action method. (Not recommended)

Disadvantages:

1. There is no way to constrain the Action method to be public without parameters.

2. The return logic view name of the Action method can be customized. Sometimes the name is invalid. For example, "ooxx ".

1 public class ConfigAction {2   public String list(){3     return "success";4   }5 }
2. Define a class to implement the com. opensymphony. xwork2.Action interface. And override the execute method. (Not recommended)

The Action interface not only provides the declaration of the Action method, but also provides common logical view names:

Public static final String SUCCESS = "success ";

Public static final String NONE = "none ";

Public static final String ERROR = "error ";

Public static final String INPUT = "input ";

Public static final String LOGIN = "login ";

Disadvantages:

1. It does not support internationalization, data verification, and message mechanisms.

1 public class ConfigAction implements com.opensymphony.xwork2.Action {2   public String list(){3     return SUCCESS;4   }5 }
3. Define a class that inherits from the com. opensymphony. xwork2.ActionSupport class. (Recommended)

Public class ActionSupport implements Action, Validateable, ValidationAware, TextProvider, LocaleProvider, Serializable {}

1 public class ConfigAction extends com.opensymphony.xwork2.ActionSupport {2   public String list(){3      return SUCCESS;4    }5 }
4. In actual development, we often provide another BaseAction class.

ActionSupport.
--- BaseAction (base class of Custom Action)
----- AAction
----- BAction

6. Call multiple methods in Action

Multiple Action methods in an Action will result in a bloated <action> configuration.

1. DMI: dynamic method call: Not officially recommended.

Format: action name! Method Name

For example, emp! Edit emp! List

In the new version of Struts2, DMI is disabled by default. To use DMI, You need to configure constants and enable dynamic method calls.

In this case, the <action/> element does not need to specify the method property value.

<constant name="struts.enable.DynamicMethodInvocation" value="true"/>
2. Use wildcard to configure a class: wildcard :*
<action name="emp_*" class="cn.itsource.manymethod.EmployeeAction" method="{1}">

Action name: emp_Action method: for example, emp_list, the value of {1} is list.

Emp_edit, then the value of {1} is edit.

Two wildcards:

<action name="*_*" class="cn.itsource.manymethod.{1}Action" method="{2}">

Action name: Action class name_action method .. For example, Employee_list indicates the list method in the called EmployeeAction.

For example, Department_edit indicates that the edit Method in DepartmentAction is called.

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.