Thursday, July 8, 2010

spring-urlfilenameviewcontroller-example

MultiActionController is one of the types of Controllers in Spring. It extends AbstractController. (Somewhat similar to DispatchAction in Struts) Why MultiActionController?: It aggregates multiple request handling methods into single controller, so related functionality can be kept together easily.
NOTE:To reduce complexity, I have added business logic in Controller. Later on I will republish this example as per MVC2 standards. Let’s go through the flow of application.

Initially index.jsp is viewed to user,

index.jsp

        

Spring Demo

1) Enter 'abc' as FirstName and 'xyz' as LastName for successful login attempt.

All other combinations will fail.  
First Name: Last Name:

2)

Click here to get 'Hello' Message.

When submit button is pressed or hyperlink is clicked flow will go to WEB.XML from index.jsp

WEB.XML

      FrontControllerServlet  org.springframework.web.servlet.DispatcherServlet      FrontControllerServlet  *.process      FrontControllerServlet  *.do       index.do    

WEB.XML instructs that, ‘*.do and *.process URLs are served by FrontControllerServlet. FrontControllerServlet is internally instantiated with the configuration file FrontControllerServlet-servlet.xml. (Remember Spring naming convention for DispatcherServlet configuration XML is ‘-servlet.xml’)

FrontControllerServlet-servlet.xml

     
  class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping" >                                   validateLogin  goToHome  sayHello       

Inside FrontControllerServlet, Request is given to ‘SimpleUrlHandlerMapping bean’ initially. There request is routed to proper controller bean. In our case *.do and *.process are routed to controller.MyController (extends MultiActionController) bean. Before request goes to MyController, request has to find out proper method that can serve it. Look at ‘PropertiesMethodNameResolver bean’, it maps URL to proper method inside our MultiActionController. Now, request is handed over to controller.MyController class,

controller.MyController.java

package controller; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.multiaction.MultiActionController; /** * @author Gaurang Patel */ public class MyController extends MultiActionController { /** * LOGIN IS SUCCESSFUL ONLY IF FIRSTNAME='abc' and LASTNAME='xyz'. * YOU CAN DEFINE HERE PROPER BUSINESS LOGIC FOR LOGIN. */   public ModelAndView validateLogin
(HttpServletRequest request,HttpServletResponse response){  String firstName = request.getParameter("firstName");  String lastName = request.getParameter("lastName");   if(firstName.equals("abc") && lastName.equals("xyz"))  return new ModelAndView("loginSuccess.jsp");  else  return new ModelAndView("loginFailure.jsp");  }  public ModelAndView sayHello(HttpServletRequest request,HttpServletResponse response)
{  return new ModelAndView("sayHello.jsp");  }  public ModelAndView goToHome(HttpServletRequest request,HttpServletResponse response)
{  return new ModelAndView("index.jsp");  }  }

Here MyController does proper business logic and route request to its relevant JSP and eventually JSP is displayed back to User. (Ideally MyController is not supposed to route request to directly JSP as per MVC2 but to reduce complexity, I have used.) Check out below JSP pages,

loginSuccess.jsp

    HOME 

Welcome.. Login attempt was successful..

loginFailure.jsp

    HOME 

Login attempt failed..

sayHello.jsp

    HOME 

Hello......

Food For Thoughts:

Have you observed sayHello/goToHome methods of MyController.java? Here, we don’t need any business logic for processing request. In short, we are just returning a static resource to user, nothing else. Then it is overhead to propagate request to Controller, isn’t it?

Spring’s one of the answer to this is ‘UrlFilenameViewController’. This controller allows you to directly map request against resource in XML itself. No need to go to Controller class like MyController.java in above case. I will post ‘UrlFilenameViewController’example in few days. Keep Checking…

Download this working example source code here.

No comments:

Post a Comment