I want to echo back the cache operation selected in the Request, so that it is displayed on the browser. I have very little JSP experience. How can I get the value of operation to appear?
JSP:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<body>
<h1>Cache Operation Results - ${operation}</h1>
<ul>
<c:forEach items="${results}" var="nextObject">
<pre>${nextObject.toString()}</pre>
</c:forEach>
</ul>
</body>
</html>
Controller snippet:
@RequestMapping(value = "/objectcache", method = RequestMethod.GET)
public ModelAndView objectCache(@RequestParam("operation") String operation, HttpServletRequest req) {
List<String> cacheReturnValue = new ArrayList<String>();
ModelAndView mav = new ModelAndView("/utils/objectCache", "results", cacheReturnValue);
if (operation.equalsIgnoreCase("reload")) {
cacheReturnValue = this.reloadCache();
}
mav.addObject("operation", operation);
return new ModelAndView("/utils/objectCache", "results", cacheReturnValue);
}
The you have code should work properly after removing a bug – you’ve created
ModelAndViewtwice and thus ignored “operation” added to firstModelAndViewobject. Solution – createmavonly one time:EDIT:
Small hint: since
ModelAndView#addObjectreturns reference tothisobject, you can chain it’s calls without creating a variablemavlike this: