Sunday, July 1, 2012

Spring : MultiActionController Example

Lets create a webproject in My Eclipse (lets say MultiActionController). Now we will add spring Capabilities to it. Here we are using Spring 3.0


(Right click on project) -> My Eclipse -> Add Spring Capabilities..


Adding below libraries (as shown in below image)

  • Spring 3.0 Core Libraries
  • Spring 3.0 Web Libraries



Now lets modify web.xml. Adding dispatcher servlet (lets say javaxp)


web.xml



<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<description>Spring MVC Dispatcher Servlet</description>
<servlet-name>javaxp</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>


<servlet-mapping>
<servlet-name>javaxp</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>




javaxp-servlet.xml 



<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

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

<bean name="/load.do" class="com.javaxp.controller.ApplicationController">
<property name="methodNameResolver" ref="paramResolver"/>
</bean>

<bean id="paramResolver" class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
<property name="paramName" value="action" />
<property name="defaultMethodName" value="loadHomePage" />
</bean>

</beans>


ApplicationController.java

/* ApplicationController.java */


package com.javaxp.controller;


import java.util.ArrayList;
import java.util.Hashtable;


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


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


public class ApplicationController extends MultiActionController {


public ModelAndView loadHomePage(HttpServletRequest req,
HttpServletResponse resp) {


System.out.println("Inside ApplicationController.java > loadHomePage() >>");


ModelAndView mvObject = null;


//Lets see how we can pass various object using ModelMap
String message = "This is a String message passed via ModelMap";


ArrayList<String> city = new ArrayList<String>();
city.add("Delhi");
city.add("Mumbai");
city.add("Pune");


Hashtable<String,String> employee = new Hashtable<String,String>();
employee.put("001", "Ram");
employee.put("002", "Shyam");
employee.put("003", "Geeta");


//adding all objects in a ModelMap
ModelMap dataMap = new ModelMap();
dataMap.put("message", message);
dataMap.put("city", city);
dataMap.put("employee", employee);


mvObject = new ModelAndView("home","data",dataMap);


return mvObject;
}
}



home.jsp
  
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<h2>This is home page</h2>

${data.message}
<hr/>
ArrayList of city
<br/>
<c:forEach items="${data.city}" var="city">
    ${city} <br/>
</c:forEach>
<hr/>
Hashtable of employee
<br/>
<c:forEach items="${data.employee}" var="employee">
    ${employee.key} ${employee.value} <br/>
</c:forEach>


Lets see the files structure created so far



Output

Now got to  http://localhost:8080/MultiActionController/load.do  and you should see below screen. Note /load.do which is your controller mapping in dispatcher-servlet.








No comments:

Post a Comment