Recently, because of the work needs, learning the Ligerui framework. However, because in the company, we just as the lowest level of the yards, so the environment has been set up, we are the yards of farmers are directly to get the project, and then in other people's frame constantly CTRL + C, CTRL + V, and then tinkering. So in order to get out of this dilemma, decide to use a simple servlet to build a Ligerui, and then test how ligerui this thing is running.
1, download Ligerui related components. This is very simple, go directly to www.ligerui.com can be found.
2. Create a Web project using Eclipse, and then build the directory structure below.
3, add the relevant components of Ligerui to the Webcontent/ligerui, the content of the relevant components are:
4, through the above several steps, we have set up a most basic ligerui environment. We can directly access the ligerui/index.htm, although this is static data, but can tell you whether the relevant files are all imported. It is important to note here that when referencing the Ligerui component, the JSP uses the full path to access the components, instead of using relative paths. Because in a JSP environment, the forward directive changes the current path.
5. Simulate a Ligerui tree component. In the simulation, we first observe Ligerui's static data format, and then use object-oriented thinking to extract the JavaBean of related components.
The following data is the data format for Ligerui home indexdata.js, observing the data, and then combining Ligerui
{isexpand: "false", Text: "Layout", children:[ {url: "demos/layout/layoutminwidth.htm", Text: "Minimum width"}, {URL: " Demos/layout/layoutautoheight.htm ", Text:" Auto Height "}, {url:" demos/layout/layoutautoheightanddiff.htm ", Text:" Height replenishment "}, {url:" demos/layout/layoutcenteronly.htm ", Text:" Show only middle section "}, {url:" demos/layout/ Layoutfixedheight.htm ", Text:" Fixed height "}, {url:" demos/layout/layoutfullheight.htm ", Text:" Full screen height "}, {URL:" Demos/layout/layouthalfheight.htm ", text:" Percent height "}, {url:" demos/layout/layoutleftmiddleonly.htm ", Text:" Show only left and Middle "}, {url:" demos/layout/layoutleftwidth.htm ", Text:" Limit Left width "}, {url:" demos/layout/ Layoutlefthide.htm ", Text:" Left just beginning to hide "}, {url:" demos/layout/layouthidetoggle.htm ", Text:" Left to right not allowed to hide "}, { URL: "demos/layout/layoutresizedisable.htm", Text: "The bottom left does not allow resizing"} ] }
from above, a menu has submenus, or multiple child leaf nodes. The following is the specific code:
Package Net.itaem.vo;import java.util.list;import Net.sf.json.jsonarray;import net.sf.json.jsonobject;/** * Menu * * menu to simulate Ligerui * * */public class Menuvo {/** * ID value * */private string id;/** * Menu Name * */private string text;/** * Whether to open menu, The default does not open * */private boolean isexpand;/** * Menu below submenu * */private list<menuvo> children;/** * The menu's Father menu * */private MENUV o parent; Parent menu id/** * Menu URL * */private string url;public Menuvo () {}public Menuvo (string ID, String text, Boolean isexpend) {th Is.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<menuvo> GetChildren () {return children;} public void Setchildren (list<menuvo> children) {This.children = children;} /** * Use recursion to generate the JSON format string for this menu * @return return the JSON format string for the menus * @see Menuvo#menujson (MENUVO) * */@Overridepub Lic String toString () {jsonobject json = new Jsonobject ();//Generate this node's basic data Json.put ("text", text); Json.put ("Isexpand", Isexpand); json.put ("url", url), if (parent! = null) {json.put ("pid", Parent.getid ());} Generate submenu data for this node Jsonarray Childrenjson = new Jsonarray (), if (children! = null) {for (Menuvo Child:children) {// Let each child menu recursively generate JSON data Childrenjson.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 for this menu node json.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 submenu data for this menu node Jsonarray Childrenjson = new Jsonarray (); if (Menu.getchildren ()! = null) {for (Menuvo child: Menu.getchildren ()) {//Allow each submenu to recursively generate JSON data Childrenjson.add (ToJson (Child));} Json.put ("Children", Childrenjson);}} else{//This node is not a menu, is a specific sub-node under the menu, the node has no child nodes 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 required, 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 required, override */@Overridepublic Boolean equals (Object obj) {if (this = obj) return true;if (o BJ = = 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 (oth Er.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.tex T)) 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 an n-level submenu, so it's a good way to use recursion to solve the problem.
Below we use the servlet to generate a 10 * 10 * 3 directory menu. Here 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 Ligeruitreeservlet extends httpservlet{/** * */private static final long serial Versionuid = 145235235L; @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 * tree for (int i=0; i<10; i++) {menuvo menu = new Menuvo (); Menu.settext ("Tex T "+ i); Menu.setid (i +" ");//Even menu expand Menu.setisexpand (False); list<menuvo> children = new arraylist<menuvo> (); 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<menuvo> Menu1children = new arraylist<menuvo> (); for (int k=0; k<3; k++) {Menuvo Menuvo = new MenuVo (); m Enuvo.settext ("text" + i + "" + j + "" + K); Menuvo.setid ("text" + i+ "" + j + "" + K); Menuvo.seturl ("#"); Menuvo.setparent (M ENU1); 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:
<?xml version= "1.0" encoding= "UTF-8"? ><web-app xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns= "Http://java.sun.com/xml/ns/javaee" xmlns:web= "http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi: schemalocation= "Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version= " 2.5 "> <display-name></display-name> <servlet> <servlet-name> Ligeruitreeservlet</servlet-name> <servlet-class>net.itaem.servlet.ligeruitreeservlet</ servlet-class> </servlet> <servlet-mapping> <servlet-name>ligeruitreeservlet </servlet-name> <url-pattern>/ligerTree.do</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </ Welcome-file-list></web-app>
Then every time you visit this servlet, it jumps to ligerui/index.jsp, which is basically consistent with the static code provided by the Ligerui, just to modify the tree data to the data provided by the background servlet.
Summary: From the above example, we can see that the rapid construction of a ligerui environment is not very difficult. As a Java Web engineer, we should have more control over this open source framework. Although the above example just simulates a menu tree, but the glimpse of the leopard, we have this example, the other Ligerui components can certainly be done.