Use spring built-in Interceptor to print a sentence before each method call for struts actions

Source: Internet
Author: User

Although Spring provides many built-in interceptors, I will show you how to create your own interceptor and apply it to a struts action. To use the interceptor, you need to do three things:

1. Create an interceptor;

2. register the interceptor;

3. Declare where to intercept the code.

This seems very simple but powerful. For example, in listing 7, I created a logging interceptor for the struts action. This interceptor prints a sentence before each method call:

Listing 7. A simple logging interceptor

package ca.nexcel.books.interceptors;

import org.springframework.aop.MethodBeforeAdvice;

import java.lang.reflect.Method;

public class LoggingInterceptor implements MethodBeforeAdvice {

public void before(Method method, Object[] objects, Object o) throws Throwable {

System.out.println("logging before!");

}

}

This interceptor is very simple. The before () method runs before each method in the interception point. In this example, it prints a sentence and can actually do anything you want. The next step is to register the interceptor in the spring configuration file, as shown in listing 8:

Listing 8. register the interceptor in the spring configuration file

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" 

"http://www.springframework.org/dtd/spring-beans.dtd">

<beans>

<bean id="bookService" class="ca.nexcel.books.business.BookServiceImpl"/>

<bean name="/searchSubmit" 

class="ca.nexcel.books.actions.SearchSubmit">

<property name="bookService">

<ref bean="bookService"/>

</property>

</bean>

<!--  Interceptors --> 

<bean name="logger"    

class="ca.nexcel.books.interceptors.LoggingInterceptor"/> |(1)

<!-- AutoProxies -->

<bean name="loggingAutoProxy" 

class="org.springframework.aop.framework.autoproxy.

BeanNameAutoProxyCreator"> |(2)

<property name="beanNames">

<value>/searchSubmit</valuesgt; |(3)

</property>

<property name="interceptorNames">

<list>

<value>logger</value> |(4)

</list>

</property>

</bean>

</beans>

As you may have noticed, listing 8 extends the application shown in Listing 6 to include an interceptor. The details are as follows:

◆ At (1), I registered the interceptor.

◆ At (2), I created an automatic bean name proxy, which describes how to apply the interceptor. There are other methods to define interception points, but this method is common and simple.

◆ At (3), I register the struts action as the bean to be intercepted. To intercept other struts actions, you only need to create an additional <value> tag under "beannames.

◆ At (4), when the interception occurs, I executed the name of the interceptor bean created at (1. All the interceptors listed here are applied to "beannames ".

That's it. As shown in this example, placing your struts actions under the control of the Spring framework provides a series of new options for processing your struts applications. In this example, the spring interceptor can be used with the action delegate to easily improve the logging capability in the struts application.

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.