Expert Spring MVC and Web Flow

(Dana P.) #1

Iterator it = model.entrySet().iterator();
while (it.hasNext()) {
Map.Entry entry = (Map.Entry) it.next();
if (!(entry.getKey() instanceof String)) {
throw new ServletException(
"Invalid key [" + entry.getKey() +
"] in model Map - only Strings allowed as model keys");
}
String modelName = (String) entry.getKey();
Object modelValue = entry.getValue();
if (modelValue != null) {
request.setAttribute(modelName, modelValue);
}
else {
request.removeAttribute(modelName);
}
}
}


■NoteexposeModelAsRequestAttributesis called from the render()method in
InternalResourceView.We’ve removed logging statements for clarity.


Displaying the Model


Listing 8-4 shows, for completeness, a fragment of the JSP file that you might use to display
the model.


Listing 8-4.Displaying the Model in the JSP



Welcome to the Flight Booking Service


<p>We have the following specials now:</p>

<ul>
<%
List specials = (List) request.getAttribute("specials");
for (Iterator i = specials.iterator(); i.hasNext();) {
SpecialDeal special = (SpecialDeal) i.next();
%>
<li>
<%= special.getDepartFrom().getName() %> -

CHAPTER 8 ■SUPPORTED VIEW TYPES 225
Free download pdf