Simple Struts2 interceptor example

Source: Internet
Author: User

Interceptor) is the core component of Struts 2. A lot of features) are built on the interceptor, such as file upload and download, internationalization, converter and data validation, Struts 2 uses the built-in interceptor, completed most of the operations in the framework. In Struts 2, the interceptor is interpreted as the object that dynamically intercepts Action calls. It provides a mechanism for developers to define a specific function module. This module can be run before or after an Action is executed, or stop the Action execution before an Action is executed. It also provides a way to extract reusable parts of an Action interceptor in Struts2: 650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'alt = "" src = "http://www.bkjia.com/uploads/allimg/131228/140401OW-0.jpg"/> it can be seen that the Action of Struts 2 architecture is surrounded by one or more interceptor stacks, all user requests are intercepted by the interceptor and then sent to the Action for processing. The processing results are returned to the user in a logical view. The invocation process is implemented by the Struts 2 configuration file, which will be detailed later. Interceptor is one of the core parts of Struts 2. When a user requests to reach the ServletDispatcher of Struts 2, Struts 2 searches for the configuration file, instantiates the relative interceptor object based on the configuration, and then Concatenates the object into a List ), the sequence of the interceptor in the list is shown in 650) this. width = 650; "onclick = 'window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/1404015618-1.jpg "/> In the Struts 2 architecture, Action calls are implemented through the interceptor. Some readers may wonder why the interceptor is not clearly stated and why the Action can be called directly? That's becauseStruts 2 architecture if no explicit interceptor is configured, the system will call the default Interceptor to call ActionIn the user's opinion, it seems that no interceptor is configured..Hello World interceptor example.The program must demonstrate how to explicitly allow the Interceptor to call the Action to realize the purpose of the interceptor. Generally, we write an Action first, and then configure the struts. xml file.MyAction. javaPublic class MyAction extends ActionSupport {

// The following attributes are obtained from the front-end JSP page)
Private String username;

Private String mymsg;

Private String password1;

Private String password2;

Private Date birthday;

Public String execute (){
If (username! = Null & this. getPassword1 (). equals (this. getPassword2 ())&&! This. getUsername (). trim (). equals ("")){

// Output debugging information
System. out. println ("Action information, executing Action ....");
Return SUCCESS;
} Else {
Return INPUT;
}
}



Public String getUsername (){
Return username;
}



Public void setUsername (String username ){
This. username = username;
}



Public String getMymsg (){
Return mymsg;
}

Public void setMymsg (String mymsg ){
This. mymsg = mymsg;
}

Public String getPassword1 (){
Return password1;
}

Public void setPassword1 (String password1 ){
This. password1 = password1;
}

Public String getPassword2 (){
Return password2;
}

Public void setPassword2 (String password2 ){
This. password2 = password2;
}

Public Date getBirthday (){
Return birthday;
}

Public void setBirthday (Date birthday ){
This. birthday = birthday;
}

}
Custom interceptorMyInterceptor. javaPublic class MyInterceptor extends actinterceptor {

// Interception Method
Public String intercept (ActionInvocation invocation) throws Exception {

MyAction myA = (MyAction) invocation. getAction ();
System. out. println ("Interceptor information: hello world Interceptor ");
// Execute action or the next interceptor
String result = invocation. invoke ();
System. out. println ("Interceptor information: completed Action ");
Return result;
}

} Now let's configure the struts. xml file. First, I defined a my. xml file. <? Xml version = "1.0" encoding = "UTF-8"?>
<! DOCTYPE struts PUBLIC
"-// Apache Software Foundation // DTD Struts Configuration 2.0 // EN"
Http://struts.apache.org/dtds/struts-2.0.dtd>

<Struts>

<Package name = "mynew" namespace = "/" extends = "struts-default">

<Interceptors>
<Interceptor name = "myInterceptor" class = "com. MyInterceptor">
</Interceptor>
</Interceptors>

<Action name = "myAction" class = "com. MyAction">
<Result name = "success">/success. jsp </result>
<Result name = "input">/index. jsp </result>
<! -- Reference the default interceptor -->
<Interceptor-ref name = "defaultStack"> </interceptor-ref>
<! -- Reference a custom interceptor -->
<Interceptor-ref name = "myInterceptor"> </interceptor-ref>
</Action>
</Package>
</Struts>
In this case, we need to introduce it in struts. xml, <? Xml version = "1.0" encoding = "UTF-8"?>
<! DOCTYPE struts PUBLIC
"-// Apache Software Foundation // DTD Struts Configuration 2.0 // EN"
Http://struts.apache.org/dtds/struts-2.0.dtd>

<Struts>
<Constant name = "struts. devMode" value = "true"/>
<Include file = "my. xml"> </include>
</Struts>
If you are not used to it, you can write it in the struts. xml file.Index. jsp<% @ Page language = "java" import = "java. util. *" pageEncoding = "UTF-8" %>
<% @ Taglib prefix = "s" uri = "/struts-tags" %>
<Html>
<Head>
</Head>

<Body>
<S: form method = "post" action = "myAction">
<S: textfield name = "username" label = "username"> </s: textfield>
<S: password name = "password1" label = "password"> </s: password>
<S: password name = "password2" label = "Confirm password"> </s: password>
<S: submit value = "register"> </s: submit>
</S: form>
</Body>
</Html>
Success. jsp<% @ Page language = "java" contentType = "text/html; charset = UTF-8"
PageEncoding = "UTF-8" %>
<% @ Taglib prefix = "s" uri = "/struts-tags" %>
<! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd">
<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
<Title> Insert title here </title>
</Head>
<Body>
<H3> Registration successful Username: <s: property value = "username"/> <p>
Password: <s: property value = "password1"/>
</Body>
</Html> background output result: Interceptor information: hello world interceptor
Action information, executing Action ....
2010-11-1 20:39:11 com. opensymphony. xwork2.util. logging. commons. CommonsLogger warn
Warning cocould not find property [org. apache. catalina. jsp_file]
Interceptor information: Action completed

This article is from the blog "make a little progress every day". For more information, contact the author!

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.