Passing data from mvc controller to JSP


Note that the following maven dependency has been included in the pom, which will make the exercise in this page possible.
Getting back to the request handler "anotherHandleRequest( ... )" in the DurianGroveMVController, notice the two high-lighted lines in the code.
@RequestMapping(value = "/h2g2j", method = {RequestMethod.GET,RequestMethod.POST})
public String anotherHandleRequest(ModelMap model,
    @RequestParam("who") String who,
    @RequestParam("street") String street,
    @RequestParam("zip") int zip)
throws Exception {
    Address addr = zipMap.get(zip);
    String city = addr != null ? : "Walla Walla";
    String state = addr != null ? : "ZZ";
    String where = String.format("%s, %s, %s %05d", street, city, state, zip);
    model.addAttribute("who", who);
    model.addAttribute("where", where);
    model.addAttribute("what", season);"anotherHandleRequest: {},{},{} ", who, where, season);
    return "Cello";
The objects mapped into the ModelMap object can be retrieved by the JSP as ${} variables.
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Cello Tremello</title>
<h2>Cello Tremello</h2>
${who} lives in ${where} in ${what} .

Starting with the URL
which is routed by
to get its contents from
which contains a form that invokes
which is routed by
to gets its contents from
which gets its model objects from
which results in the web page displaying

Cello Tremello

Annie Walker lives in 123 Main St, Hampden, MA 01001 in Spring .

No comments:

Post a Comment