Spring 3 -- accessing Model values in JSP

23,672

No need to reference model in your JSP.

<c:out value='${poll1Yes}' />
<c:out value='${poll1No}' />
Share:
23,672

Related videos on Youtube

Admin
Author by

Admin

Updated on July 05, 2022

Comments

  • Admin
    Admin almost 2 years

    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  –  
    

Related