Spring MVC — Controller Class Name Handler Mapping | Code Factory

Spring Tutorial Index Page: Link

Create Java Project

<?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-HelloWorld-ControllerClassName</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> <!-- controller class name handler mapping -->
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" />
<!-- controller -->
<bean id="hc" class="com.codeFactory.controller.HelloController" />
<!-- 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.Controller;
public class HelloController implements Controller { @Override
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
String name = request.getParameter("name");
Map map = new HashMap();
map.put("type", "ControllerClassNameHandlerMapping");
map.put("message", "Hello " + name);

ModelAndView modelAndView = new ModelAndView("success", map);
return modelAndView;
}
}
<h2>Hello World</h2>
<form action="./hello.code">
Name: <input type="text" name="name">
<input type="submit" value="Submit">
</form>
<p>Handler Mapping Type : ${type}</p>
${message}

--

--

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