【问题标题】:Spring 3 -- accessing Model values in JSPSpring 3 - 在 JSP 中访问模型值
【发布时间】:2013-05-21 04:56:45
【问题描述】:

我有一个 JSP,我试图从我的模型中打印出值,但在我引用这些值的地方没有出现任何内容。这是我设置值的控制器方法。

@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";
}

这是我引用模型属性的 JSP 部分。

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

这是我的输出。在属性的位置没有打印出任何内容,而不是实际值。

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

Yes  – 

No  –  

【问题讨论】:

    标签: jsp jstl spring-3 modelattribute


    【解决方案1】:

    无需在您的 JSP 中引用 model

    <c:out value='${poll1Yes}' />
    <c:out value='${poll1No}' />
    

    【讨论】:

    • 这就是我所需要的。谢谢。
    • 如果我们想从模型中访问一个动态对象怎么办,有没有办法像model["poll" + num + "yes"]那样做?
    猜你喜欢
    • 1970-01-01
    • 2011-11-21
    • 1970-01-01
    • 1970-01-01
    • 2023-01-08
    • 2016-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多