如何在jsp中访问ModelMap? [英] How do I access ModelMap in a jsp?

查看:193
本文介绍了如何在jsp中访问ModelMap?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在jsp中从ModelMap访问对象,以便可以在其上调用方法?目前,我收到此错误:

How can an object be accessed from the ModelMap in jsp so that a method can be called on it? Currently I recieve this error:

Syntax error on token "$", delete this token

JSP

<body>
        <% MenuWriter m = ${data.menus} %>
        <%= m.getMenus()%>  
</body>

Java

@Controller
@RequestMapping("/dashboard.htm")
@SessionAttributes("data")
public class DashBoardController {

    @RequestMapping(method = RequestMethod.GET)
    public String getPage(ModelMap model) {
        String[] menus = { "user", "auth", "menu items", };
        String[] files = { "menu", "item", "files", };
        MenuWriter m = new MenuWriter(menus, files);
        model.addAttribute("menus", m);

        String[] atocs = { "array", "of", "String" };
        model.addAttribute("user_atocs", atocs);

        return "dashboard"; 
    }
}

推荐答案

<% %>语法已被弃用,不应再使用.

The <% %> syntax is deprecated, and shouldn't be used any more.

在现代JSP中,您的JSP片段的等效项是:

The equivalent in modern JSP of your JSP fragment would be:

<body>
   ${menus.menus}
</body>

显然,这看起来令人困惑,因此您可能需要考虑重命名模型的各个部分以使其更加清晰.

Obviously, that looks confusing, so you may want to consider renaming parts of your model for clarity.

另外,您的注释

@SessionAttributes("data")

在这里什么也不做,因为您没有在ModelMap中使用键data进行输入.仅当您要在整个会话中保留模型数据时才有用,这似乎并不需要.

does nothing here, since you have no entry in the ModelMap with the key data. This is only useful if you want to keep the model data across the session, which it doesn't seem you need to here.

这篇关于如何在jsp中访问ModelMap?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆