How do I access ModelMap in jsp?

How can I access an object from ModelMap in jsp so that a method can be called on it? I am currently getting 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"; } } 
+4
source share
2 answers

The syntax <% %> %% <% %> deprecated and should no longer be used.

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

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

Obviously, this seems confusing, so you might consider renaming parts of your model for clarity.

Also your annotation

 @SessionAttributes("data") 

does nothing, since you do not have an entry in ModelMap with the data key. This is only useful if you want to save model data during a session, which you don't seem to need here.

+8
source
Designation

${varName} can only be used in jstl, and never in plain Java code. The $ character has no special meaning in Java.

Try something like pageContext.getAttribute("varName") or session.getAttribute("varName") (don't remember exactly how it was done).

+1
source

All Articles