Spring MVC Tutorial With Example Using Eclipse And Tomcat [download simple spring web mvc application]

HelloWorldController :

DispatcherServlet calls this controller for the request generated at index.jsp (as the URI specified in index.jsp is mapped with this controller defined in dispatcher-servlet.xml with name /requestURI.htm) It has a method handleRequestInternal where it is performing the business rules by calling the processName method of processor. It returns an object of ModelAndView.
return new ModelAndView(“result”,”processedNameKey”, processedNameValue); This controller is returning a view name as ‘result’ (first argument) and this name is concatenated with prefix and suffix to get required view by DispatcherServlet. Second and third arguments are for sending data to view. Second argument(model parameter name)is used in view (result) to access the value of third argument(model parameter). So second and third arguments are used as name value pair to send data to view.

package com.thecafetechno;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
import process.Processor;

public class HelloWorldController extends AbstractController {
	private Processor processor;
    protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
    	String processedNameValue=processor.processName(request.getParameter("textName"));
    	return new ModelAndView("result","processedNameKey", processedNameValue);
	public void setProcessor(Processor processor) {
			this.processor = processor;

Processor.java :

It is calculating the number of characters in the name and returning a formatted string (Business rule).

package process;

public class Processor {
	public String processName(String str){
		return "Mr. "+str+" your name has "+str.length()+" characters";

result.jsp :

Here we are displaying the model parameter value (Returned by controller) by using model parameter name as ${ processedNameKey}.

<%@ page language="java" contentType="text/html; charset=UTF-8"
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<h1>Hi ${processedNameKey}</h1>

To run this example download code , import it into eclipse and configure build path by adding jars to classpath.