struts Initial Knowledge 6-Interceptor
1. Interceptor use
1.1 Custom Interceptor
-
Method:
1. Create common classes, inherit AbstractIntercepter, and implement abstract methods
2.struts.xml configuration interceptor:
2.1 Declare Interceptor
2.2 Use interceptors:
> Note that all default interceptors do not work after configuring the interceptor > > Solutions: > > 1. Call the default stack Interceptor at every place where the interceptor is used > > Disadvantage: You need to call the default interceptor stack everywhere you use the interceptor > > 2. At your own interceptor, call the default stack interceptor group and add your own interceptor > > Advantages: You don't need to configure multiple interceptors in every place where interceptors are used, just configure your own interceptors. > > Disadvantage: You still need to use interceptors everywhere. > > 3. Configure intercepting XML to configure itself as the default interceptor > > Disadvantage: Intercept everything, and intercept anything that does not need to be intercepted. > > 4. Solution: There is an abstract class in the AbstractIninterceptor subclass - MethodFilterInterceptor: > > excludeMethod: No interception is required > > Include Method: Need to intercept > > Disadvantage: When declaring an interceptor, you don't know which methods need to be intercepted and which don't need to be intercepted. > > 5. Interception in Use
2. Processes
2.1 Execution process
- Interception - > (If there is a next interceptor, let it go to the next interceptor) - > Last interceptor let it go - > Reaching the target resource
3. Practice
3.1 Login Control
-
Logon Interception Class
public class LoginInterceptor extends MethodFilterInterceptor { /** * @Author ANGLE0 * @Description Determine whether there are objects in the session domain * @Date 10:41 2019/7/28 * @Param * @return **/ @Override protected String doIntercept(ActionInvocation actionInvocation) throws Exception { Object obj = ActionContext.getContext().getSession().get("loginStaff"); //Not logged in, pointing to the logon page if (obj == null) { return "login"; } //Logged in return actionInvocation.invoke(); } }
-
strtus.xml configuration interceptor
There are two ways to configure interceptors:
Method 1: Define your own interceptor directly, and then configure the interceptor in action
Method 2: Pack default interceptors and custom interceptors into stacks and then configure them into action
- Method 1
//Custom Interceptor <interceptor name="loginInterceptor" class="com.it.w.sms.web.interceptor.LoginInterceptor"></interceptor> //Reference in action <action name=""> //Reference to the default stack <interceptor-ref name="defaultStack"></interceptot-ref> //Call your own interceptor <interceptor-ref name="loginInterceptor"></interceptot-ref> </action>
- Stack Packing
<interceptors> <!--Custom stack--> <interceptor-stack name="loginInterceptorStack"> <interceptor-ref name="default-stack"></interceptor-ref> <interceptor-ref name="loginInterceptor"></interceptor-ref> </interceptor-stack> </interceptors>
- Declare default
<interceptors> <!--3.1 Declare interceptor, register interceptor--> <interceptor name="loginInterceptor" class="com.it.w.sms.web.interceptor.LoginInterceptor"></interceptor> <!--3.2 Declare the interceptor stack, action Quote--> <interceptor-stack name="loginStack"> <interceptor-ref name="default-stack"></interceptor-ref> <!--Configure methods that do not require interception. exculeMethod Configure methods that do not require interception"Division"--> <interceptor-ref name="loginInterceptor"> <param name="excludeMethods">login</param> </interceptor-ref> </interceptor-stack> </interceptors> <!--3.3 Declare default--> <default-interceptor-ref name="loginStack"></default-interceptor-ref>
3.2 File Upload
Properties of uploaded files provided by 3.2.1 Struts Library
- photoFileName: Upload File Name
- photoContentType: File type
3.2.2 File Cache
- <10KB: Memory as Cache
- > 10KB: Disk temporary file as cache
3.2.3 File Processing Mode
3.2.3.1 copies
- Copy it to the specified location (temporary file processing is still in progress)
3.2.3.2 shear
- Cut the temporary file to the specified location and rename it (the temporary file has been processed and is no longer there)
3.2.4 Modification Limits Upload File Size
- Modifying Constants
- Modifying File Restrictions by Injecting Parameters
3.2.5 Multi-file Upload
3.3 File Download
3.3.1 Small bug
- The variable name of InputStream in the download action class cannot be used in
- Determine whether the picture has been P, the text is open, and whether the first voyage has been E.... Subsample