Deep understanding of STRUTS2 configuration Files

Source: Internet
Author: User
Tags constant http request pack
1.1. Package Configuration:The core components in the STRUTS2 Framework are action, interceptors, etc., and the STRUTS2 framework uses packages to manage actions and interceptors. Each package is a collection of more than one action, multiple interceptors, and multiple interceptor references. In the Struts.xml file, the package element is used to define the bundle configuration, and each package element defines a bundle configuration. Its common properties are: L Name: Required property, used to specify the name of the package. L Extends: Optional attribute that specifies that the package inherits other packages. Inheriting other packages, you can inherit the action definitions, interceptor definitions, and so on in other packages. L Namespace: Optional attribute that specifies the namespace for the package.

<! DOCTYPE struts public         "-//apache software foundation//dtd struts Configuration 2.0//en "       " HTTP://STRUTS.APACHE.ORG/DTDS/STRUTS-2.0.DTD " > < struts >     <!--struts2 action must be defined in a specified package space---    < pack n AME = "Default" extends = "Struts-default" >     <!--define an action that handles the request URL to Login.action &nbsp ;       < action name = "Login" class = "org.qiujy.web.struts.action.LoginAction" >         <!--Define the mapping between the processing result string and the resource--             < result name = "Success" >/success.jsp </result >     & nbsp;       < result name = "Error" >/error.jsp </result >          </Action > &NBSP;&NBsp;  </Package > </struts >
As configured in the example above, a package named default is configured with an action defined under the package. 1.2. Namespace configuration:The same namespace cannot have an action with the same name, considering that action,struts2 in the same Web application requires the same name as the namespace to manage the action. STRUTS2 specifies a common namespace for all the actions under the package by specifying the namespace property for the package. Change the configuration of the example above to the following form:

<! DOCTYPE struts public         "-//apache software foundation//dtd struts Configuration 2.0//en "       " HTTP://STRUTS.APACHE.ORG/DTDS/STRUTS-2.0.DTD " > < struts >     <!--struts2 action must be defined in a specified package space---    < pack n Ame = "Qiujy" extends = "Struts-default" >     <!--define action to process request URL to login.action--- & nbsp;      < action name = "Login" class = "Org.qiujy.web.struts2.action.LoginAction" > & nbsp;       <!--defines the mapping between the processing result string and the resource-             < result name = "Success" >/success.jsp </result >     & nbsp;       < result name = "Error" >/error.jsp </result >          </Action >     </Package >         < package name = "my" extends = "Struts-default" name Space = "/manage" >     <!--define action to handle request URL login.action---         < action name = "Backlogin" class = "org.qiujy.web.struts2.action.LoginAction" >    & nbsp;    <!--defines the mapping between the processing result string and the resource-             < result name = "Success" >/success.jsp </result >        & nbsp;    < result name = "Error" >/error.jsp </result >          </Action >     </Package > </struts >
As configured on two packages: Default and my, the namespace specified for the package when configuring my package is/manage. For package Default: The namespace attribute is not specified. If a package does not specify the namespace property, which means that the package uses the default namespace, the default namespace is always "". For package my: The namespace/manage is specified, and the URL for all actions under the package should be "namespace/action name." As on the action named Backlogin, it handles the URL: http://localhost:8080/userlogin_struts2/manage/backLogin.action The function of the Struts2 namespace is equivalent to the function of the Struts1 module. 1.3. Include Configuration:In Struts2 a profile can be decomposed into multiple profiles, we must include additional configuration files in the Struts.xml.

< struts > < include file = "Struts-default.xml"/> < include file = "Struts-user.xml"/> &L T Include file = "Struts-book.xml"/> < include file = "Struts-shoppingcart.xml"/> ... </str UTS >
1.4. Interceptor Configuration:See the following section for more information. 1.5. Constant configuration:The STRUTS2 framework has two core configuration files, where the Struts.xml file is primarily responsible for managing the action mappings in the application, and the mapping between the action processing results and the physical resources. In addition to this, the STRUTS2 framework also contains a struts.properties file that Struts2 a large number of constant attributes of the framework. However, it is usually recommended to configure these constant properties in the Struts.xml file. For example, the internationalization of Struts2 is described later, and its resource file location is specified with a constant attribute:

< struts > ... < constant name = "Struts.custom.i18n.resources" value = "messages"/> </struts >
Specifies that the resource file is placed in the classes directory, and the base name is messages, you should place a file similar to the Messages_zh_cn.properties,message_en.properties name in the classes directory. 2. Action of Struts2 2.1. Implement the Action class:The action in STRUTS2 is the core content that contains the processing logic for the user request, and we also call the action the business controller. The action in STRUTS2 is designed with a low-intrusive design, STRUTS2 does not require the action class to inherit any Struts2 base class or implement STRUTS2 interfaces. (however, in most cases we will inherit the Com.opensymphony.xwork2.ActionSupport class to facilitate the action, and override the public String execute () in this class throws Exception method. Because there are many practical interfaces implemented in this class, there are many default methods, such as the method of obtaining internationalized information, the method of data checking, the default method of handling user request, etc., which can greatly simplify the development of the action. In Struts2, the action is usually used to encapsulate the HTTP request parameters, so the action class should also contain the properties corresponding to the request parameters, and provide the corresponding getter and setter methods for the property. (Of course, the action class can also encapsulate processing results, treat the result information as a property, provide a corresponding getter and setter method) modify the first part of the user login example: Change the action to the following:

package org.qiujy.web.struts2.action;   Import Co M.opensymphony.xwork2.actionsupport;  /**   * @author qiujy   * @version 1.0   */ publicclass loginaction extends actionsupport{     Private String UserName;     private String password;         Private String

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.