Controller Classes

Controller Classes

The main intention of Controller classes is to manage business logic or to include code in order access the business logic existed with Business components. To prepare Controller classes, Spring framework has provided a set of predefined classes in “org.springframework.web.servlet.mvc” package an which are implemented by Controller interface.

Capture 43
AbstractController

This Controller Class will be used to prepare Controller classes where no form data submission in request, but it will display dynamic content through the web pages. AbstractController class is able to have the following method to have Business logic or the code to access business components.

public ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception

In general, we will use this Controller class in the following situations.

  • When we click on “view profile” link in Facebook account, generating profile data
  • When we click on “Get all Jobs” link JOB Portals

Note

In all the cases, no form submission is going on, but, dynamic content will be included in response.

E.g

index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
    pageEncoding="ISO-8859-1"%> 
<!DOCTYPE html> 
<html> 
<head> 
<meta charset="ISO-8859-1"> 
<title>Insert title here</title> 
</head> 
<body> 
    <jsp:forward page="wish"/> 
</body> 
</html> 

wish.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
    pageEncoding="ISO-8859-1"%> 
<%@page isELIgnored="false"%> 
<!DOCTYPE html> 
<html> 
   <head> 
      <meta charset="ISO-8859-1"> 
      <title>Spring MVC</title> 
   </head> 
   <body> 
      <h1>Hello..!!, ${message}.</h1> 
   </body> 
</html> 

WishController.java

package com.ashok.spring.mvc.controller; 

import java.time.LocalTime; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
import org.springframework.web.servlet.ModelAndView; 
import org.springframework.web.servlet.mvc.AbstractController; 
/**
 * 
 * @author ashok.mariyala
 *
 */
public class WishController extends AbstractController { 
   @Override 
   protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) 
         throws Exception { 
      LocalTime time = LocalTime.now(); 
      int hour = time.getHour(); 
      String wishMessage = ""; 
      if (hour < 12) { 
         wishMessage = "Good Morning"; 
      } else if (hour < 17) { 
         wishMessage = "Good After noon"; 
      } else { 
         wishMessage = "Good Evening"; 
      } 
      return new ModelAndView("wish", "message", wishMessage); 
   } 
}

ds-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" 
   xmlns:context="http://www.springframework.org/schema/context" 
   xsi:schemaLocation=" 
      http://www.springframework.org/schema/beans 
      http://www.springframework.org/schema/beans/spring-beans.xsd 
      http://www.springframework.org/schema/context 
      http://www.springframework.org/schema/context/spring-context.xsd"> 
   <bean name="/wish" class="com.ashok.spring.mvc.controller.WishController"/> 
   <bean name="handlerMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/> 
   <bean name="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
      <property name="prefix" value="/WEB-INF/jsp/"/> 
      <property name="suffix" value=".jsp"/> 
   </bean> 
</beans> 

web.xml

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
   xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
   xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" 
   id="WebApp_ID" version="4.0"> 
   <display-name>MVC</display-name> 
   <welcome-file-list> 
      <welcome-file>index.html</welcome-file> 
      <welcome-file>index.htm</welcome-file> 
      <welcome-file>index.jsp</welcome-file> 
      <welcome-file>default.html</welcome-file> 
      <welcome-file>default.htm</welcome-file> 
      <welcome-file>default.jsp</welcome-file> 
   </welcome-file-list> 

   <servlet> 
      <servlet-name>ds</servlet-name> 
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
      <load-on-startup>1</load-on-startup> 
   </servlet> 
   <servlet-mapping> 
      <servlet-name>ds</servlet-name> 
      <url-pattern>/</url-pattern> 
   </servlet-mapping> 
</web-app>
Screenshot from 2020 01 14 12 36 50

There are three types of AbstractControler classes.

  1. ParameterizableViewController
  2. MultiActionController
  3. BaseCommandController

Controller Classes
Scroll to top