Java struts2 getting started-interceptor Learning

Source: Internet
Author: User

1. interceptor and interceptor stack 1. the interceptor's role is essentially the same as the servlet filter. In struts2, the interceptor can intercept the Action before and after the Action. The Interceptor is pluggable. You can choose to use the interceptor or uninstall the interceptor. 2. The sequence of interceptor execution is in the struts. xml file. <intercepto-ref/> is first referenced and then executed. If an interceptor fails or is not allowed to pass, the next interceptor cannot be executed. Configure the Action in the <Action> tag corresponding to the action to intercept. When you deploy a web application, the empty parameter constructor and the init () method of the interceptor are executed once each time, and the intercept () method is executed once each request. 3. Custom interceptor 1) declare an interceptor to copy the Code <pacakge> <interceptors> <interceptor name = "LoginInterceptor" class = "interceptor. loginInterceptor "> </interceptor> </interceptors> <action> </package> copy the code. Note: The interceptors and action labels are the same. The class corresponds to the full path of the interceptor. The name corresponds to the interceptor name, which can be defined by yourself. We recommend that you use the same name as the class name, which must be unique. 2) reference an interceptor: <action> <! -- Reference a custom interceptor --> <interceptor-ref name = "LoginInterceptor"> </interceptor-ref> <action> Note: How to reference an interceptor? You can configure it under the <Action> label in action. Here, the name must be the same as the custom name. 4. Custom interceptor stack 1) declare an interceptor stack interceptor-stack tag to configure the interceptor to be referenced, as shown below: copy the Code <interceptors> <interceptor name = "LoginInterceptor" class = "interceptor. loginInterceptor "> </interceptor> <interceptor-stack name =" amosStack "> <interceptor-ref name =" LoginInterceptor "> </interceptor-ref> <interceptor-ref name =" defaultStack "> </interceptor-ref> </interceptor-stack> </interceptors> copy Code 2) reference an interceptor Stack: <! -- Reference the custom interceptor Stack --> <interceptor-ref name = "amosStack"> </interceptor-ref> 2. analyze the requirements and access upload. jsp to upload files-> LoginInterceptor Interceptor to intercept-> 1. Not logged on, go to the logon page> logon success> and click back to the Upload File page. 2. log on and start uploading files directly. The code for file upload is mainly referenced in the previous article. Therefore, we will focus on the Code Analysis of LoginInterceptor. java Interceptor: implements the Interceptor interface and overwrites its method. Here we will focus on the intercept () method. Here, the ActionInvocation parameter is used to obtain the session value to determine whether the user has logged on. 1) if no login return value is "toLoginJsp", this corresponds to the result name in the upload-interceptor.xml, forward it to login. jsp page; 2) If you have logged on, execute the upload, but note that, if the returned value is "success", it will match the content in the configuration file where the result attribute is "success". If the returned value is invocation. invoke (), the UploadAction method will be executed, so remember to return invocation here. invoke (); otherwise, the file upload fails. The configuration of the upload-sturts.xml is also a detailed problem, mainly can refer to the content at the beginning of the 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.