1. Overview of interceptors

Concept: Struts2 is a framework that encapsulates a lot of functionality. The functionality encapsulated in Struts2 is in interceptors. Struts2 has many interceptors, but not all of them are executed.

Interceptor execution time: after the action object is created and before the action’s methods are executed.

2. Principle of interceptor bottom layer

Aop thought: AOP is facing the aspect (aspect) programming, has the basic function, the extension function, does not modify the source code way extension function

Chain of Responsibility mode:

  • injavaChain of responsibility is one of many design patterns used.
  • 1:The chain of liability is very similar to the chain of filters
    • Filter chain: a request can be filtered by multiple filters, and each filter can only pass to the next filter.
    • Chain of responsibility: To perform multiple operations, such as add, delete, modify three operations,
      • After completing one operation, it is released, and the next operation is performed until the last operation.
  • 2: aopHow does the chain of thought and responsibility model apply to interceptors?
    • inactionMethod is executed before the default interceptor is used by the execution procedureaopThe thoughts, inactionInstead of calling interceptor methods directly, you operate in configuration files
    • When executing interceptors, many interceptors are executed using the chain of responsibility mode
    • If you execute three interceptors, execute interceptor 1, execute interceptor 1 and then release, execute interceptor 2, execute interceptor 2 and then release, execute interceptor 3 and then release, execute action

3. Differences between filters and interceptors

  • Filter:Filters can theoretically be arbitrary, for examplehtml,jsp,servlet, picture path.
  • The interceptor:Interceptors can only interceptaction

4. Custom landing interceptors

  • 1:struts2There are a lot of interceptors in there, and these interceptors arestruts2Encapsulated functionality, but in real development,struts2Inside the interceptor can not use the function, this time need to write the interceptor to implement the function
  • 2:Interceptor structure source code introduction
    • class ModelDrivenInterceptor extends AbstractInterceptor
      • class AbstractInterceptor implements Interceptor: Three methods in the interface
        • void init(): Initialization method.
        • void destroy(): destruction.
        • String intercept(ActionInvocation invocation): Intercepts logical operations.
    • Another approach is recommended for development
      • Write classes, inheritMethodFilterInterceptorClass implements
      • letactionOne of these methods does not intercept
    • Let the interceptor andactionHave a relationship
      • Not inactionThe interceptor method is called, but the relationship is established through a configuration file

5. Struts2 Tag Library (JSP)

  • s:propertyAnd:ognlThe expression injspPage to get value stack data
  • s:iteratior: gets the value stacklistSet data, representslistA collection of
  • s:debug: View value stack structure and data

6. Struts2 form tags

HTML form tags:

  • Form: Action, Method, encType
  • Most encapsulate type= “value” in input
    • text: Common input
    • password: Indicates the password
    • radio: Optional input
    • checkbox: Check the input
    • file: Indicates the file upload item
    • hidden: hide items
    • button: Common button
    • submit: Submit button
    • image: Picture submission
    • resetReset:
    • select: Drop down input items
    • textarea: text field
  • instruts2Inside the correspondinghtmlForm tags are mostly available