Frage

I am new to Spring and having a basic question related to the Spring 3 MVC controller class.

I have this below method in my controller:

@RequestMapping("/index")
public String listContacts(Map<String, Object> map) {
    map.put("contacts", new EmployeeForm());
    map.put("contactList", employeeService.listEmployee());
    return "contacts";
}

In the JSP file, I an accessing the contactList using this below piece of code:

<c:forEach items="${contactList}" var="contact">
<tr>
    <td>${contact.lastname}, ${contact.firstname} </td>
    <td>${contact.email}</td>
    <td>${contact.telephone}</td>
    <td><a href="delete/${contact.id}">delete</a></td>
</tr>
</c:forEach>

My confusion:

I have not added that contactList in a session or request attribute in my controller class, then how am I able to access the same in JSP file using ${contactList} ?

Please clarify.

War es hilfreich?

Lösung

The Map argument you have in your controller handler method is effectively used as a map of model attributes.

Model attributes are (typically) added as HttpServletRequest attributes before the view is rendered.

This is explained in the documentation here.

The following are the supported method arguments:

  • java.util.Map / org.springframework.ui.Model / org.springframework.ui.ModelMap for enriching the implicit model that is exposed to the web view.

Andere Tipps

Spring is providing you a Map, or basically a view model, which you can populate in your controller handler method. After your handler completes, Spring adds anything you added to the Map to the jsp context so that they can be accessed as normal context variables. I believe the variables are added to the 'request' scope for access by the JSP code, but I am not certain, as it might be 'page' scope.

Because the handler and view rendering are being handled (managed by Spring) in the same request, there is no need to store any of the model information in the session.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top