Build a simple ligerui environment using java

Source: Internet
Author: User
Tags tojson

Build a simple ligerui environment using java

Recently, I learned about the ligerui framework because of my work needs. However, in the company, we are only a code farmer at the lowest layer, so the environment has been set up. We usually get the project directly, then, continue to ctrl + c, ctrl + v on other people's frameworks, and then repair and supplement. To get rid of this dilemma, we decided to build a ligerui using a simple servlet, and then test how ligerui was running.

1. Download ligerui components. This is very simple. You can find it directly at www.ligerui.com.

2. Use Eclipse to create a web project and then build the following directory structure.



3. Add ligerui components to WebContent/ligerUI. The related components are as follows:

4. Through the above several steps, we have set up the most basic ligerui environment. We can directly access ligerUI/index.htm. Although this is static data, it can tell you whether all the related files are imported. Note that When referencing ligerui components, jsp uses full paths to access these components and does not use relative paths. In the jsp environment, the forward command changes the current path.

5. simulate a ligerui tree component. During the simulation, we first observe the static data format of ligerui, and then use the object-oriented idea to extract the javabean of related components.


The following data is the data format of indexdata. js on the ligerui homepage. Observe the data and then combine it with ligerui

{Isexpand: "false", text: "layout", children: [{url: "demos/layout/layoutMinWidth.htm", text: "Min width"}, {url: "demos/layout/layoutAutoHeight.htm", text: "auto height" },{ url: "demos/layout/layoutAutoHeightAndDiff.htm", text: "" },{ url: "demos/layout/layoutCenterOnly.htm", text: "show only the middle part" },{ url: "demos/layout/layoutFixedHeight.htm", text: "fixed height" },{ url: "demos/layout/layoutFullHeight.htm", text: "full screen height" },{ url: "demos/layout/layoutHalfHeight.htm", text: "percentage height" },{ url: "demos/layout/layoutLeftMiddleOnly.htm", text: "show only left and center" },{ url: "demos/layout/layoutLeftWidth.htm", text: "Limit left width "}, {url: "demos/layout/layoutLeftHide.htm", text: "hide on the left" },{ url: "demos/layout/layoutHideToggle.htm", text: "the right side of the left cannot be hidden"}, {url: "demos/layout/layoutResizeDisable.htm", text: "The bottom of the Left cannot be resized"}]}

You can see from the above that a menu has a sub menu or multiple sub-leaf nodes. The specific code is as follows:

Package net. itaem. vo; import java. util. list; import net. sf. json. JSONArray; import net. sf. json. JSONObject;/*** menu ** is used to simulate the ligerui menu ***/public class MenuVo {/*** id value **/private String id; /*** menu name **/private String text;/*** whether to enable the menu. By default, the menu is disabled **/private boolean isexpand; /*** submenu under the menu **/private List
 
  
Children;/*** parent menu of the menu **/private MenuVo parent; // parent menu id/*** menu url **/private String url; public MenuVo () {} public MenuVo (String id, String text, boolean isexpend) {this. id = id; this. text = text; this. isexpand = isexpend;} public String getUrl () {return url;} public void setUrl (String url) {this. url = url;} public MenuVo getParent () {return parent;} public void setParent (MenuVo parent) {this. parent = parent;} public String getId () {return id;} public void setId (String id) {this. id = id;} public String getText () {return text;} public void setText (String text) {this. text = text;} public boolean isIsexpand () {return isexpand;} public void setIsexpand (boolean isexpand) {this. isexpand = isexpand;} public List
  
   
GetChildren () {return children;} public void setChildren (List
   
    
Children) {this. children = children;}/*** use recursion to generate the json string of the Menu * @ return the json string of the Menu * @ see MenuVo # menuJson (MenuVo) **/@ Overridepublic String toString () {JSONObject json = new JSONObject (); // generate the basic data json for this node. put ("text", text); json. put ("isexpand", isexpand); json. put ("url", url); if (parent! = Null) {json. put ("pid", parent. getId ();} // generates the submenu data of this node. JSONArray childrenJson = new JSONArray (); if (children! = Null) {for (MenuVo child: children) {// recursively generate the json data childrenJson for each sub menu. add (toJson (child);} json. put ("children", childrenJson);} return json. toString ();}/*** recursive entry * @ see MenuVo # toJson () **/private String toJson (MenuVo menu) {JSONObject json = new JSONObject (); if (menu. getChildren ()! = Null) {// generate the basic data json of this menu node. put ("text", menu. getText (); json. put ("id", menu. getId (); if (menu. getParent ()! = Null) {json. put ("pid", menu. getParent (). getId ();} json. put ("isexpand", menu. isIsexpand (); if (menu. getParent ()! = Null) {json. put ("pid", menu. getParent (). getId ();} // generate the sub-menu data of this menu node JSONArray childrenJson = new JSONArray (); if (menu. getChildren ()! = Null) {for (MenuVo child: menu. getChildren () {// recursively generate the json data childrenJson for each sub menu. add (toJson (child);} json. put ("children", childrenJson) ;}} else {// This node is not a menu, but a specific subnode under the menu. This node has no subnode json. put ("id", menu. getId (); if (menu. getParent ()! = Null) {json. put ("pid", menu. getParent (). getId ();} json. put ("text", menu. getText (); json. put ("url", menu. getUrl ();} return json. toString ();}/*** hasCode Method * due to business logic needs, rewrite **/@ Overridepublic int hashCode () {final int prime = 31; int result = 1; result = prime * result + (children = null )? 0: children. hashCode (); result = prime * result + (id = null )? 0: id. hashCode (); result = prime * result + (isexpand? 1231: 1237); result = prime * result + (parent = null )? 0: parent. hashCode (); result = prime * result + (text = null )? 0: text. hashCode (); result = prime * result + (url = null )? 0: url. hashCode (); return result;}/*** equals Method * due to business logic needs, rewrite **/@ Overridepublic boolean equals (Object obj) {if (this = obj) return true; if (obj = null) return false; if (getClass ()! = Obj. getClass () return false; MenuVo other = (MenuVo) obj; if (children = null) {if (other. children! = Null) return false;} else if (! Children. equals (other. children) return false; if (id = null) {if (other. id! = Null) return false;} else if (! Id. equals (other. id) return false; if (isexpand! = Other. isexpand) return false; if (parent = null) {if (other. parent! = Null) return false;} else if (! Parent. equals (other. parent) return false; if (text = null) {if (other. text! = Null) return false;} else if (! Text. equals (other. text) return false; if (url = null) {if (other. url! = Null) return false;} else if (! Url. equals (other. url) return false; return true ;}}
   
  
 

We use recursion to generate json data for each menu, because a menu can have N levels of sub-menus, so it is suitable to use recursion to solve the problem.

Next we use servlet to generate a 10*10*3 directory menu. Below is the servlet:

Package net. itaem. servlet; import java. io. IOException; import java. util. arrayList; import java. util. list; import javax. servlet. servletException; import javax. servlet. http. httpServlet; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletResponse; import net. itaem. vo. menuVo; import net. sf. json. JSONArray; public class extends HttpServlet {/*****/private static final long serialVersionUID = response; @ Overrideprotected void doGet (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doPost (req, resp) ;}@ Overrideprotected void doPost (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {JSONArray json = new JSONArray (); // generate a 10*10*3 tree for (int I = 0; I <10; I ++) {MenuVo menu = new MenuVo (); menu. setText ("text" + I); menu. setId (I + ""); // expand the menu with an even number. setIsexpand (false); List
 
  
Children = new ArrayList
  
   
(); For (int j = 0; j <10; j ++) {MenuVo menu1 = new MenuVo (); menu1.setText ("text" + I + j ); menu1.setId (I + "" + j); children. add (menu1); menu1.setParent (menu); List
   
    
Menu1Children = new ArrayList
    
     
(); For (int k = 0; k <3; k ++) {MenuVo menuVo = new MenuVo (); menuVo. setText ("text" + I + "" + j + "" + k); menuVo. setId ("text" + I + "" + j + "" + k); menuVo. setUrl ("#"); menuVo. setParent (menu1); menu1Children. add (menuVo);} menu1.setChildren (menu1Children); menu. setChildren (children);} json. add (menu. toString (); System. out. println (menu. toString ();} req. setAttribute ("treeData", json. toString (); req. getRequestDispatcher ("ligerUI/index. jsp "). forward (req, resp );}}
    
   
  
 

Then configure the relevant servlet:

 
   
      
        
   
    LigerUiTreeServlet
         
   
    net.itaem.servlet.LigerUiTreeServlet
     
        
        
   
    LigerUiTreeServlet
         
   
    /ligerTree.do
     
        
      
   
    index.jsp
     
  
 

Then, every time you access this servlet, you will jump to ligerUI/index. jsp, which is basically the same as the static Code provided by ligerui. You only need to change the data in the tree to the data provided by the background servlet.

The component can certainly be done.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.