Create a Controller and a View

To see the result of calling your new logic, prepare a Spring Controller and a view. In this section, you will write a simple controller that listens for requests for trainingProduct.html and responds with a trainingProduct.jsp view.

Procedure

  1. If it doesn't exist, create a org/training/web/controllers directory in the training/web/src directory.

    This contains the Java packages later on.

  2. Create a new class in the training/web/src/org/training/web/controllers folder that implements the org.springframework.web.servlet.mvc.Controller interface:
    package org.training.web.controllers;
    
    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 TrainingProductController implements Controller
    {
    
    	public ModelAndView handleRequest(final HttpServletRequest request,
    		final HttpServletResponse response) throws Exception
    	{
    		return new ModelAndView("trainingProduct.jsp");
    	}
    
    }
  3. Create a new file named trainingProduct.jsp in the training/web/webroot directory and add the following content:
    <html>
    	<head>
    		<title>Product</title>
    	</head>
    	<body>
    		Product goes here
    	</body>
    </html>
  4. Reference the controller in the springmvc-servlet.xml file:
    <bean
    	name="/trainingProduct.html"
    	class="org.training.web.controllers.TrainingProductController"/>