Struts1-configuration-detailed instructions and examples are provided for ease of understanding.

Source: Internet
Author: User

Action, actionform, and actionforward constitute the core of struts.
The core controller of Struts is actionservlet, which intercepts user requests and transfers user requests to the struts system.
1. Configure actionservlet
Actionservlet is a standard servlet configured in the web. xml file. It is used to block all HTTP requests. Therefore, configure the Servlet as a self-starting servlet, that is, configure the load-on-startup attribute for the servlet.

Note: In many scenarios where struts is applied, configuring the direct load-on-startup attribute for the servlet is required. Therefore, we recommend that you configure the direct load-an-startup attribute for servletaction.

Add the following snippet to configure actionservlet in the web. xml file:

<! -- Configure actionservlet as a self-starting servlet --> <servlet> <! -- Specify the servlet name --> <servlet-Name> actionsevlet </servlet-Name> <! -- Specify the Servlet implementation class --> <servlet-class> org. Apache. Struts. Action. actionservlet </servlet-class> <! -- Configure the auto-start level --> <load-on-startup> 2 </load-on-startup> </servlet> <servlet-mapping> <! -- Configure the URL ing of actionservlet --> <servlet-Name> actionsevlet </servlet-Name> <! -- All requests ending with. Do are intercepted by actionservlet --> <URL-pattern> *. DO </url-pattern> </servlet-mapping>

As a standard servlet, this actionservlet is configured in a web application to intercept user requests. This servlet is also responsible for loading the struts configuration file. However, I did not tell it how to load the struts configuration file.

And the location of the struts configuration file and the file name. Actionservlet loads WEB-INF files under the struts-config.xml by default. If the struts configuration file is not in the WEB-INF path or the file name is changed, configure it as follows:



In the above configuration, the configuration file of actionservlet is specified: struts-config-user.xml file, this file is loaded as the init-Param parameter, the parameter name is specified during loading: config, config is a fixed parameter name of Struts. Struts parses the parameter and loads the specified configuration file of the parameter. Struts supports multiple configuration files. When there are multiple configuration files, you should configure different configuration files into different modules and specify different Uris. The following snippet configures two configuration files:

<Servlet> <! -- Actionservlet name --> <servlet-Name> actionsevlet </servlet-Name> <! -- Configure the Servlet implementation class --> <servlet-class> org. Apache. Struts. Action. actionservlet </servlet-class> <! -- Specify the first struts configuration file --> <init-param> <! -- Specify the configuration file ing --> <param-Name> config </param-Name> <param-value>/WEB-INF/Struts-con job fgl. XML </param-value> </init-param> <! -- Specify the second struts configuration file --> <init-param> <! -- Specify the configuration file ing --> <param-Name> config/Wawa </param-Name> <param-value>/WEB-INF/struts-config2.xml </param-value> </init -param> <! -- Configure the actionservlet as a self-starting servlet --> <load-on-startup> 2 </load-on-startup> </servlet>


The configuration snippets above specify two configuration files: Struts-config L. XML and the struts-config2.xml file. These two configuration files are configured under the config and config/Wawa paths respectively. It indicates that the action in Struts-config 1.xml is mapped to the root path of the application, and the action in the struts-config2.xml file is mapped to the Wawa sub-path of the application. That is to say, Wawa will be used as a module of the system.

2. Configure actionform
To configure actionform, you must include the actionform class. Struts requires actionform to inherit the struts base class: Org. apache. struts. action. the implementation of actionform and actionform is very simple. This class is just a common JavaBean. You only need to provide the corresponding setter and getter methods for each attribute. As described above, actionform is used to encapsulate the user's request parameters, which are passed through the form fields of the JSP page. Therefore, ensure that the actionform parameter is the same as the form field name.

Note: The javab EAN parameter is determined by the getter and setter methods. If you want to have a property of a, you should provide the geta and Seta methods.
(1) Implementation of actionform
The actionform attribute must be the same as the form field of the JSP page. The form in this example contains the following two form fields:
• Usemame
• Password
Therefore, actionform must inherit from org. Apache. Struts. Action. actionform and provide the corresponding setter and getter methods for these two fields. The following is the source code of actionform:

Import Org. apache. struts. action. actionform; public class loginform extends actionform {private string username; private string password; // setter method corresponding to the form field username/*** @ return the username */Public String GetUserName () {return username;}/*** @ Param username * the username to set */Public void setusername (string username) {This. username = username;}/*** @ return the password */Public String GetPassword () {return password ;} /*** @ Param password * the password to set */Public void setpassword (string password) {This. password = password ;}}

In addition, the two attribute names of the actionform can be usemame and password. You only need to provide the setter and getter methods for usemame and password.

Note: The formbean class should be declared as public as much as possible.
(2) actionform Configuration
All actionforms are configured in the struts-config.xml file, which includes an element of form-beans, which defines all actionforms and each actionform corresponds to a form-bean element.

To define loginform. The following code must be added to the struts-config.xml file:

<! -- Defines all actionforms --> <form-beans> <! -- Define actionform and specify at least two attributes: name, type --> <form-bean name = "loginform" type = "Lee. loginform"/> </form-beans>

It is very easy to configure actionform. You only need to specify the name attribute of actionform. This attribute defines the actionform ID, which is used to identify the form. In addition, a type attribute is required, which defines the implementation class of actionform.

The following describes how action uses this actionform. And how action is associated with this actionform.

3. configure action
The configuration of action is more complex than that of actionform, because action is responsible for managing the associated actionform. It requires not only configuring the implementation class, but also configuring the path attribute of action. This attribute is used

User request. For the forward that only needs to be valid in this action, you should also configure partial forward in the Action element.
(1) Implementation of action
By using actionform, action does not need to parse parameters from HTTP requests. Because all parameters are encapsulated in actionform, the following is the source code for the action to get the request parameters from acitionform:

Import javax. servlet. HTTP. httpservletrequest; import javax. servlet. HTTP. httpservletresponse; import Org. apache. struts. action. action; import Org. apache. struts. action. actionform; import Org. apache. struts. action. actionforward; import Org. apache. struts. action. actionmapping; public class loginaction extends action {// This core method must be rewritten. actionform encapsulates the Request Parameters of the form into a value object public actionforward execute (actionmapping mapping, AC Tionform form, httpservletrequest request, httpservletresponse response) throws exception {// forcibly convert the actionform type to loginformloginform loginform = (loginform) form; // parse the request parameters from the actionform: usernamestring username = loginform. getUserName (); // parse the request parameter from actionform: passwordstring pass = loginform. getUserName (); // the subsequent processing is the same as the action in the previous example ....}}

This action parses request parameters from the forwarded actionform. The corresponding actionform is instantiated by the actionservlet when receiving user requests.
The actual process is: after the actionservlet intercepts the user request, it searches for the corresponding action in the configuration file based on the user request. The action name attribute specifies the actionform used to encapsulate the request parameters; then, actionservlet creates the default actionform instance and calls the corresponding setter method to initialize the actionform.

When the actionservlet distributes user requests, it also distributes the corresponding actionform instances.
(2) action configuration
Actions must be configured in the following aspects.
• Action path: Based on this attribute, actionservlet forwards user requests to forward user requests to actions with the same name. Remove the. Do Suffix of the request and match the path attribute value of the action.

• Action name: The name attribute here is not the action name, but the actionform associated with the action. Therefore, the name attribute must be the previous actionform name.

• Action type: This attribute is used to specify the implementation class of the action, that is, the industry responsible for processing user requests.
Service controller.
• Partial forward: action is not forwarded to the actual jsp resource, but to the logic name, that is, the forward name. The forward configured in the action is partial forward (this forward is only valid in this action ).

The configuration code for this action is as follows:

<! -- Configure all actions in this element --> <action-mappings> <! -- Configure action. Specify attributes such as path, name, and type --> <action Path = "/login" type = "Lee. loginaction" name = "loginform"> <! -- Configure partial forward --> <forward name = "welcome" Path = "/WEB-INF/JSP/welcome. JSP "/> <forward name =" input "Path ="/login. JSP "/> </Action-mappings>

4. Configure forward
As mentioned above, forward is divided into two types: Local forward and global forward. The former is configured in action and only valid for this action: the latter is configured separately, and valid for all actions.

Configuring forward is very simple. You must specify the following three attributes.
• Name: Logical name of the forward.
• PATH: the jsp resource mapped to the forward.
• Redirect: whether to use redirection.
Partial forward is configured as the sub-element of action. Global forward is configured in the global-forwards element.
The following code configures Global Forwarding:

<! -- Configure global forward --> <global-forwards> <! -- Configure the name and path attributes of the forward Object --> <forward name = "error" Path = "/WEB-INF/JSP/error. jsp"/> </Global-forwards>

In the preceding configuration code, a global forward is configured, which can be accessed by all actions. Generally, only global resources are configured as global forward. When each action is forwarded, the corresponding forward is first searched in the partial forward. If the corresponding forward object cannot be found in the partial forward, the corresponding forward object will be searched in the global forward. Therefore, local forward can overwrite global forward.

The following provides all the source code for the struts-config.xm1 file for the application:

<? XML version = "1.0" encoding = "UTF-8"?> <! -- Struts configuration file header, including DTD and other information --> <! Doctype Struts-config public "-// Apache Software Foundation // DTD struts configuration 1.2 // en" ""> <! -- Struts configuration file root element --> <Struts-config> <! -- Configure all actionforms --> <form-beans> <! -- Configure the first actionform and specify the name and type attributes of the actionform --> <form-bean name = "loginform" type = "Lee. loginform "/> </form-beans> <! -- Configure the global forward Object --> <global-forwards> <! -- The name attribute of the forward object is error. map resources to/WEB-INF/JSP/error. JSP --> <forward name = "error" Path = "/WEB-INF/JSP/error. JSP "/> </Global-forwards> <! -- Configure all action mappings here --> <action-mappings> <! -- Configure the action's path. Type attribute name to configure the actionform corresponding to the action --> <action Path = "/login" type = "Lee. loginaction" name = "loginform"> <! -- Two partial forwarding rules are configured. the two partial forward is only valid for this action --> <forward name = "welcome" Path = "/WEB-INF/JSP/welcome. JSP "/> <forward name =" input "Path ="/login. JSP "/> </Action-mappings> </Struts-config>



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: 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.