Wednesday, June 22, 2016

Spring MVC Multiple Controller Example

Spring MVC Multiple Controller Example


a) Libraries to be used:

spring-core - 4.1.6
spring-web - 4.1.6
spring-webmvc - 4.1.6
javax.servlet-api - 3.1.0
jstl - 1.2


b) Create a xml file named as web.xml in WEB-INF folder and write the following code in it:

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>

</web-app>


c) Create a xml file named as spring-servlet.xml in WEB-INF folder and write following code in it:


<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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.gag.controller" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>  


d) Create an index.jsp and do following code in it:

<html>
<body>
<h2>Hello World!</h2>
<a href="hello.html">Hello Controller</a>
<a href="welcome.html">Welcome Controller</a>
</body>
</html>


e) Create a folder named jsp in WEB-INF folder and Create following JSP pages in this folder:
data.jsp 

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>Hi you have arrived on Hello COntroller.....</h1>

</body>
</html>


and gagandeep.jsp


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>Hii..you have arrived on Welcome Controller....</h1>

</body>
</html>


e) Create a package named as com.gag.controller and within this package Create following classes in it:

HelloController.java


package com.gag.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class HelloController {
@RequestMapping("/hello")
public ModelAndView helloWorld() {
String message = "HELLO CONTROLLER";
System.out.println("Heloooo..........");
return new ModelAndView("data", "message", message);
}
}


WelcomeController.java


package com.gag.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class WelcomeController {
@RequestMapping("/welcome")
public ModelAndView helloWorld() {
String message = "WELCOME CONTROLLER";
System.out.println("Welcome.........");
return new ModelAndView("gagandeep", "message", message);
}
}