Extended Struts Framework

Source: Internet
Author: User

Extended Struts Framework

A good software framework should be extensible. The Struts Framework provides many extensible points, which are not called extension points ). The following are struts extensions:

· General extension points: struts plug-in (plugin) and extended struts configuration class.

· Controller extension points: Extended actionservlet class, requestprocessor class, and action class.

· View Extension: Expand struts custom labels.

· Model extension: extends the sessioncontainer class and applicationcontainer class.

 

1.
StrutsPlug-in)

The struts1.1 framework provides the ability to dynamically insert and load components, which are used as Struts plug-ins. The struts plug-in is actually a Java class. It is initialized when the struts application is started and destroyed when the application is closed. Any Java class used as the plug-in should implement the org. Apache. Struts. Action. plugin interface. The plugin interface includes two methods:

 

Public interface plugin {

/**

* Notification that
Specified application module is being started.

*/

Public void
Init (actionservlet servlet, applicationconfig config)

Throws
Servletexception;

/**

* Notification that
Application module is being shut down.

*/

Public void destroy ();

}

 

A struts application can contain one or more plug-ins. When the struts application is started, the Struts Framework calls the init () method of each plug-in class for initialization. In the initialization phase of the plug-in, you can complete initialization operations, such as establishing a database connection or connecting to a remote system.

When an application is disabled, the Struts Framework calls the destroy () method of each plug-in class. The destroy () method can be used to release resources, for example, close the database connection or remote system connection.

In addition to creating a plug-in class, you also need to configure the plug-in the struts configuration file. The Struts framework will initialize the plug-in according to the relevant configuration information at startup. The configuration element corresponding to the plug-in is <plug-in>.

According to the DTD definition in the struts configuration file, the <plug-in> element in the struts configuration file must be located behind other configuration elements. In addition, if multiple plug-ins are configured in the configuration file, the Struts framework will initialize them one by one according to their order in the configuration file.

 

2.
Extended struts configuration class

When the struts application is started, all information in the struts configuration file will be read to the memory, which is stored in the instance of the corresponding configuration class of the org. Apache. Struts. config package.

Most configuration elements are composed of a classname attribute, which is used to set the configuration class corresponding to the configuration element. Each configuration element has a default configuration class, which can be extended by the Struts framework.

 

3.
Controller extension point

The Struts Framework provides many extensions in the controller, allowing the extension of actionservlet, requestprocessor, and Action classes to implement various custom functions.

3.1Extended actionservlet class

In versions earlier than struts1.1, Struts applications usually need to extend the actionservlet class to implement various custom control functions. In struts1.1, extending the actionservlet class is no longer necessary. However, in some cases, the actionservlet class can be expanded as needed.

When the struts application starts, it loads the actionservlet class and calls its Init () method to initialize the Struts framework. To modify the initialization behavior of the Struts framework, you can create a subclass of the org. Apache. Struts. Action. actionservlet class and overwrite its Init () method.

Next, configure the custom actionservlet in the web. xml file.

In struts1.1, the specific operations for preprocessing HTTP requests are completed by the requestprocessor class. Therefore, you can extend the requestprocessor class to customize the pre-processing HTTP request method.

 

3.2Extended requestprocessor class

If the requestprocessor class is extended, configure the custom requestprocessor class through the <controller> element in the struts configuration file.

<Controller> the processorclass attribute of the element is used to specify the requestprocessor class used. The Struts framework creates an instance of the requestprocessor class at startup and uses it to process all HTTP requests. Because each sub-application module has its own configuration file, you can configure different requestprocessor classes for each sub-application module.

An extension of the requestprocessor class is the processpreprocess () method. In the requestprocessor base class, this method does not perform any operations, and Zhijun returns true.

The requestprocessor class processes requests in the process () method. The process () method calls the processpreprocess () method before calling the action's execute () method. If processpreprocess () if the return value is true, the request is processed according to the normal process.

In the custom requestprocessor class, you can overwrite the processpreprocess () method to execute specific logic. If you want to terminate the Processing request under certain conditions, you only need to make the processpreprocess () method false. In this case, you still need to determine how to forward or redirect requests programmatically.

The servlet 2.3 API provides servlet filters that implement the same functions as the processpreprocess () method. The servlet filter can also be used to perform custom pre-processing request operations. The Web Container first calls the servlet filter and then forwards the request to the struts controller.

Compared with the extended requestprocessor class, using Servlet filters to pre-process requests has two major weaknesses:

 

3.3 Extended action class

The action class of the Struts framework is the most frequent extension point. For a specific struts application, you can first create an action base class that extends the struts action class for the application. In this action base class, define some public logics of all actions in the application, it can be used as the parent class of other actions. This processing method can improve code reusability and reduce code duplication.

 

 

4.
Extended view component

Generally, there is no need to expand the view components. Because different applications have different appearances and interfaces, the JSP page of one application is unlikely to be applicable to another application. However, Struts custom labels can be extended because the tag processor is a regular Java class and can be extended by defining subclasses. These extended custom labels can be reused by different applications.

The labels in the struts HTML Tag library have the greatest impact on the View content. Therefore, you can expand these labels to create custom application appearances. After the tags are extended, define the tag library for storing these tags. Although you can add custom labels to the standard struts tag library, it makes it more difficult to upgrade the application to the new struts version. Therefore, we recommend that you define a separate tag library to store custom tags related to specific applications.

Once a TLD file is created for the customer's China tag Library and the tag library is registered in Web. XML, these tags can be conveniently used in JSP files.

5.
Extended model component

The Struts framework itself does not provide ready-made model components at the model layer. Therefore, extended model components are not part of the STRUTS technology.

6.
Summary

This document summarizes all the Extensible points of the Struts framework. The extensibility of the Struts framework allows developers to easily customize custom functions, improving application flexibility and adaptability to various needs. However, to implement more functions at a higher cost, we should avoid abusing struts's Extensible features. Struts consists of the core package and many toolkit. They have provided many ready-made functions. Therefore, do not expand the Struts framework blindly. Before deciding to write the extension code, make sure that struts does not provide the functions you need. Otherwise, duplicate features will lead to confusion in the application structure, and additional experience will be required to clear duplicate features in the future.

In addition, you must consider whether the extended framework will be compatible with the new struts version in the future. Major changes have taken place from struts 1.0 to Struts 1.1. In the APIs of the previous struts version, if the methods of some classes have been declared to be deprecated, try not to overwrite these methods. Otherwise, when using the new struts version, you have to upgrade the application.

Reading material: proficient in struts: MVC-based Java Web design and development

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.