Control interceptor of Struts2 learning notes

Source: Internet
Author: User
Tags i18n

This article will introduce you to the Struts2 control interceptor. I hope this tutorial will be helpful to you.

The role of the interceptor is similar to that of the filter used for development based on the strtus framework.


Configure the default Interceptor. the interceptor name here is defaultStack.

The Code is as follows: Copy code
<Default-interceptor-ref name = "defaultStack"/>

 
Declare the Interceptor. The name of the interceptor we declare here is myInterceptor.

The Code is as follows: Copy code
<Interceptors>
<Interceptor name = "myInterceptor" class = "org. Rudiment. interceptor. myInterceptor"/>
</Interceptors>

The Interceptor is used. Here, the interceptor name is myInterceptor.

The Code is as follows: Copy code
<Action name = "ltlogin" class = "org. Rudiment. action. LoginAction" method = "checkSome">
<Interceptor-ref name = "myInterceptor"/>
<Result name = "success">/welcom. jsp </result>
<Result name = "input">/login. jsp </result>
</Action>


======================== Actual example ============================ =


1. Interceptor source code

The Code is as follows: Copy code

Package org. Rudiment. interceptor;

Import com. opensymphony. xwork2.ActionInvocation;
Import com. opensymphony. xwork2.interceptor. Interceptor;

Public class myInterceptor implements Interceptor
{

@ Override
Public void destroy (){
System. out. println ("destroyed ");
}

@ Override
Public void init (){
System. out. println ("created ");
}

@ Override
Public String intercept (ActionInvocation invacation) throws Exception {
System. out. println ("intercepted ");
/* When the invoke () method is called, the interceptor releases the current request */
Invacation. invoke ();
Return null;
}

}


2. strtus. xml configuration file

The Code is as follows: Copy code

<? Xml version = "1.0" encoding = "UTF-8"?>
<! DOCTYPE struts PUBLIC "-// Apache Software Foundation // DTD Struts Configuration 2.1 // EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<Struts>
<Constant name = "struts. i18n. encoding" value = "GBK"/>
<Constant name = "struts. custom. i18n. resources" value = "mess"/>
<Constant name = "struts. ognl. allowStaticMethodAccess" value = "true"/>
<Package name = "default" extends = "struts-default">
<! -- Declare interceptor -->
<Interceptors>
<Interceptor name = "myInterceptor" class = "org. Rudiment. interceptor. myInterceptor"/>
</Interceptors>
<! -- Use the default interceptor when no declarative interceptor is displayed. Note that this configuration item is later than </interceptors>; otherwise, an error occurs due to order. -->
<Default-interceptor-ref name = "defaultStack"/>
<Action name = "ltlogin" class = "org. Rudiment. action. LoginAction">
<! -- Use interceptor -->
<Interceptor-ref name = "myInterceptor"/>
<! -- If you want the default Interceptor to take effect for this action, you need to use it. -->
<Interceptor-ref name = "defaultStack"/>
<Result name = "success">/welcom. jsp </result>
</Action>
</Package>
</Struts>

3. Action configuration file

The Code is as follows: Copy code

Package org. Rudiment. action;


Import org. apache. struts2.ServletActionContext;
Import com. opensymphony. xwork2.ActionSupport;


Public class LoginAction extends ActionSupport
{
@ Override
Public String execute () throws Exception
{
System. out. println ("execute ()");
Return SUCCESS;
}
}

 

4. JSP file

The Code is as follows: Copy code

<% @ Page language = "java" import = "java. util. *" pageEncoding = "GBK" %>
<% @ Taglib prefix = "s" uri = "/struts-tags" %>
<Html>
<Head>
<Title> test </title>
</Head>
<Body>
<S: form action = "ltlogin" validate = "">
<S: submit value = "Conversion"/>
</S: form>
</Body>
</Html>


Note: When we explicitly use our interceptor in action. The default interceptor does not take effect. If you want the default Interceptor to take effect, you need to explicitly declare

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.