我是 Spring MVC/JSP 世界的新手。抱歉,如果以下问题很明显,
我有一张 map Map<BigInteger, String> reports = new HashMap<>();
需要从 Controller 传递到 JSP。
map 的内容将是这样的
reports.put(new BigInteger("101"), "type1");
reports.put(new BigInteger("102"), "type2");
reports.put(new BigInteger("103"), "type3");
从 Spring MVC Controller 中,我传递此 map ,如下所示:
ModelAndView mav = new ModelAndView("view");
mav.addObject("reports", reports);
但是当我尝试使用以下脚本在 view.jsp 中访问它时:-
<% String a1 = request.getAttribute("reports").get(new BigInteger("101")); %>
但这给了我以下异常(exception):
PWC6199: Generated servlet error:
cannot find symbol
symbol: method get(java.math.BigInteger)
location: class java.lang.Object
如果我尝试使用如下表达式获取值,也会出现相同的异常:-
<%= request.getAttribute("reports").get(new BigInteger("101")) %>
任何帮助将不胜感激。
最佳答案
request.getAttribute("reports")
返回 Object
的引用,而不是 Map
的引用,您必须强制转换它
你最好使用 JSTL 以避免 View 模板中的 java 代码
关于java - 将 map 传递给 JSP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20595909/