Detailed description of the configuration file in struts

Source: Internet
Author: User

Detailed description of the configuration file in struts:

The struts application is configured using two XML-based configuration files: Web. XML and struts-cofig.xml files. web. the XML file is to configure all web applications and the struts-config.xml file is Struts-specific configuration file, in fact, can also give this configuration file other names as needed.

Web application release description file:

The Web application release description file can transmit configuration information between the application owner, publisher, and assembler. The Web Container reads configuration information from the file at startup, load and configure the web application based on it. document Type Definition DTD defines the format of an XML document. a dtd is divided into elements and attributes. Each XML document of an object has its own DTD file. can be downloaded from the Internet. the <web-app> element is web. XML root element. Other elements must be embedded in the <web-app> element. note that sub-elements are also ordered. For example, you must first <servlet> and then <servlet-mapping> last <taglib>.

Configure the Web. xml file for the struts application:

The most important step is to configure the actionservlet, which uses the servlet-name attribute of the <servlet> label to start an action, and then uses the servlet-class attribute to specify the actionservlet class.

Then, use the servlet-name attribute of the <servlet-mapping> label to specify the action, and use URL-pattern to specify the receiving range *. do request. no matter how many sub-applications are included in the application, you only need to configure an actionservlet. different functions of the application come out of the class. In fact, the servlet itself is multi-threaded, currently, Struts only allows configuring one actionservlet. declare the initialization parameters of actionservlet: The <init-param> sub-element of <servlet> is used to configure the initialization parameters of servlet. param-Name: Set the config parameter name. param-value sets the path parameter value for the struts-config.xml.

Configuration welcome list:

If the client accesses the web, the value is the URL of the root directory of the Web application. if no specific file is specified, the Web will automatically call the Web welcome file. <welcome-file-List>. use the <welcome-File> welcome page </welcome-File> to configure.

Configuration error handling:

Although the Struts framework has a powerful error handling mechanism, it cannot handle all errors or exceptions. when an error occurs, if the framework cannot handle the error, it will discard the error to the Web Container. By default, the Web Container will want the client to return the error message. to prevent customers from seeing the original error information, you can configure the <error-page> element in the Web application release description file. <error-code> 404 is used to define the error type. then, you can use the <location> JSP page to handle the error. you can also use <exception-type> to set exceptions, and then use <location> to handle abnormal JSP pages to handle exceptions.

Configure the struts tag Library:

This is similar to the previously learned JSP custom tag. The configuration element is <taglib>. <taglib-Uri> specifies the URI of the tag library, which is similar to a name. <taglib-location> the location of the tag library is the actual path. this method is used to introduce a tag library, and then the tag can be called through its own URI on the front-end JSP page.

Struts configuration file:

Struts-config.xml files.

First, let's take a look at Org. apache. struts. config package. When the struts application is started, the struts configuration file information will be read to the memory and stored in the corresponding JavaBean class instance in the config package. each class in the package corresponds to a specific configuration element in the struts configuration file. moduleconfig plays a very important role in the Struts framework and is the core of the entire config package, stores the configuration information of the entire application during struts runtime. if multiple sub-applications have a moduleconfig object, it corresponds to <Struts-config> Of the struts file root element. the root element contains <form-bean> <action> <forward> and other elements.

<Struts-config> element: the root element of the struts configuration file, and its corresponding configuration class moduleconfig. The <Struts-config> element has eight child elements. their DTD definition is data-sources? Form-bean? Global-exception? Global-forwards? Action-mapping? Controller? Message-resources? Plug-in * in the struts configuration file, the sub-elements of the <Struts-config> element must be configured in the FIFO order specified by the DTD. If the order of these sub-elements is reversed, an error occurs.

<Data-sources> element: used to configure the data source required by the application. The data source is used to create a connection to a specific database. many data sources adopt the connection pool mechanism. to improve database access performance. the Java language provides javax. SQL. datasource interface. All data sources must implement this interface. many application servers and web servers provide data source components. many database vendors also provide data source implementation. the <data-sources> element contains multiple <data-source> child elements that are used to configure specific data sources. they can contain multiple <set-property> sub-elements to set various attributes of the data source. after the data source is configured, you can access the data source in the action class. In the action, the getdatasource (httprequest) method is defined to obtain reference of the data source object. then, you can use the datasource object to call getconnection to obtain a connection object for database operations. when declaring multiple data sources in the configuration file, you need to assign a unique key value for each data source. This indicates a specific data source. you can use datasource = getdatasource (reqeust, "a") to obtain a specific data source ");

<Form-beans> element: used to configure multiple actionforms, including one or n <form-bean> child elements. each <form-bean> element contains multiple attributes. classname specifies the class that matches <form-bean>. name specifies the unique identifier of the actionform. This attribute is required for reference later. type specifies the complete Class Name of the actionform class, which is also required. note that the package name must also be added. <form-property> is an element used to specify a dynamic form.

<Global-exception> element: used to configure exception handling. The element can contain one or more <exception> elements to set mappings between Java exceptions and exception handling class exceptionhandler. classname specifies the configuration class corresponding to the element, which is not required by default. handler specifies that the default exception handling class is exceptionhandler. key specifies the Message key with an exception in the local resource file, and path specifies the path to forward when the current exception occurs. scope specifies the range where the actionmessages instance is stored. type specifies the name of the exception class to be processed. required. bundle specifies the resource bundle.

<Global-forwards> element: Used to declare Global Forwarding. An element can contain one or more <forward> elements to map a logical name to a specific URL, in this way, the action class or JSP page does not need to specify a URL. You only need to specify a logical name to forward or redirect requests. this reduces the aggregation of control components and views. easy to maintain. the configuration class corresponding to the classname. if the value of contextrelative is true, it indicates that when the path attribute starts with a slash (/), the corresponding context URL is provided. The default value is false. name: Logical name of the forwarding path, which must be written. the URL forwarded or redirected by path must start. if redirect is set to true, the redirection operation is executed. If this parameter is set to false, the request forwarding operation is executed. the difference between redirection and request Forwarding is that redirection generates a response to the request to the client and resends the request to the targeted URL. the address bar of the browser is displayed. forwarding means to directly forward the request to another file of the application without generating a response. Therefore, the client IE is not displayed.

<Action-mapping> element: contains one or n <action> elements, which describes the action ing from a specific request path to the response action. the <action> element can contain multiple <exception> and <forward> sub-elements, which configure local Exception Handling and local forwarding respectively. attribute Sets the key of the actionform associated with the action within the request or session range. is the name in the request or session share. the class of the configuration element corresponding to the classname. the default value is actionmapping. forward specifies the forwarding URL path include to specify the URL path. input specifies the URL containing the form. The URL sent when form verification fails. name: Specifies the name of the form associated with the action. this name must have been defined in form-bean and can be written or not. path must start. parameter specifies the action configuration parameters. in the execute () method of action, you can call the getparameter () method of actionmapping to read the matching parameters. roles specifies the security role that can call this action. Multiple roles are separated by commas. scope specifies the scope of the form. the default value is session. tyep specifies the complete Class Name of the action. if unknown is true, all invalid actionurls sent by the user can be processed. The default value is false. validate specifies whether to call the validate method of actionform.

 

<Controller> element: used to configure actionservlet. buffresize specifies the input buffer size of the uploaded file. this attribute is optional. The default value is 4096. classname specifies the configuration class corresponding to the element, controllerconfig. the contenttype specifies the content type and character encoding of the response result. This attribute is optional. The default value is text/html. If the type content is set in action or JSP page, the content will be overwritten. locale specifies whether to save the locale object to the current user's session. The default value is false. tempdir specifies the temporary working directory for processing file uploads. if nochache is true, a specific header parameter is added to the response result.

<Message-resources> element: used to configure resource bundle. used to store local text message files. configuration class corresponding to the classname element. messageresourcesconfig. factory specifies the factory class of the message. key specifies the attribute key used in the servlet object where the file is stored. null specifies how to process unknown messages. parameter specifies the message file name.

<Plug-in> element: used to configure the struts plug-in.

Configure multiple application modules: All Sub-applications can share the same actionservlet instance, but each sub-application has a separate configuration file. divides an application into multiple sub-application modules. create a separate struts configuration file for each application. XML actionservlet ConfigurationCodeAdd several sub-application information. Use elements to switch between applications.

Digester component: an Apache componentSource codeProject. when struts is initialized, it first reads and parses the configuration file. The framework uses the digester component and uses the West configuration file. create an object in the config package. objects are used to store configuration information.

In fact, it is not difficult to configure the file, as long as all the principles are taken into account, it will be OK. the tools used in real project development, such as the eclipse series, provide corresponding plug-ins. The configuration file labels are automatically generated when a struts project is created, and we only need to fill in the attribute in it and it will be OK

 

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.