0

我想回显在请求中选择的缓存操作,以便它显示在浏览器上。我很少有 JSP 经验。我怎样才能让操作的价值出现?

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>

控制器片段:

@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);
}
4

2 回答 2

0

JSP 页面可以通过表达式访问多个隐式对象。根据该文档中给出的示例,您可以像这样获取参数值:

${param['operation']} 
于 2012-09-04T18:16:15.370 回答
0

删除错误后,您的代码应该可以正常工作-您已经创建了两次,因此忽略了添加到第一个对象的“操作”。解决方案 -只创建一次:ModelAndViewModelAndViewmav

@RequestMapping(value = "/objectcache", method = RequestMethod.GET)
public ModelAndView objectCache(@RequestParam("operation") String operation,
        HttpServletRequest req) {

    List<String> cacheReturnValue = new ArrayList<String>();

    if (operation.equalsIgnoreCase("reload")) {
        cacheReturnValue = this.reloadCache();
    }

    ModelAndView mav = new ModelAndView("/utils/objectCache");
    mav.addObject("results", cacheReturnValue);
    mav.addObject("operation", operation);
    return mav;
}

编辑:

小提示:由于ModelAndView#addObject返回对this对象的引用,因此您可以链接它的调用而无需创建mav如下变量:

return new ModelAndView("/utils/objectCache")
        .addObject("results", cacheReturnValue)
        .addObject("operation", operation);
于 2012-09-04T18:16:54.727 回答