≡ Menu

Podcast Episode 34 – Fun with Controllers

Java-Podcast

Before we get started…

I need your help.

It’s been about two months since my last podcast was released and I’ve decided that since this has become a recurring thing, it’s time I did something about it.

To help me keep these podcasts and blog posts FREE, I need you to take 10 seconds to fill out an easy survey they will help me figure out who my listeners are! Please click the link below:

Keep these podcasts FREE – Fill out this Survey

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;

@Controller
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";
    else
      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.

ModelMap

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).

HttpServletRequest

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")

HttpServletResponse

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.

@RequestParam

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

  • Podcast Survey – Help me to keep these episodes free by completing this quick survey
  • Apache Maven – Used to manage your dependencies and download source code
{ 0 comments… add one }

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.