jQuery treeview在JSP中的应用:ASYNC

1. async.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

    <head>

       <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

       <title>Insert title here</title>

       <link rel="stylesheet" href="css/jquery.treeview.css" />

       <link rel="stylesheet" href="css/screen.css" />

 

       <script src="javascript/jquery.js" type="text/javascript"></script>

       <script src="javascript/jquery.cookie.js" type="text/javascript"></script>

       <script src="javascript/jquery.treeview.js" type="text/javascript"></script>

       <script src="javascript/jquery.treeview.async.js"

           type="text/javascript"></script>

 

       <script type="text/javascript">

           $(document).ready(function(){

              $("#black").treeview({

                  url: "source.html",

                  control: "#treecontrol",

                  persist: "location"

              });

           });

       </script>

    </head>

    <body>

       <h1 id="banner">

           <a

              href="http://bassistance.de/jquery-plugins/jquery-plugin-treeview/">jQuery

              Treeview Plugin</a> Demo

       </h1>

       <div id="main">

           <a href=".">Main</a>

           <h4>

              Sample - async

           </h4>

           <div id="treecontrol">

              <a title="Collapse the entire tree below" href="#"><img

                     src="images/minus.gif" /> Collapse All</a>

              <a title="Expand the entire tree below" href="#"><img

                     src="images/plus.gif" /> Expand All</a>

              <a

                  title="Toggle the tree below, opening closed branches, closing open branches"

                  href="#">Toggle All</a>

           </div>

           <ul id="black">

           </ul>

       </div>

    </body>

</html>

 

2. SourceController

@Controller

public class SourceController {

 

    @Autowired

    SourceService sourceService;

 

    @RequestMapping("/source.html")

    public void source(HttpServletRequest request, HttpServletResponse response)

           throws IOException {

       request.setCharacterEncoding("utf-8");

       response.setContentType("text/json;charset=UTF-8");

 

       PrintWriter out = response.getWriter();

       String root = request.getParameter("root");

       String output = sourceService.generateNodeString(root);

       out.print(output);

       out.flush();

       out.close();

    }

}

 

3. SourceService

@Service("sourceService")

public class SourceService {

 

    @Autowired


    TestDao testDao;


    

    public String generateNodeString(String root) {

       if (root == null) {

           return "";

       } else if (root.equalsIgnoreCase("source")) {

           return generateTopTreeString();

       } else {

           return generateSubTreeString(Long.parseLong(root));

       }

    }

 

    private String generateTopTreeString() {

       StringBuilder sb = new StringBuilder();

       sb.append("[");

 

       List<Item> list = testDao.findTopItem();

 

       int i = 0;

       for (Iterator<Item> it = list.iterator(); it.hasNext(); i++) {

           if (i > 0) {

              sb.append(",");

           }

           Item item = (Item) it.next();

           sb.append(generateNodeString(item));

       }

 

       sb.append("]");

       return sb.toString();

    }

 

    private String generateSubTreeString(Long itemId) {

       StringBuilder sb = new StringBuilder();

       sb.append("[");

 

       List<Item> list = testDao.findSubItem(itemId);

 

       int i = 0;

       for (Iterator<Item> it = list.iterator(); it.hasNext(); i++) {

           if (i > 0) {

              sb.append(",");

           }

           Item item = (Item) it.next();

           sb.append(generateNodeString(item));

       }

 

       sb.append("]");

       return sb.toString();

    }

 

    private String generateNodeString(Item item) {

       StringBuilder sb = new StringBuilder();

       sb.append(" {");

       sb.append("  \"text\": \"" + generateLinkString(item) + "\"");

 

       if (testDao.hasSubItem(item.getItemId())) {

           sb.append(",  \"id\":\"" + item.getItemId() + "\"");

           sb.append(",  \"hasChildren\":true");

       }

 

       sb.append(" }");

       return sb.toString();

    }

 

    private String generateLinkString(Item item) {

       return "<a href='menu.html?menuId=" + item.getItemId()

              + "' target='main' >" + item.getItem() + "</a>";

    }

}

 

4. Item

 

public class Item implements java.io.Serializable {

……

private Long itemId;

    private String item;

    private Long itemLevel;

    private Set<Item> itemsForParentId = new HashSet<Item>(0);

    private Set<Item> itemsForItemId = new HashSet<Item>(0);

……

}

 

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/lz_net/archive/2009/08/17/4457097.aspx

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