DHTML Tree 使用例項

nighthun發表於2007-07-20

Java 動態選單例項

一.jsp頁面顯示選單(最簡單的樹型選單)

PAS選單

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

request.setAttribute("id", id);

%>

tree=new dhtmlXTreeObject(document.getElementById('menuTree'),"100%","100%",0);

tree.setImagePath("../images/imgs/csh_winstyle/");//設定選單圖片風格路徑。

//link tree to asp script

tree.setXMLAutoLoading("../menuTree.do?method=showMenu");

//load first level of tree

tree.loadXML("../menuTree.do?method=showMenu&type=root&id=");

//設定點選事件,開啟對應連線頁面。

tree.setOnClickHandler(doOnClick);

//tree.setOnOpenHandler(tonopen);

//設定選單圖片型別,目前有3中風格可選,也可自定義風格。

//tree.setImageArrays("plus","","","","plus_ar.gif");

//tree.setImageArrays("minus","","","","minus_ar.gif");

//tree.setStdImages("book.gif","books_open.gif","books_close.gif");

function doOnClick(nodeId){

var myUrl = tree.getUserData(nodeId,"file")

if(null != myUrl && myUrl !='#'){

parent.mainMenu.location=myUrl;

}

}

二.Action取得選單資料,並處理選單格式。

使用dom4j格式化資料。

public class ShowMenuAction extends PasBaseAction{

@Override

protected Class setClass() {

return this.getClass();

}

//dom4j 物件

private Document document = null;

private Element rootElement = null;

private Element prevElement = null;

public ActionForward showMenu(

ActionMapping mapping,

ActionForm form,

HttpServletRequest request,

HttpServletResponse response)throws Exception {

// a) 獲取引數grpId

String xmdh = request.getParameter("id");//上級選單編號

String type = request.getParameter("type");//根選單型別。

//取使用者

LoginUser user = getLoginUser(request);

MenuService menuSrv = (MenuService)SpringAppContext.getBean(MenuService.KEY);

//根選單

if (null != type && type.equals("root")) {

document = DocumentHelper.createDocument();

rootElement = document.addElement("tree");

rootElement.addAttribute("id", "0");

XtbXtcd key = new XtbXtcd();//選單物件。(下附選單物件類)

key.setXmdh(Integer.parseInt(xmdh));

XtbXtcd rootCd = menuSrv.selectByPrimaryKey(key);//根選單物件

addElement(rootElement,rootCd);

} else {

document = DocumentHelper.createDocument();

rootElement = document.addElement("item");

rootElement.addAttribute("id", xmdh);

//下級選單資料集合(附帶使用者許可權)

List menu = menuSrv.LoadUserMenuChildrens(user.getKhdxdh().toString(),xmdh);

for (Iterator it=menu.iterator() ; it.hasNext(); ){

XtbXtcd firstCd = (XtbXtcd)it.next();

//找到父選單的element

String pareId = firstCd.getSjxm()+"";

prevElement = null;

Iterator itr = rootElement.elementIterator();

getElement(itr, pareId);

addElement(rootElement,firstCd);

}

}

//輸出格式化後的資料

Writer writer = response.getWriter();

response.setContentType("text/xml; charset=GB2312");

document.write(writer);

writer.close();

return null;

}

/**

* 找到父id

* @param nodelist

* @param id

*/

private void getElement(Iterator nodelist, String id) {

if (nodelist.hasNext() == false)

return;

while (nodelist.hasNext()) {

if (prevElement != null)

break;

Element cnode = (Element) nodelist.next();

if (cnode.getNodeType() == Document.ELEMENT_NODE) {

if (id.equals(((Element) cnode).attributeValue("id"))) {

prevElement = (Element) cnode;

} else

getElement(cnode.elementIterator(), id);

}

}

return;

}

/**

* 新增item

* @param element

* @param menu

*/

void addElement(Element element,XtbXtcd menu){

Element newElement = element.addElement("item");

newElement.addAttribute("text", menu.getXmmc());

newElement.addAttribute("id", menu.getXmdh()+"");

if(!menu.isURL()){

newElement.addAttribute("child", "1");

}

addUrl(newElement,menu);

}

/**

* 新增連線

* @param element

* @param childCd

*/

void addUrl(Element element,XtbXtcd childCd){

if(childCd.isURL()){

Element urlElement=element.addElement("userdata");

urlElement.addAttribute("name", "file");

urlElement.setText(childCd.getXmdz());

}

}

public Document getDocument() {

return document;

}

public void setDocument(Document document) {

this.document = document;

}

public Element getPrevElement() {

return prevElement;

}

public void setPrevElement(Element prevElement) {

this.prevElement = prevElement;

}

public Element getRootElement() {

return rootElement;

}

public void setRootElement(Element rootElement) {

this.rootElement = rootElement;

}

}

[@more@]

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/106943/viewspace-927242/,如需轉載,請註明出處,否則將追究法律責任。

相關文章