Spring MVC — Parameterizable View Controller | Code Factory

Spring Tutorial Index Page: Link

  • ParameterizableViewController use setter DI so from xml file we must have to pass viewName.

Create Java Web Project

  • Open Eclipse
  • Go to File -> New -> Dynamic Web Project
  • Create MVC-Controller2 project
  • Right click on project -> Build Path -> Configure Build Path -> Libraries tab -> Add External JARs (Used 2.X jars)
    - commons-logging-X.X.jar
    - spring-beans-X.X.X.jar
    - spring-context-X.X.X.jar
    - spring-core-X.X.X.jar
    - spring-web-X.X.X.jar
    - spring-webmvc-X.X.X.jar
  • Also, add these jars in WEB-INF/lib folder for runtime.
  • * You can find dtd information from spring-beans-X.X.X.jar -> org -> springframework -> beans -> factory -> xml -> spring-beans.dtd (line no 36 & 37)
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">

<display-name>MVC-Controller2</display-name>

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<!-- default -->
<servlet>
<servlet-name>helloWorld</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>helloWorld</servlet-name>
<url-pattern>*.code</url-pattern>
</servlet-mapping>
</web-app>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans> <!-- default handler mapping -->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />
<!-- controller -->
<bean name="/hello.code" class="com.codeFactory.controller.HelloController">
<property name="viewName" value="success" />
</bean>
<!-- view resolver -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
package com.codeFactory.controller;import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.ParameterizableViewController;
/**
* @author code.factory
*
*/
public class HelloController extends ParameterizableViewController {
/* ParameterizableViewController use setter DI so from xml file we must have to pass viewName */

@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
String name = request.getParameter("name");
Map map = new HashMap();
map.put("type", "ParameterizableViewController");
map.put("message", "Hello " + name);

/* instead of passing static name "success", we use getViewName()
* we pass viewName from xml file */
ModelAndView modelAndView = new ModelAndView(getViewName(), map);
return modelAndView;
}
}
<h2>Hello World</h2>
<form action="./hello.code">
Name: <input type="text" name="name">
<input type="submit" value="Submit">
</form>
<p>Controller Type: ${type}</p>
<p>${message}</p>

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Top Reasons Why Java Development is still Relevant in 2022

Why Java is Relevant in 2022

Learn DynamoDB in Under 5 Minutes

Last step before IOI : Thailand selection camp

Getting started with Tegra K1 in 2021

Facelock Authentication for Linux

Small Demo of Facelock

Throwback to Deezer ConTEST: the first hackathon for Product and QA

What AWS CloudFormation Registry and CLI means for DevOps in the Cloud

[Python一行解] To Be Pythonic — One line Solutions

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Code Factory

Code Factory

More from Medium

Functional Interfaces in Java

Dynamic Linkage to a z/OS Native Library in Java

Let’s talk about testing

Comparison Inheritance And Polymorphism in JAVA