4

So, I've got the following method in the Controller:

@RequestMapping(value="/modules.htm",method = RequestMethod.GET )
protected String modules(ModelMap model) throws Exception {

      List<Module> modules = moduleService.getStudentModules(course_id);
      model.addAttribute....?
      return "modules";
}

And I need to display this list in jsp View, like:

<h1>Modules: </h1>
module 1 
module 2... etc

What do I need to add to show the full list on the JSP page? How should I upload it to the model and retrieve it? Thank you in advance.

exomen
  • 355
  • 2
  • 6
  • 20
  • 2
    `model.addAttribute("modules", modules);` will add the list to the model. Use [`jstl`](http://stackoverflow.com/questions/2117557/how-to-iterate-an-arraylist-inside-a-hashmap-using-jstl) to loop over the list. – Sotirios Delimanolis May 13 '13 at 13:41

1 Answers1

7

Pass the whole list:

model.addAttribute("modules", modules);

Then iterate on it:

<c:forEach items="${modules}" var="module">
    ${module.anyProperty}
</c:forEach>
sp00m
  • 47,968
  • 31
  • 142
  • 252
  • ".anyProperty" - would that refer to the method declared in the object? i.e. ".getModuleName()" (I'm not very familiar with views code :P ) – exomen May 13 '13 at 13:49
  • 3
    @exomen It refers to any getter of the class, but *without the `get` part*. For example, to call `.getModuleName()`, you'll have to write `.moduleName`. This is called *Expression Language*, have a look at the [doc](http://docs.oracle.com/javaee/6/tutorial/doc/gjddd.html) for further info. – sp00m May 13 '13 at 13:52
  • what if in class it is private? – exomen May 13 '13 at 14:07
  • @exomen I never tried actually, but I guess it won't work since a getter can't be private by definition. Moreover, it has no functional meaning, a private method shouldn't be called from somewhere else than in the class itself. – sp00m May 13 '13 at 14:19
  • nope, I mean getter is public, but the field is private and when you call smth like ".code" in theory it must return field, not the result of get() method.. isn't it? :P – exomen May 13 '13 at 14:30
  • 2
    @exomen No, that's what I told you (you should really read the doc `:)`). Let's say your class `Module` has a private field `name` and a public getter `getName()`. In your JSP files, when calling `.name`, the getter will be called, and not the field itself. With `.name`, the compiler will look for a method called `getName()` (or `isName()`, essentially used for booleans). Even if you have a private field `name` but no corresponding getter, an exception will be thrown. – sp00m May 13 '13 at 14:33