Struts2 Map嵌套List实现

最近一直在研究struts2,看了sturts2和博客的相关文章,学习了Struts2 Map嵌套List实现,下面是是示例演示:

要求:一个业务线对应一个目录树


Struts2 Map嵌套List实现

过程:

1.页面前一个Action中实现ServletRequestAware接口

2.Action中将serviceTypesCategories 放入request

private Map<String, List<Category>> serviceTypesCategories = new HashMap<String, List<Category>>();

request.setAttribute("serviceTypesCategories", serviceTypesCategories);

3.JSP页面代码

<s:iterator value="%{serviceTypesCategories.keySet()}" id="serviceTypeString" >
<s:set name="serviceTypeRequest" scope="request" value="#serviceTypeString"/>
       <s:text name="preferential_servicestype"/>:<s:property value="serviceTypeString"/><
br>
     <%
       Map<String, List<Category>> serviceTypesCategories = (Map<String, List<Category>>) request
           .getAttribute("serviceTypesCategories");
       String servicetype = (String) request.getAttribute("serviceTypeRequest");
       PreferentialCheckTreeUtil util = new PreferentialCheckTreeUtil();
       List<Category> myCategories = serviceTypesCategories.get(servicetype);
       List<Category> rootCategories = new ArrayList<Category>();

       out.println(" <script language=\"JavaScript\">");
       out.println("var check" + treeIndex + " = new CheckTree('check" + treeIndex + "');");
       out.println("</script>");

       out.println(" <ul id=\"tree-check" + treeIndex + "\" class=\"checktree\">");

       for (Category category : myCategories) {
         if (category.getLevel() == 1) {
           rootCategories.add(category);
         }
       }
       final PrintWriter mcpsOut = response.getWriter();
       for (Category rootCategory : rootCategories) {
         // out.println(rootCategory.getName());
         //判定是否是当前层的最后一个节点
         if (rootCategory.getId() == rootCategories.get(rootCategories.size() - 1).getId()) {
           util.printCategoryWithCheck(rootCategory, out, true);
         }
         else {
           util.printCategoryWithCheck(rootCategory, out, false);
         }
       }
       out.println("</ul>");
       treeIndex++;
     %>
</s:iterator>

注意:这里使用了<S:SET/>标签,并且设置scoperequest,如果不放入request中,会出现页面偶尔显示不出来的情况,

原因是: <s:iterator value="%{serviceTypesCategories.keySet()}" id="serviceTypeString" >

默将serviceTypeString放入stack context中,并非reqeust.

 

你可能感兴趣的:(JavaScript,jsp)