Struts2 summary interceptor

Source: Internet
Author: User
Tags i18n
 
<!-- Basic stack -->            <interceptor-stack name="basicStack">                <interceptor-ref name="exception"/>                <interceptor-ref name="servletConfig"/>                <interceptor-ref name="prepare"/>                <interceptor-ref name="checkbox"/>                <interceptor-ref name="multiselect"/>                <interceptor-ref name="actionMappingParams"/>                <interceptor-ref name="params">                    <param name="excludeParams">dojo\..*,^struts\..*</param>                </interceptor-ref>                <interceptor-ref name="conversionError"/>            </interceptor-stack>

The "basicstack" interceptor is the core interceptor of struts2. It is called the "interceptor stack" and contains references from several interceptors in the "interceptor stack. If "Interceptor" is compared to a file, "Interceptor" is equivalent to a folder. The "Interceptor stack" named "basicstack" contains eight interceptors (these eight interceptors are defined at the top of the document ),
 <!-- Basic stack -->            <interceptor-stack name="basicStack">                <interceptor-ref name="exception"/>                <interceptor-ref name="servletConfig"/>                <interceptor-ref name="prepare"/>                <interceptor-ref name="checkbox"/>                <interceptor-ref name="multiselect"/>                <interceptor-ref name="actionMappingParams"/>                <interceptor-ref name="params">                    <param name="excludeParams">dojo\..*,^struts\..*</param>                </interceptor-ref>                <interceptor-ref name="conversionError"/>            </interceptor-stack>

We noticed that there is an "Interceptor stack" in the "Interceptor stack", so we can say that the "Interceptor stack" can also be used as the "Interceptor" to use the default Interceptor:

<default-interceptor-ref name="defaultStack"/>

1) define the interceptor <interceptor name = "" class = "> 2) define the interceptor stack in the package.

<interceptor-stack name="">      <interceptor-ref name=""> </interceptor-stack> 
3) define the default interceptor in the package <default-interceptor-ref name = ""/> 4) Action define the interceptor

<Action name = "" class = ""> <! -- Indicates that the interceptor is called before the action is accessed --> <Interceptor-ref name = ""> <Param name = ""> </param> </Interceptor-ref> </ action>
Custom Interceptor:
<Struts> <package name = "action" extends = "struts-Default"> <interceptors> <interceptor name = "mylogger" class = "com. interceptor. loggerinterceptor "/> <interceptor name =" mylogin "class =" com. interceptor. logininterceptor "/> </interceptors> <action name =" list "class =" com. action. listaction "> <Interceptor-ref name =" mylogger "/> <Interceptor-ref name =" defaultstack "/> // you must add the default interceptor that will be overwritten if not written <result name = "success">/LIS T. JSP </result> </Action> <action name = "user" class = "com. action. useraction "> <Interceptor-ref name =" mylogin "> <Param name =" includemethods "> add, delete, update </param> </Interceptor-ref> <Interceptor-ref name = "mylogger"/> <Interceptor-ref name = "defaultstack"/> <! -- View --> <result name = "look">/look. jsp </result> <! -- Update the initial display --> <result name = "LOAD">/updateemp. jsp </result> <! -- Add, update, and delete --> <result name = "list" type = "redirectaction"> <Param name = "actionname"> List </param> <Param name = "namespace ">/</param> </result> </Action> </package> </struts>

Custom Interceptor:
Public class logininterceptor extends methodfilterinterceptor {@ overrideprotected string dointercept (actioninvocation Invocation) throws exception {Map <string, Object> session = invocation. getinvocationcontext (). getsession (); Object User = (object) session. get ("user"); If (user = NULL) {return "login" ;}return invocation. invoke (); // call the subsequent interceptor or action business method }}

The actioninvocation class encapsulates all the APIS required to call an action.
3. Resource file and internationalization (i18n) * We learned the five major components of struts2 (valuestack, interceptor, result, action, and tag library ). Now, let's start learning about some tips for returning components: internationalization, error processing, form verification, and so on. Resource file internationalization (also known as i18n, with 18 letters in the middle ending with N starting with I ). The core of internationalization: The text displayed on the page is configurable (such as Chinese, English, and Japanese, and cannot be written to the page ). 1) resource files are generally attribute files *. properties 2) for Chinese characters 1myeclipse8. 5. Unicode encoding can be easily obtained. 2. Use the command tool native2ascii provided by JDK. 3) Naming and location of resource files (important, all resource files are stored in the same directory. Resource files need to be layered and classified. A package-level resource file indicates that a resource file is used by all actions in a package. A Class-level resource file indicates that a resource file can only be used by a specified class. 4) action must inherit from the actionsupport class that has encapsulated access to resource files. If action does not have the class attribute in the configuration file, the actionsupport class is called.

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.