Search

Spring Multi Action Controller Tutorial / Example

Using Spring MultiActionController class you can group related actions into a single controller class. The handler method for each action should be in the following form.
public (ModelAndView | Map | String | void) actionName(HttpServletRequest, HttpServletResponse [,HttpSession] [,CommandObject]);
 To group multiple actions your controller class should extend MultiActionController class. Here theUserController class extends the MultiActionController class and contains the add() and the remove()method as shown below.
package com.vaannila.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction. MultiActionController;

public class UserController extends MultiActionController {

public ModelAndView add(HttpServletRequest request,
HttpServletResponse response) throws Exception {
System.out.println("Add method called");
return new ModelAndView("user""message""Add method called");
}

public ModelAndView remove(HttpServletRequest request,
HttpServletResponse response) throws Exception {
System.out.println("Remove method called");
return new ModelAndView("user""message""Remove method called");
}
}
Here we use the BeanNameUrlHandlerMapping to map the request url. The Spring bean configuration file is shown below.
<?xml version="0" encoding="UTF-8"?>

<bean id="viewResolver" class="org.springframework.web.servlet.view. InternalResourceViewResolver" p:prefix="/WEB-INF/jsp/"p:suffix=".jsp" />

<bean name="/user/*.htm" class="com.vaannila.web.UserController" />

</beans>
So to map multiple actions, we use the asterisk character. Anything that matches the asterisk character will be considered as the method name in the UserController class.
In the redirect.jsp page we have two urls one to call the add() method and the other to call theremove() method.
<a href="user/add.htm" >Add</a>

<a href="user/remove.htm" >Remove</a>

Spring Interceptor Using Annotation Tutorial / Example

If you are using annotated Spring controllers, the only change that you need to do to the previous interceptor example is the configuration. In the Spring bean configuration file instead of usingBeanNameUrlHandlerMapping or any other handler mapping use DefaultAnnotationHandlerMapping. The configuration file is shown below.
<?xml version="0" encoding="UTF-8"?>

<bean id="viewResolver" class="org.springframework.web.servlet.view. InternalResourceViewResolver" p:prefix="/WEB-INF/jsp/"p:suffix=".jsp" />

<bean class="org.springframework.web.servlet.mvc.annotation. DefaultAnnotationHandlerMapping" p:interceptors-ref="loggerInterceptor" />

<context:component-scan base-package="com.vaannila.web" />

<bean id="loggerInterceptor"class="com.vaannila.interceptor.LoggerInterceptor" />

<bean id="userService" class="com.vaannila.service.UserServiceImpl" />

</beans>

Spring Interceptor Tutorial / Example

Spring Interceptors has the ability to pre-handle and post-handle the web requests. Each interceptor class should extend the HandlerInterceptorAdapter class. Here we will create a Logger Interceptor by extending the HandlerInterceptorAdapter class. You can override any of the three callback methodspreHandle(), postHandle() and afterCompletion(). As the names indicate the preHandle() method will be called before handling the request, the postHandle() method will be called after handling the request and the afterCompletion() method will be called after rendering the view.
In each method we will log information using log4j. First instantiate the logger in the static context, then set up the basic configuration so that the log messages will be logged on the console.
The LoggerInterceptor class is shown below.
package com.vaannila.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler. HandlerInterceptorAdapter;

public class LoggerInterceptor extends HandlerInterceptorAdapter {

static Logger logger = Logger.getLogger(LoggerInterceptor.class);

static{
BasicConfigurator.configure();
}

@Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler) throws Exception {
logger.info("Before handling the request");
return super.preHandle(request, response, handler);
}

@Override
public void postHandle(HttpServletRequest request,
HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
logger.info("After handling the request");
super.postHandle(request, response, handler, modelAndView);
}

@Override
public void afterCompletion(HttpServletRequest request,
HttpServletResponse response, Object handler, Exception ex)
throws Exception {
logger.info("After rendering the view");
super.afterCompletion(request, response, handler, ex);
}
}
 Now the logger interceptor is created you need to associate this interceptor with the handler mapping. Here we use BeanNameUrlHandlerMapping, incase you are using more than one handler mapping you need to associate the interceptor with each one of them. The code below shows how to associate an interceptor with the handler mapping.
<?xml version="0" encoding="UTF-8"?>

<bean id="viewResolver" class="org.springframework.web.servlet.view. InternalResourceViewResolver" p:prefix="/WEB-INF/jsp/"p:suffix=".jsp" />

<bean id="handlerMapping"class="org.springframework.web.servlet.handler. BeanNameUrlHandlerMapping" p:interceptors-ref="loggerInterceptor" />

<bean id="loggerInterceptor"class="com.vaannila.interceptor.LoggerInterceptor" />

<bean id="userService" class="com.vaannila.service.UserServiceImpl" />

<bean name="/userRegistration.htm"class="com.vaannila.web.UserController" p:userService-ref="userService" p:formView="userForm" p:successView="userSuccess"/>

</beans>
When you execute the example you can see the log messages getting dispalyed on the console.
Download Spring examples source code