Struts2 The interceptor is based on AOP The realization of thought , and AOP The implementation of is based on dynamic proxy .Struts2 The interceptor will be accessing a Action Intercept before or after , also Struts2 Interceptors are pluggable ;Struts2 The interceptor stack is the chain that connects the interceptors in order , When the interception requirements are met , The interceptors are executed in the order in which they are implemented .

1、Struts2 Introduction to custom interceptor

Struts2 All interceptors must be implemented Interceptor Interface ,Interceptor The main interfaces are 3 A way :

  • init(): Initialization method , Only once when the interceptor is loaded
  • intercept(ActionInvocation invocation): Interceptor execution method , Every request is executed once
  • destory(): Destruction method , Only once when the interceptor is released

AbstractInterceptor The abstract class is implemented Interceptor Interface . And for init() and destroy() Provides a blank implementation , So in the actual development , Custom interceptors just need to inherit AbstractInterceptor class , And realize intercept(ActionInvocation invocation) The method is ok .

2、Struts2 Steps to create a custom interceptor

1). Create a class implementation Interceptor Interface or inheritance AbstractInterceptor class .
  2). rewrite intercept Method , This method is a real intercept operation , If you want to continue down to other interceptors , Must be in intercept Method through the parameter ActionInvocation call invoke Method .
  3). Configure interceptors , Need to be in struts.xml Configuration in file , There are two cases :

  • by package Pack everything <action> Configure common interceptors , It needs to be set by <default-interceptor-ref> To set the default interceptor , Need to be in <action> Previous configuration
    • Do not configure interceptor stack , Only execute <default-interceptor-ref> Set up interceptors

       <package name="default" namespace="/" extends="struts-default">
      <interceptors>
      <interceptor name="login" class="com.sunny.interceptor.LoginIntercept"></interceptor>
      </interceptors>
      <default-interceptor-ref name="login"></default-interceptor-ref>
      <action name="loginAction" class="com.sunny.action.LoginAction">
      <result>/success.jsp</result>
      <result name="error">/error.jsp</result>
      </action>
      </package>
    • Configure the interceptor stack
       <package name="default" namespace="/" extends="struts-default">
      <interceptors>
      <interceptor name="login" class="com.sunny.interceptor.LoginIntercept"></interceptor>
      <interceptor-stack name="my">
      <interceptor-ref name="login"></interceptor-ref>
      <interceptor-ref name="defaultStack"></interceptor-ref>
      </interceptor-stack>
      </interceptors>
      <default-interceptor-ref name="my"></default-interceptor-ref>
      <action name="loginAction" class="com.sunny.action.LoginAction">
      <result>/success.jsp</result>
      <result name="error">/error.jsp</result>
      </action>
      </package>
  • by package Wrap up some <action> Configure interceptors , Need to be in <action> By setting <interceptor-ref> To set up interceptors , If you want to execute defaultStack, You need to in <action> Middle configuration
    • Do not configure interceptor stack

       <package name="default" namespace="/" extends="struts-default">
      <interceptors>
      <interceptor name="login" class="com.sunny.interceptor.LoginIntercept"></interceptor>
      </interceptors>
      <action name="loginAction" class="com.sunny.action.LoginAction">
      <result>/success.jsp</result>
      <result name="error">/error.jsp</result>
      <interceptor-ref name="login"></interceptor-ref>
      <interceptor-ref name="defaultStack"></interceptor-ref>
      </action>
      </package>
    • Configure the interceptor stack
       <package name="default" namespace="/" extends="struts-default">
      <interceptors>
      <interceptor name="login" class="com.sunny.interceptor.LoginIntercept"></interceptor>
      <interceptor-stack name="my">
      <interceptor-ref name="login"></interceptor-ref>
      <interceptor-ref name="defaultStack"></interceptor-ref>
      </interceptor-stack>
      </interceptors>
      <action name="loginAction" class="com.sunny.action.LoginAction">
      <result>/success.jsp</result>
      <result name="error">/error.jsp</result>
      <interceptor-ref name="my"></interceptor-ref>
      </action>
      </package>

2、Struts2 Custom interceptor implementation example : Determine whether to log in

This example is mainly used to verify whether the user logs in , If you don't log in , Jump to error.jsp page , Prompt to log in to the system .

Interceptor class :

 public class LoginIntercept extends AbstractInterceptor {
@Override
public String intercept(ActionInvocation invocation) throws Exception {
Map session = ServletActionContext.getContext().getSession();
if (session.get("user")==null) {
return "error";
} else {
return invocation.invoke();
}
}
}

Action class :

 public class LoginAction extends ActionSupport {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String execute() throws Exception {
return "success";
}
}

struts.xml The configuration file :

 <struts>
<constant name="struts.devMode" value="true" /> <package name="default" namespace="/" extends="struts-default">
<interceptors>
<interceptor name="login" class="com.sunny.interceptor.LoginIntercept"></interceptor>
<interceptor-stack name="my">
<interceptor-ref name="login"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</interceptor-stack>
</interceptors>
<action name="loginAction" class="com.sunny.action.LoginAction">
<result>/success.jsp</result>
<result name="error">/error.jsp</result>
<interceptor-ref name="my"></interceptor-ref>
</action>
</package>
</struts>

input.jsp page :

 <body>
<form action="${pageContext.servletContext.contextPath}/loginAction.action">
full name :<input type="text" name="name"><br>
<input type="submit" value=" Submit ">
</form>
</body>

error.jsp page :

 <body>
Please log in to the system
</body>

Login screen :

Because I didn't log in to the system , So click Submit , Will be displayed :

Struts2 Learning notes ( Ten )—— More articles on custom interceptors

  1. SpringBoot Learning notes : Custom interceptors

    SpringBoot Learning notes : Custom interceptors Quick start Interceptors are like filters , But interceptors provide more sophisticated control , It can be intercepted by two nodes in the process of a request : Before the request is sent to Controller Before In response to sending ...

  2. [ original ]java WEB Learning notes 74:Struts2 Way of learning -- Custom interceptors ,struts Built in interceptors

    The purpose of this blog :① Summarize your learning process , It's like learning notes ② Share your experience with you , Learn from each other , Communicate with each other , Not for commercial use There are bound to be problems with the content , Welcome to correct , communication , discuss , You can leave a message , You can also contact through the following ways . I love internet technology ...

  3. 【Struts2 Learning notes -6--】Struts2 The interceptor

    The use of simple interceptors The most basic use of interceptors : Interceptors for intercepting methods The execution sequence of interceptors The listener that intercepts the results - amount to The rear interceptor Execution order : Override the parameters of a specific interceptor in the interceptor stack Use interceptors to complete - Access control It mainly completes two functions : ...

  4. Struts2 Learning notes 04 And Interceptor

    One . Create interceptor components 1. Create a class , Realization Interceptor Interface , And implement intercept Method 2. Register interceptors 3. Reference interceptor Two . Interceptor stack Preset interceptors : The default reference interceptor Interceptor call order : Fil ...

  5. [ SSH frame ] Struts2 Frame learning 4 ( Custom interceptors )

    One .Struts2 Interceptor 1.1 Interceptor Overview Interceptor , stay AOP( Aspect-Oriented Programming) Before a method or field is accessed , Intercept and then add some actions before or after . Intercept ...

  6. struts2 Frame learning notes 6: Interceptor

    The interceptor is Struts2 Realize the core part of the function The creation of interceptors : The first one is : package interceptor; import com.opensymphony.xwork2.ActionInvocati ...

  7. Struts2 Study :interceptor( Interceptor ) Use

    For those who need login verification . Permission verification and other functions of the website , Every request , every last action Write a piece of verification code , It's redundant and hard to maintain .struts2 Provides interceptors interceptor, Provide a section for these pages , Or public groups ...

  8. Struts2 The fourth day of study —— Interceptor and file upload

    1. summary Struts2 Many of our core functions are done by interceptors . The interceptor is well implemented AOP Programming ideas , Before the execution of the action and after the return of the result , Do interception . 2.struts2 The default interceptor stack for 3. Custom interceptors St ...

  9. Struts2 Learning the second 4 God -- Interceptor

    The first 1 Chapter      Struts2_day04 note 1.1      Review of the last lesson l  OGNL expression n  OGNL Overview u  OGNL: Object graph Navigation Language , Is a powerful expression language . n  OGN ...

Random recommendation

  1. Django Cache system settings

    Reference resources : http://lesliezhu.github.io/public/2016/04/19/django-cache.html http://www.opscoder.info/django_c ...

  2. python Object oriented __init__ How to understand ?

    We are learning python Class time , There are always classes in books __init__() Such a function , Many students are puzzled , It's actually python Construction method of . The construction method is similar to init() This initialization method , To initialize the new ...

  3. centos Lower installation nginx

    1.yum install nginx     install nginx 2.service nginx start   start-up nginx 3. Then go to the browser , Input http://Ip/ test , If you see Welcome ...

  4. TextView in gravity Attribute value measurement

    Attributes Explain top Do not change control size , Align to the top of the container bottom Do not change control size , Align to the bottom of the container left Do not change control size , Align to the left side of the container right Do not change control size , Align to ...

  5. Swift Array addition operator usage :array1 += array2

    var stringList1 = [String]() // establish String Type empty array var stringList2 = ["1", "3", " ...

  6. Session note

    One .Session Mechanism session The mechanism used is in server End hold HTTP Solution of state information . server Use a hash table like structure ( It could be using hash tables ) To save information . When the program needs to be for a cli ...

  7. ACM Lecture experience

  8. AngularJS in service,factory,provider The difference between

    One .service guide Just beginning to learn Angular When , The component that is often misunderstood and asked by beginners is service(), factory(), and provide() The difference between these methods .This is whe ...

  9. python The reflection function of (hasattr()、getattr()、setattr() And delattr()) And built-in properties of classes attr(__getattr()__、__setattr()__ And __delattr()__)

    Mainly means that the program can access . The ability to detect and modify its own state or behavior ( introspection ), There are four introspective functions . hasattr(object,name) Judge object If there name Properties or methods corresponding to strings , return Tr ...

  10. Centos7 Deploy kubernetes Proxy( 7、 ... and )

    1. To configure kube-proxy Use LVS( All three nodes are installed ) [root@linux-node1 ssl]# yum install -y ipvsadm ipset conntrack [root@l ...