问题描述
在页面的doGet上,我设置了一些默认属性.
On my doGet of the page I setup some default attributes.
private static final CategoryFactory cf = CategoryFactory.getInstance();
public static void setupHeader(HttpServletRequest req) {
ArrayList<String> catagories = cf.getPrimaryCategories();
Map<String, ArrayList<String>> categoryMap = cf.getCategoryMap();
User user = UserUtils.getUserSession(req);
req.setAttribute("catagories", catagories);
req.setAttribute("categoryMap", categoryMap);
req.setAttribute("isAdmin", UserUtils.isAdmin(user));
if (user != null) {
req.setAttribute("orderCount", user.getOrderCount(false));
req.setAttribute("unreadMessageCount", user.getUnreadMessageCount());
req.setAttribute("cartTotal", user.getShoppingCart().getTotal());
}
}
然后在我的JSP页面上,我试图根据类别(即Map的键)使用categoryMap.
Then on my JSP page I'm trying to work with the categoryMap based on the catagories which are the keys from Map.
<c:forEach var="cata" items="${catagories}">
<li class="dropdown-submenu"><a tabindex="-1" href="#"><c:out value="${cata}" /></a>
<ul class="dropdown-menu" role="menu">
<c:forEach var="secCategories" items="${categoryMap['cata']}">
<c:forEach var="second" items="${secCategories}">
<li role="presentation"><a role="menuitem"
href="/browse?type="${fn:replace(second, ' ','+')}"><c:out
value="${second}" /></a></li>
</c:forEach>
</c:forEach>
</ul></li>
</c:forEach>
我得到的错误是
其中"[航空和起重设备""是变量类别中的第一个关键字,并设置为新变量$ {cata}.由于此操作不起作用,因此我缺少有关如何正确传递动态密钥的一些信息.
Where "[Aerial & Lifting Equipment" is the first key from the variable categories and set as the new variable ${cata}. As this is not working, I'm missing something on how to pass the dynamic key properly.
解决方案
这解决了我的问题,我设置了之前忘记的属性类型,为了便于使用,我将ArrayList更改为String [].
SOLUTION
This fixed my issue, I set the attribute types which I forgot before, and for ease of use I changed the ArrayList to a String[].
<%@attribute name="user" required="true" type="com.entity.User"%>
<%@attribute name="catagories" required="true" type="java.lang.String[]"%>
<%@attribute name="categoryMap" required="true" type="java.util.Map"%>
<c:forEach var="cata" items="${catagories}">
<li class="dropdown-submenu"><a tabindex="-1" href="#"><c:out value="${cata}" /></a>
<ul class="dropdown-menu" role="menu">
<c:forEach var="secCategories" items="${categoryMap[cata]}">
<c:forEach var="second" items="${secCategories}">
<li role="presentation"><a role="menuitem"
href="/browse?type="${fn:replace(second, ' ','+')}"><c:out
value="${second}" /></a></li>
</c:forEach>
</c:forEach>
</ul></li>
</c:forEach>
推荐答案
你会生自己的气,但是....
You're gonna be mad at yourself, but ....
<c:forEach var="secCategories" items="${categoryMap['cata']}">
应该是
<c:forEach var="secCategories" items="${categoryMap[cata]}">
您不希望文字字符串"cata"成为键,而您希望cata page属性的值成为键. :)
You don't want the literal string "cata" being the key, you want the value of the cata page property being the key. :)
这篇关于JSTL Hashmap和动态密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!