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