Working Principle and framework extension of Struts2

Source: Internet
Author: User

 

 

Framework mainly involves technologies: Spring + Struts2 + Hibernate + Freemarker

 

Let's take a look at the working principle of Struts2, so that we can easily consider the expansion points when designing the development framework:

 

 

 

ActionFilter and other Filters

Configure these filters in web. xml before Struts filters. Including the global default ActionFilter, the language code LocaleFilter, And the redirection UrlRewriteFilter.

StrutsPrepareFilter

Struts Action preprocessing filter. My approach is to write a subclass to inherit it, such as CoreStrutsPrepareFilter, so that I can implement

Dynamic Loading of struts. xml and struts-xxx.xml that specifies the rule for a directory, etc.

StrutsExecuteFilter

Similarly, use the write subclass to inherit from it, such as CoreStrutsExecuteFilter. Write and regenerate ExecuteOperations based on conditions (such as Struts-related configuration files changed ).

(Advanced encapsulation of Dispatcher ).

Interceptor

The Interceptor is the essence of Struts2. Processing here includes handling exceptions in a unified manner, permission control, parameter settings, access history, and blocking saving of some specific functions.

Action

I have designed the BaseAction and GenericAction methods for storing the specified package path for processing requests. BaseAction handles global public actions, including inheriting ActionSupport,

Implement InitializingBean and ModelDriven (in this way, you can perform initialization operations and obtain functions of the Struts2 model driver ).

GenericAction is written based on generics and implements public methods such as addition, deletion, modification, and query. There are several other project-related public xxxactions, which are designed as needed.

Manager

In the Action Method, you can call several Manager methods to implement related services. transactions are written at the Manager layer and are declared using Spring.

ViewManager

Because the system uses Freemarker template technology, there are two ways to parse the template: 1) Use the FreemarkerManager provided by Struts; 2) use FreeMarkerTemplateUtils

. To facilitate expansion (for example, writing custom Freemarker methods), I have added subclass extension for both methods.

CoreFreemarkerManager is configured in struts. xml:

<Constant name = "struts. freemarker. manager. classname" value = "com. xxxx. framework. core. freemarker. CoreFreemarkerManager"/>

CoreFreeMarkerTemplateUtils is a tool class. It is generally applicable to CMS and email template parsing.

Returned Interceptor

AOP points are before and after, while after operations are generally to write logs.

 

Author: Henry Zhen

Related Article

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.