Implement Ajax-based unlimited menus

Source: Internet
Author: User

Framework features:

Supports form-based non-transient submission (the method is a bit stupid)

Supports the MVC framework.

Multi-thread concurrent requests (to support threads in languages)

Dynamic file loading, only useful! Handles the problem of bloated JS files in the Ajax framework.

Use the full Div + CSS layout of no table

A. Get the XMLHTTPRequest object, which can be found everywhere on the Internet, not to mention:

function newXMLHttpRequest() {

var xmlreq = false;

if (window.XMLHttpRequest) {

xmlreq = new XMLHttpRequest();

} else if (window.ActiveXObject) {

try {

xmlreq = new ActiveXObject("Msxml2.XMLHTTP");

} catch (e1) {

try {

xmlreq = new ActiveXObject("Microsoft.XMLHTTP");

} catch (e2) {

}

}

}

return xmlreq;

}

Here we provide a general method to support multiple browsers.

B. Submit an asynchronous request

// Here we use bcandy as the method name to thank someone who is very important to me and she has been supporting me.

Function bcandy (TID, URL, parm, JS ){

If (url = ""){

Return;

}

// This is a prompt box for loading information, or not!

Document. getelementbyid ("LOAD"). style. Visibility = "visible ";

// Load the JS file of the corresponding page

If (JS! = NULL ){

// Load the JS File

Loadjs (JS );

}

// Obtain an XMLHttpRequest instance

VaR Req = newxmlhttprequest ();

// Set the handle function used to receive callback notifications from the request object

VaR handlerfunction = getreadystatehandler (req, tid );

Req. onreadystatechange = handlerfunction;

// The third parameter indicates that the request is asynchronous.

Req. Open ("Post", URL, true );

// Indicates that the request body contains form data

Req. setRequestHeader ("Content-Type ",

"Application/X-WWW-form-urlencoded ");

// Sending Parameters

Req. Send (parm );

}

Function getreadystatehandler (req, tid ){

// Returns an anonymous function that listens to the XMLHttpRequest instance.

Return function (){

// If the request status is "complete"

If (req. readystate = 4 ){

// The server response is successfully received.

If (req. Status = 200 ){

// The following sentence is important. The content of the returned information is displayed and can be modified. Other Processing

Document. getelementbyid (TID). innerhtml = Req. responsetext;

Document. getelementbyid (TID). style. Visibility = "visible ";

// This statement is used to hide or hide the loading prompt box.

Document. getelementbyid ("LOAD"). style. Visibility = "hidden ";

} Else {

// An HTTP Error occurs.

Document. getelementbyid ("LOAD"). style. Visibility = "hidden ";

Alert ("HTTP Error:" + Req. status );

}

}

}

}

// Dynamically load JS files

Function loadjs (File ){

VaR head = Document. getelementsbytagname ('head'). Item (0 );

VaR script = Document. createelement ('script ');

Script. src = file;

Script. type = "text/JavaScript ";

Head. appendchild (SCRIPT );

}

This is the basic framework, because the request is used. responsetext; therefore, you can directly request a page JSP, Servlet, but special processing is required for requests using the Struts framework, because form does not support asynchronous requests. We recommend that you do not add tags on these pages, just like the asxm file in. net! When using the Struts framework, note that the mapping object can return NULL directly, because we will talk about concurrent multithreading below. To solve this problem.

In general, it seems like building blocks. This facilitates File Modification and expansion without affecting each other, and achieves code and tag separation. During the traditional page revision, you do not need to rewrite all the code. You only need to modify a small part to perfectly implement the non-flash refreshing pleasure brought by Ajax.

The above code has been tested in IE and Firefox.

First, create a data table menu

 

Mid menu primary key

Name menu name

URL menu Link

Father low-level menu ID

Whether sub is the underlying menu (used to determine whether sub can be expanded)

Target menu Link Target (as the display ID when opened in Ajax Mode)

Pa menu parameters (this is used to open the menu in Ajax Mode)

 

Create a menu object class

Class Menu {

Private int mid;

Private string name;

... // Other members

Public getmid (){

Return mid;

}

Public setmid (INT mid ){

This. Mid = mid;

}

... // Get Set Method for other members,

}

 

The other is the operation class.

Class menuopt (){

Public vector getmenus (INT father ){

Vector vector = new vector ();

// Obtain all the menus whose parent menu ID is father.

// And encapsulate it in an object of the vector ..

Return vector;

}

}

The second is the general JSP file. But do not include tags as you have mentioned before!

Menu. jsp:

% @ Page contenttype = "text/html; charset = gb2312" %>

% @ Taglib uri = "http://java.sun.com/jsp/jstl/core" prefix = "C" %>

JSP: usebean id = "menu" Scope = "page" class = "ycoe. Basic. menuopt"/>

JSP: setproperty name = "menu" property = "father" value = "$ {Param. Father}"/>

Div>

C: foreach Var = "M" items = "$ {menu. Vector}" varstatus = "C">

C: Choose>

C: When test = "$ {M. sub EQ 'y'}">

Div onclick = "showmenu('{}m.mid}', '{}m.url}', '{}m.tar get} ', 'Father =$ {M. Mid}')">

IMG src = "PIC/menu0.gif" id = "IMG $ {M. Mid}" alt = "" style = "cursor: hand;">

A href = "#" class = "text1" >$ {M. name}

/Div>

Div style = "display: none;" id = "Tr $ {M. Mid}">

Div style = "padding-left: 12pt" id = "$ {M. Mid}">

/Div>

/C: When>

C: otherwise>

Div onclick = "openmenu('{}m.url}', '{}m.tar get} ',' $ {M. Pa} ');">

IMG src = "PIC/menu1.gif" id = "IMG $ {M. Mid}" alt = "">

A href = "#" class = "text1" >$ {M. name}

/Div>

Menu. JS:

// Opermenu (open the drop-down menu ID, open address, link to open the target, parameters ).

// This is the method used in menu. jsp

Function showmenu (ID, URL, target, Param ){

VaR trobj = Document. getelementbyid ("TR" + id );

VaR tdobj = Document. getelementbyid (ID );

// Try {

If (document. getelementbyid ("TR" + id). style. Display = "NONE "){

// Display menu

If (tdobj. innerhtml = NULL | tdobj. innerhtml = ""){

// Extract data

Document. getelementbyid ("TR" + id). style. Display = "";

Document. getelementbyid ("IMG" + id). src = "PIC/menu2.gif"

Bcandy (ID, "page/menu. jsp", Param ,"");

Openmenu (URL, target, Param );

} Else {

// If there is content in it, it is displayed directly

Document. getelementbyid ("TR" + id). style. Display = "";

Document. getelementbyid ("IMG" + id). src = "PIC/menu2.gif"

Openmenu (URL, target, Param );

}

// Bcandy (target, URL, Param, ""); // open the menu Link

} Else {

// Hide the menu

Document. getelementbyid ("TR" + id). style. Display = "NONE ";

Document. getelementbyid ("IMG" + id). src = "PIC/menu0.gif"

}

//} Catch (e ){}

}

// Open the menu

Function openmenu (URL, target, Param ){

// Do not write it here. There are several implementation methods. We recommend using Ajax!

}

Finally, the page is displayed:

% @ Page contenttype = "text/html; charset = gb2312" %>

Meta http-equiv = Content-Type content = "text/html; charset = gb2312">

Style>

. Text1: hover {border: 1px #999999 solid; Background-color: # cccccc; Height: 12px ;}

. Text1 {border: 1px # ffffff solid; Height: 12px ;}

Function INI (){

Bcandy ("0", "menu. jsp", "id = 0 & father = 0", "menu. js ");

}

Body onload = "INI ();">

Div id = "LOAD" style = "Z-index: 1; color: # ff0000;

Visibility: hidden; filter: alpha (opacity = 85 );

Background-color: # ffffff; left: 48%; top: 48%;

Border-Right: #000000 1px solid;

Padding-Right: 12px; border-top: #000000 1px solid;

Padding-left: 12px; padding-bottom: 12px;

Border-left: #000000 1px solid;

Line-Height: 22px; padding-top: 12px;

Border-bottom: #000000 1px solid; position: absolute; ">

IMG src = 'pic/loop.gif 'alt = "">

Processing Data. Please wait...

BR>

Div id = "0" align = "center">

We can see that at any level, there is nothing different from the traditional one. Only the JSP part removes the file header (in fact, it does not remove the file header). In addition, we can also see that, A page has been divided into several parts. As I mentioned earlier, it is a modular structure (this is a structure proposed by the author when I saw a. NET Framework Structure on the Internet. I think it is good and I applied it to JSP ).

I have made some reservations for some details. Please understand. However, the general framework is tested by IE and Firefox. I think about some functional extensions.

Principle: Page recursion is actually applied! Just like a general recursive method, but it is only used on the page.

Div id = "Tr $ {M. ID}">

Loop to display the objects encapsulated into the vector one by one

For {

If (if it is the top menu sub = N ){

Div id = "T $ {M. ID}" onclick = "showmenu ($ {M. Father...})">

Show menu content

Div style = "display: none" id = "TD $ {M. ID...}">

} Else {

Div onclick = "openmenu ($ {M. ID})"> display menu content

}

}

The showmenu (father, ID...) method calls this page again after obtaining data from the server based on the passed-in father. In this case, the page content is displayed in the new ID. In this way, it looks like the tree menu in msdn.

Advantage: Multi-Level menus are obtained multiple times, which speeds up the response. Ajax requests are also applied, making the page flickering invisible and highly friendly. In addition, code can be added to JS so that users do not need to obtain server data every time they click. Instead, they can first determine whether there is any content and no more information. At the same time, menus and pages are synchronized. You can open the page at the corresponding place for each level menu opened. Similarly, opermenu () can also be Ajax.

Go to: http://www.helloajax.com/article/0/58.htm

Related Article

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.