Thursday, February 17, 2011

MultiActionController in Spring MVC

Under Spring MVC package, i could find an API “MultiActionController”, through which i can forward request to any of the action method in a controller class. Have put a simple code snippet briefing the configuration of MultiActionController.

• Write a java class extending “MultiActionController”.
Extend MultiActionController in SessionController class and group multiple action methods related to session. Eg: login and logout.

package com.jb.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 SessionController extends MultiActionController {
     
    public ModelAndView login(HttpServletRequest request,
            HttpServletResponse response) throws Exception {
   String message = “Login method called”;
        System.out.println(message);
        return new ModelAndView("user", "message", message);
    }
     
    public ModelAndView logout(HttpServletRequest request,
            HttpServletResponse response) throws Exception {
     String message = “Logout method called”;
        System.out.println(message);
        return new ModelAndView("user", "message", message);
    }
}

• Configure the controller in spring bean file.
Here is a spring bean configuration file to map a request url to MultiActionController. An asterisk character can be used to define request url.
Characters toning asterisk will be considered as method name in SessionController.



 
    
         
    
 


• Invoking login and logout action methods from jsp.
Login and Logout methods can be invoked by posting request on following url from jsp.
  Login 
  Logout 

No comments:

Post a Comment