ParameterizableViewController

ParameterizableViewController
  • Spring framework has provided this controller class in the form of org.springframework.web.servlet.mvc.ParameterizableViewController
  • The main intention of this controller class is to display web pages without processing request.
  • In ParameterizableViewController classes, it is not required to define any user defined controller class, rather, it must be configured in Spring configuration file directly and it will take “viewName” property with the logical name of the view name in order to open JSP page.
  • In general, we will use the mechanism when we want to get some web pages just by clicking on hyperlinks.

E.g

If we click on “ContactUs” and “AboutUs” links in webpages then we are able to get ContactUs web page and AboutUs web page.

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="home"/> 
</body> 
</html> 

home.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>ParameterizableViewController</title> 
</head> 
<body> 
    <h1 align="center"> 
        <a href="contactUs">ContactUs</a>  
        <a href="aboutUs">AboutUs</a> 
    </h1> 
</body> 
</html> 

aboutUs.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> 
    <h2 style="color: red" align="center">Waytoeasylearn gives easy tutorials.</h2> 
    <br> 
    <br> 
    <br> 
    <h3> 
        <a href="home">Home Page</a> 
    </h3> 
</body> 
</html> 

contactUs.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> 
    <h2 style="color: red" align="center"> 
        Waytoeasylearn<br> 
        Website : www.waytoeasylearn.com<br> 
        Mobile : +91 9000027370<br> 
    </h2> 
    <br> 
    <br>  
    <h3> 
        <a href="home">Home Page</a> 
    </h3> 
</body> 
</html> 

HomeController.java

package com.ashok.spring.mvc.controller; 

import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
import org.springframework.web.servlet.ModelAndView; 
import org.springframework.web.servlet.mvc.AbstractController; 

public class HomeController extends AbstractController { 
   @Override 
   protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) 
         throws Exception { 
      return new ModelAndView("home"); 
   } 
} 

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="/home" class="com.ashok.spring.mvc.controller.HomeController" /> 
    <bean name="/contactUs" class="org.springframework.web.servlet.mvc.ParameterizableViewController"> 
        <property name="viewName" value="contactUs" /> 
    </bean> 
    <bean name="/aboutUs" class="org.springframework.web.servlet.mvc.ParameterizableViewController"> 
        <property name="viewName" value="aboutUs" /> 
    </bean> 
    <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> 
ParameterizableViewController
Scroll to top