Spring JSP - 如何显示请求值?

时间:2012-09-04 17:45:12

标签: java spring-mvc

我想回显在Request中选择的缓存操作,以便它显示在浏览器上。我的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>

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

2 个答案:

答案 0 :(得分:0)

JSP页面可以访问多个implicit objects via表达式。根据该文档中给出的示例,您可以获得如下参数值:

${param['operation']} 

答案 1 :(得分:0)

你有 代码在删除错误后正常工作 - 你创建了ModelAndView两次,因此忽略了“operation”添加到第一个{{ 1}}对象。解决方案 - 只创建一次ModelAndView

mav

修改

小提示:由于ModelAndView#addObject返回对@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; } 对象的引用,您可以链接它的调用而不创建像这样的变量this

mav