Simple implementation of interceptor in STRUTS2

Source: Internet
Author: User

Struts2 interceptors are similar to servlet filters. Before the Execute method of the action is executed, STRUTS2 first executes the reference in Struts.xml
Interceptor that executes the Execute method of the action after all the intercept methods of the referenced interceptor have been executed. In the Struts2 interceptor system,
Struts2 's built-in interceptors complete most of the framework's operations, so the actual development process usually uses a system interceptor. Of course we can also develop our own interceptors, similar to the development of filters.
Principle:
The Struts2 interceptor is relatively simple to implement, and when you request STRUTS2 action, STRUTS2 looks for the configuration file and instantiates the relative
The Interceptor object is then strung into a list, the last one in the invocation list of the interceptor.
The process of implementing a user-defined interceptor:
1. Customize the Interceptor class to implement the Com.opensymphony.xwork2.interceptor.Interceptor interface or inherit COM.OPENSYMPHONY.XWORK2.
Interceptor. Abstractinterceptor class.
2. Define a custom interceptor in Struts.xml.
3. Use interceptors in the action of Struts.xml.
Example:
Helloaction.java the action class in the instance to verify the form data
Package com.action;

Import com.opensymphony.xwork2.Action;
Import Com.opensymphony.xwork2.ActionSupport;

public class Helloaction extends Actionsupport {
Private String username;
Private String Pass1;
Private String Pass2;

Public String GetUserName () {
return username;
}
public void Setusername (String username) {
This.username = Username;
}
Public String GetPass1 () {
return pass1;
}
public void SetPass1 (String pass1) {
This.pass1 = Pass1;
}
Public String GetPass2 () {
return pass2;
}
public void SetPass2 (String pass2) {
This.pass2 = Pass2;
}
Public String Execute () throws Exception {
if (Username!=null&&getpass1 (). Equals (GetPass2 ()) &&!getusername (). Trim (). Equals ("")) {
SYSTEM.OUT.PRINTLN ("Executing action ...");
return action.success;
}
else{
SYSTEM.OUT.PRINTLN ("Executing action ...");
return action.input;
}
}

}

Hellointerceptor.java is the Interceptor class
Package com.inter;

Import com.opensymphony.xwork2.ActionInvocation;
Import Com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class Hellointerceptor extends Abstractinterceptor {
Private static final long serialversionuid = 1L;
@Override
Public String Intercept (Actioninvocation arg0) throws Exception {
System.out.println ("The Interceptor is running ...");
String result = Arg0.invoke ();
System.out.println ("The Interceptor is ending ...");
return result;
}
}
User-defined interceptors must implement interceptor interfaces or directly inherit Abstractinterceptor. The main function of the Intercept method is to achieve the interception action
, the parameter actioninvocation is a reference to the Intercept action, which can be passed to the next interceptor or action's Execute method by invoking the Invoke method of the parameter.


In Struts.xml configuration:
<?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>
<package name= "Hellpinterceptor" extends= "Struts-default" >
<interceptors>
<interceptor name= "Hellointerceptor" class= "Com.inter.HelloInterceptor" ></interceptor>
</interceptors>
<action name= "helloaction" class= "Com.action.HelloAction" >
<result name= "Success" >/success.jsp</result>
<result name= "Input" >/reg.jsp</result>
<interceptor-ref name= "Defaultstack" ></interceptor-ref>
<interceptor-ref name= "Hellointerceptor" ></interceptor-ref>
</action>
</package>
</struts>
The required interceptors are configured in the Interceptor tag, and the interceptor tag is used to configure a specific interceptor, Hellointerceptor is configured here. The use of interceptors in every specific format
Action, the INTERCEPTOR-REF tag in the action tag uses the interceptor that is still defined in the interceptor tag. This is the Strtus.xml file.


Reg.jsp Code:
<%@ page language= "java" import= "java.util.*" pageencoding= "Utf-8"%>
<%@ taglib prefix = "s" uri = "/struts-tags"%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
&LT;TITLE&GT;STRUTS2 Interceptor Application </title>
<body>
<s:form id= "id" action= "helloaction" >
<s:textfield name= "username" label= "username" ></s:textfield>
<s:password name= "Pass1" label= "password" ></s:password>
<s:password name= "Pass2" label= "Duplicate password" ></s:password>
<s:submit value= "Register" ></s:submit>
</s:form>
</body>

Success.jsp Code:
<%@ page language= "java" import= "java.util.*" pageencoding= "Utf-8"%>
<%@ taglib prefix= "s" uri= "/struts-tags"%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
&LT;TITLE&GT;STRUTS2 Interceptor Application </title>
<body>
</body>



Some of the information that runs is as follows:
Info: At least one JAR is scanned for TLDs yet contained no TLDs. Enable debug logging for the logger for a complete list of JARs that were scanned and no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
The Interceptor is running ...
Executing action ...

The Interceptor is ending ...


When implementing the Ntercept method in an interceptor, the Actioninvocation parameter can be obtained to obtain the intercepted action instance, and once the instance has been obtained, it is possible to parse the parameters in the HTTP request and set the properties of the action. You can also transfer HttpServletRequest instances and HttpServletResponse instances from an HTTP request directly to an action.

Simple implementation of interceptor in STRUTS2

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.