Spring 3 -- accessing Model values in JSP
Asked Answered
S

1

9

I have a JSP where I am trying to print out values from my Model, but nothing appears in the place where I refer to those values. Here is the controller method where I set the values.

@RequestMapping(value = "/poll1", method = RequestMethod.POST)
public String processPoll1(@RequestParam String vote,
        HttpServletResponse response, Model model) {
    Map<String, Object> resultMap = poll1DAO.tallyVote(vote);
    Cookie poll1 = new Cookie("poll1", "voted");
    model.addAttribute("poll1Yes", resultMap.get("yes").toString());
    model.addAttribute("poll1No", resultMap.get("no").toString());
    poll1.setMaxAge(maxSeconds);
    response.addCookie(poll1);
    return "redirect:/polls";
}

Here is the part of the JSP where I refer to the Model attributes.

<table>
    <tr>
        <td><b><i>Poll #1 -- </i></b>Would you like to have a 30-year reunion in 2016?<br></td>
    </tr>
    <tr>
        <td><b>Yes</b></td>
        <td>&nbsp;&ndash;&nbsp;<c:out value='${model.poll1Yes}' /><br /></td>
    </tr>
    <tr>
        <td><b>No</b></td>
        <td>&nbsp;&ndash;&nbsp;<c:out value='${model.poll1No}' /><br />
        </td>
    </tr>
</table>

Here is my output. Instead of actual values, nothing is printed out in the attributes' places.

Poll #1 -- Would you like to have a 30-year reunion in 2016?

Yes  – 

No  –  
Snowbird answered 25/5, 2013 at 22:28 Comment(0)
S
7

No need to reference model in your JSP.

<c:out value='${poll1Yes}' />
<c:out value='${poll1No}' />
Sandarac answered 26/5, 2013 at 2:19 Comment(1)
What if we want to access a dynamic object from the model, is there a way to do something like model["poll" + num + "yes"] ?Haemin

© 2022 - 2024 — McMap. All rights reserved.