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/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/106943/viewspace-927242/