Spring Framework – Fun with Controllers

This podcast episode is all about digging our heels into this whole “Controller” concept. Today we will be focusing on the parameters that can be passed into a method within a controller. Before I continue with some explanation, here’s what I mean by “a method within a controller”. In keeping with our Login screen theme, I’ll outline a bare LoginController:

package net.javavideotutorials.web;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;

public class LoginController

  @RequestMapping(value="login", method=RequestMethod.GET)
  public String userLoginGet (ModelMap model)
    return "login";

  @RequestMapping(value="login", method=RequestMethod.POST)
  public String userLoginPost (ModelMap model)
    if (loginAttemptValid())
      return "home";
      return "login";

  private boolean loginAttemptValid()
    // let's assume there's some code here that will determine
    //   if this login attempt is valid or not... for now it's
    //   hard-coded to true
    return true;

So the part of this code that we are going to be talking about are the parameters that are passed into the userLoginGet and userLoginPost methods. In the code above we see that we have passed in a ModelMap object and named it “model”. Let’s take a look at the most popular parameters that we can pass into these method signatures.


As far as my programs go, I tend to always have a ModelMap in each of the method’s marked with the @RequestMapping annotation. This is because I always tend to have information that I want to pass back to the view. The ModelMap is used as the object that “encapsulates” all of your data that you will then use on your view (webpage).


This object is used to gain access to the data that has been passed from a form on a webpage. The example I used in this podcast was that the user has filled out a username and password… these variables will be passed along the HttpServletRequest and can be accessed in many different ways. The most old-school way of accessing the username data from the request is to just invoke the following: String username = (String)request.getParameter("username")


Though I don’t use the response object directly often, it’s still worth talking about. The response is what happens after a request has been processed. A request is sent from a webpage to a Java server, a response is what gets sent back from a Java server to a webpage. The response is essentially your “model” in the sense that it will contain the information needed to be displayed on the view. Modern frameworks like Spring, however, allow us to use objects like the ModelMap in order to avoid dealing directly with the response.


This annotation is used as a “shortcut” of sorts. As I mentioned above you can access parameters from the request using an “old-school” method… or… you could use this more modern method. You can retrieve the parameter object from a request and automatically cast it to a variable using the @RequestParam annotation. Here’s an example:

  @RequestMapping(value="login", method=RequestMethod.POST)
  public String userLoginPost (ModelMap model, @RequestParam("username") String username)
    // now the username variable is populated with the value from the request
    System.out.println("The username is: " + username);


Links from the Episode


  • Apache Maven – Used to manage your dependencies and download source code


Free Java Beginners Course

Start learning how to code today with our free beginners course. Learn more.

Thank you for your message. It has been sent.
There was an error trying to send your message. Please try again later.