Menu.JSP源碼
<%@page contentType="text/Html; charset=GBK"%>
<%@taglib uri="http://Java.sun.com/JStl/core_rt" prefix="c"%>
<Html>
<head>
<title>menu</title>
<style>
.btn input{
background:#FF6600;
border-left:1px #FFFFFF solid;
border-top:1px #FFFFFF solid;
border-right:1px #990000 solid;
border-bottom:1px #990000 solid;
padding-top:4px;
color:#FFFFFF;
}
</style>
<!--以下JS腳本處理menu的浏覽器端增,刪,改-->
<script language="Javascript" type="">
function newItem(ele){
var pNode;
var pdiv;
var item_name,item_url,item_tip;
var text1,text2,text3;
pNode =ele.parentNode;
pdiv=document.createElement("<div class='item'></div>");
item_hidden=document.createElement("<input id='test' type='hidden' name='parent' value='"+pNode.childNodes[1].value+"'/>");
text1 =document.createTextNode("子菜單名稱:")
item_name=document.createElement("<input type='text' name='menuitem_name'/>");
text2 =document.createTextNode("子菜單URL:")
item_url=document.createElement("<input type='text' name='menuitem_url'/>");
text3 =document.createTextNode("子菜單鏈接說明:")
item_tip=document.createElement("<input type='text' name='menuitem_title'/>");
pdiv.appendChild(item_hidden);
pdiv.appendChild(text1);
pdiv.appendChild(item_name);
pdiv.appendChild(text2);
pdiv.appendChild(item_url);
pdiv.appendChild(text3);
pdiv.appendChild(item_tip);
pdiv.appendChild(document.createElement("<input type='button' value='刪除此子菜單' onclick='delItem(this)'/>"));
pNode.appendChild(pdiv);
}
function delItem(ele){
var pNode;
var pdiv;
pNode =ele.parentNode.parentNode;
pdiv =ele.parentNode;
pNode.removeChild(pdiv)
}
function newMenu(ele){
var pNode;
var pdiv;
var text1,menunameElement,newmenuBtnElement,delmenuBenElement;
pNode=ele.parentNode.parentNode;
pdiv = document.createElement("<div style='border:#999999 1px solid; background:#FFFFCC; padding:5px; margin-bottom:10px;disply:block;'></div>");
text1 =document.createTextNode(" 菜單:");
menunameElement =document.createElement("<input type='text' name='menuname' style='background:#cccccc;'/>");
newmenuBtnElement=document.createElement(" <input type='button' value='新增子菜單' onclick='newItem(this)'/>");
delmenuBenElement =document.createElement("<input type='button' value='刪除此菜單' onclick='delItem(this)'/>");
pdiv.appendChild(text1);
pdiv.appendChild(menunameElement);
pdiv.appendChild(delmenuBenElement);
pdiv.appendChild(newmenuBtnElement);
pNode.insertBefore(pdiv,document.getElementById("btn"));
}
</script>
</head>
<body bgcolor="#ffffff">
<form action="XMLservlet" method="post">
<div style="background:#FFFFFF; border:#333333 1px solid; font-family:Arial; font-size:12px; padding:5px;">
<div style="border:#999999 1px solid; background:#FFFFCC; padding:5px; margin-bottom:10px;"> 請指定XML文件:
<%--文檔路徑--%>
<input type="file" name="xmlfilename" value="${XMLfile}"/>
<br/>
<input name="" type="submit"/>
</div>
</div>
</form>
<form action="newXMLservlet" method="POST">
<div style="background:#FFFFFF; border:#333333 1px solid; font-family:Arial; font-size:12px; padding:5px;">
<div style="background:#ff6600; border:#333333 1px solid; font-family:Arial; font-size:12px; padding:5px;">
${xmlfile} 下的菜單結構:<input type="hidden" name="xmlfile" value="${XMLfile}"/>
</div>
<%--通過JStl標准標簽的foreach循環輸出menu--%>
<%--foreach裡的items值是從request裡得到的menus_name-arraylist-bean--%>
<c:forEach var="menu" items="${menus}">
<div style="border:#999999 1px solid; background:#FFFFCC; padding:5px; margin-bottom:10px;disply:block;">
菜單:<input type="text" name="menuname" value="${menu.menu_name}" style="background:#cccccc;"/>
<input type="button" value="新增子菜單" onClick="newItem(this)"/>
<input type="button" value="刪除${menu.menu_name}菜單" onclick="delItem(this)"/>
<br/>
<br/>
<%--遍歷輸出子items菜單信息--%>
<c:forEach var="mitem" items="${menuitems}">
<%--如果該bean內的menu_name等於父元素menu_name則將items增加到這個menu下--%>
<c:if test="${mitem.menu_name==menu.menu_name}">
<div class="item">
<input type="hidden" name="parent" value="${mitem.menu_name}"/>
子菜單名稱:
<input type="text" name="menuitem_name" value="${mitem.menuitem_text}"/>
子菜單URL:
<input type="text" name="menuitem_url" value="${mitem.menuitem_url}"/>
子菜單鏈接說明:
<input type="text" name="menuitem_title" value="${mitem.menuitem_tip}"/>
<input type="button" value="刪除此子菜單" onClick="delItem(this)"/>
</div>
<br/>
</c:if>
</c:forEach>
</div>
</c:forEach>
<div class="btn" id="btn" style="border:#999999 1px solid; background:#235478; padding:5px; margin-bottom:10px;">
<input name="" type="button" value="新增菜單" onClick="newMenu(this)"/>
<input name="" type="submit" value="保存修改"/>
<span style="background:#ffffff;padding:3px;"><a href="${xmlfile}">浏覽XML</a></span>
</div>
</div>
</form>
</div>
</body>
</Html>
XMLselvlet源碼
package XTO;
import XMLbean.*;
import Javax.servlet.*;
import Javax.servlet.http.*;
import Java.io.*;
import Java.util.*;
import Javax.XML.parsers.*;
import org.w3c.dom.*;
import org.XML.sax.*;
public class XMLservlet
extends HttpServlet {
private static final String CONTENT_TYPE = "text/Html; charset=utf-8";
//Initialize global variables
public void init() throws ServletException {
}
private String fs(Object obj){
if(obj==null){
obj="";
}
return obj.toString();
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
response.setContentType(CONTENT_TYPE);
request.setCharacterEncoding("GBK");
String XMLfile = "";
//得到文件XML文件
xmlfile = fs(request.getParameter("XMLfilename"));
if(xmlfile==null||XMLfile==""){
xmlfile =(String)request.getAttribute("XMLfilename");
}
//創建存放menuname的bean ArrayList
ArrayList alist_menuname = new ArrayList();
//創建存放menuname和menuItem信息的bean ArrayList
ArrayList alist_items = new ArrayList();
//創建DocumentBuilderFactory實例,用於解析XML文檔
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
//創建DocumentBuilder
DocumentBuilder db = null;
try {
db = dbf.newDocumentBuilder();
}
catch (ParserConfigurationException ex1) {
System.out.print(ex1.toString());
request.setAttribute("Error", ex1.toString());
}
//創建document用於讀入XML文檔
Document doc = null;
try {
doc = db.parse(new File(XMLfile));
}
catch (Exception ex) {
System.out.print(ex.toString());
request.setAttribute("Error", ex.toString());
}
//得到根節點
Element root = doc.getDocumentElement();
//得到根節點的下級子元素
NodeList children = root.getChildNodes();
//遍歷根節點的下級子元素
for (int i = 0; i < children.getLength(); i++) {
Node child = children.item(i);
if (child instanceof Element) {
//創建當前讀取到的節點元素對象
Element childElement = (Element) child;
//判斷當前元素的tagName是否為想要得到的"menu"
if (childElement.getTagName().equals("menu") != false) {
//創建bean實例
menuBean mb = new menuBean();
//把當前menu節點的name屬性值放入這趟循環的bean實例中
mb.setMenu_name(childElement.getAttribute("name"));
//堆入專門存放menu_name bean的一個 arraylist
alist_menuname.add(mb);
}
//得到當前menu節點下的子節點結構
NodeList cEchildren = childElement.getChildNodes();
//循環遍歷menu節點下子節點信息
for (int j = 0; j < cEchildren.getLength(); j++) {
Node menuitem = cEchildren.item(j);
if (menuitem instanceof Element) {
Element menu = (Element) menuitem;
if (menu.getTagName() == "items") {
menuBean mb2 = new menuBean();
mb2.setMenu_name(childElement.getAttribute("name"));
mb2.setMenuitem_url(menu.getAttribute("url"));
mb2.setMenuitem_tip(menu.getAttribute("tip"));
mb2.setMenuitem_text( ( (Text) menu.getFirstChild()).getData().
trim());
alist_items.add(mb2);
}
}
}
}
}
//將兩個arraylist放進request裡
request.setAttribute("menus", alist_menuname);
request.setAttribute("menuitems", alist_items);
//把當前操作的文件路徑放入request李
request.setAttribute("xmlfile",XMLfile);
//服務器段轉向
request.getRequestDispatcher("menu.JSP").forward(request, response);
//處理以get方法的表單提交
doGet(request, response);
}
//Clean up resources
public void destroy() {
}
}
newXMLselvlet源碼
package XTO;
import XMLbean.*;
import Javax.servlet.*;
import Javax.servlet.http.*;
import Java.io.*;
import Java.util.*;
import Javax.XML.parsers.*;
import org.w3c.dom.*;
import org.XML.sax.*;
import Javax.XML.transform.*;
import Javax.XML.transform.dom.*;
import Javax.XML.transform.stream.*;
public class newXMLservlet
extends HttpServlet {
public newXMLservlet() {
try {
jbInit();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
private static final String CONTENT_TYPE = "text/Html; charset=GBK";
//
private DocumentBuilder db;
//Initialize global variables
public void init() throws ServletException {
}
//輸出XML文檔樹到文件
private void saveDocument(Document doc, File f) throws
TransformerFactoryConfigurationError, TransformerConfigurationException,
FileNotFoundException,
TransformerException {
Transformer t = TransformerFactory.newInstance().newTransformer();
Properties props=new PropertIEs();
//格式化輸出
props.put(OutputKeys.INDENT,"yes");
//設置XML-encoding
props.put(OutputKeys.ENCODING,"GB2312");
t.setOutputPropertIEs(props);
t.transform(new DOMSource(doc), new StreamResult(new FileOutputStream(f)));
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
}
//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
response.setContentType(CONTENT_TYPE);
request.setCharacterEncoding("GBK");
//獲得XML文件
String xmlfile =request.getParameter("XMLfile").toString();
File f =new File(XMLfile);
//創建DocumentBuilderFactory實例
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
//
db = dbf.newDocumentBuilder();
}
catch (ParserConfigurationException ex) {
System.out.print(ex.toString());
}
//創建文檔模型
Document doc = db.newDocument();
//設置文檔引用節點
Node sheet = doc.createProcessingInstruction("XML-stylesheet",
"type="text/xsl" href="menus.xsl"");
doc.appendChild(sheet);
//創建根元素
Element menusroot = doc.createElement("menus");
//以從表單提交過來的"menuname"遍歷新建"menus"元素信息
for (int i = 0; i < request.getParameterValues("menuname").length; i++) {
Element menu = doc.createElement("menu");
menu.setAttribute("name", request.getParameterValues("menuname")[i]);
//以表單提交過來的"menuitem_name"值個數,循環新建子菜單元素Items
for (int j = 0; j < request.getParameterValues("menuitem_name").length; j++) {
if (request.getParameterValues("parent")[j].equals(request.
getParameterValues("menuname")[i])) {
Element menuitem = doc.createElement("items");
menuitem.setAttribute("url",
request.getParameterValues("menuitem_url")[j]);
menuitem.setAttribute("tip",
request.getParameterValues("menuitem_title")[j]);
Text itemname = doc.createTextNode(request.getParameterValues(
"menuitem_name")[j]);
menuitem.appendChild(itemname);
menu.appendChild(menuitem);
}
}
//將節點增加至根元素menus
menusroot.appendChild(menu);
}
//將根元素增加至文檔對象
doc.appendChild(menusroot);
try {
//調用xml文檔輸出方法,傳入文檔樹形結構,和欲輸出XML文件路徑
this.saveDocument(doc, f);
}
catch (Exception ex1) {
System.out.print(ex1.toString());
}
//保存成功依然返回到XMLservlet,並且刷新讀取了保存後的文檔結構
request.setAttribute("xmlfilename",XMLfile);
request.getRequestDispatcher("XMLservlet").forward(request,response);
//
doGet(request, response);
}
//Clean up resources
public void destroy() {
}
private void jbInit() throws Exception {
}
}
menuBean源碼
package XMLbean;
public class menuBean {
public menuBean() {
}
private String menu_name;
private String menuitem_url;
private String menuitem_tip;
private String menuitem_text;
public String getMenuitem_url() {
return menuitem_url;
}
public String getMenuitem_tip() {
return menuitem_tip;
}
public String getMenuitem_text() {
return menuitem_text;
}
public String getMenu_name() {
return menu_name;
}
public void setMenuitem_url(String menuitem_url) {
this.menuitem_url = menuitem_url;
}
public void setMenuitem_tip(String menuitem_tip) {
this.menuitem_tip = menuitem_tip;
}
public void setMenuitem_text(String menuitem_text) {
this.menuitem_text = menuitem_text;
}
public void setMenu_name(String menu_name) {
this.menu_name = menu_name;
}
}
menu.XML
<?XML version="1.0" encoding="GB2312"?>
<?XML-stylesheet type="text/xsl" href="menus.xsl"?>
<menus>
<menu name="資訊門戶網站">
<items tip="" url="#">搜狐</items>
<items tip="" url="#">雅虎</items>
<items tip="" url="#">中國人</items>
<items tip="" url="#">貓撲</items>
<items tip="" url="#">MSN</items>
<items tip="中國最大資訊門戶之一" url="http://sina.com.cn">新浪</items>
</menu>
<menu name="播客門戶網站">
<items tip="" url="#">土豆</items>
<items tip="" url="#">我樂</items>
<items tip="" url="#">偶偶</items>
<items tip="" url="#">新浪視頻</items>
<items tip="" url="#">貓撲視頻</items>
<items tip="" url="#">Youtube</items>
</menu>
<menu name="IT門戶網站">
<items tip="" url="#">中關村</items>
<items tip="" url="#">PChome</items>
<items tip="" url="#">InTo中關村</items>
<items tip="" url="#">PConlIE</items>
<items tip="數碼家電引領者" url="#">數碼</items>
</menu>
<menu name="娛樂網站">
<items tip="這是一個不錯的娛樂網站" url="#">娛樂網站1</items>
</menu>
</menus>
menus.xsl
<?XML version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" XMLns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:output method="Html"></xsl:output>
<xsl:template match="/menus">
<Html>
<head>
<title>
</title>
<link href="menu.css" rel="stylesheet" type="text/CSS" />
<script language="Javascript">
var menus=new Array();
var menulist;
var menunamepraentnode;
var n;
var f;
function MenuWork(menuname) {
menus.push(menuname);
for(var i=0;i<menus.length;i++){
if(menus[i]!=menuname){
menulist(menus[i]).style.display="none";
}
}
var display = menulist(menuname).style.display;
menulist(menuname).style.display = (display == "block") ? "none" : "block";
if(menulist(menuname).style.display=="block"){
n =menulist(menuname);
n.style.overflow="hidden";
}
}
function menulist(menuname){
menunamepraentnode =menuname.parentNode;
x =menunamepraentnode.childNodes[1];
return x;
}
</script></head>
<body>
<div id="menubody">
<xsl:for-each select="menu">
<div id="menus">
<div id="menuname" onmousedown="MenuWork(this)">
<xsl:value-of select="@name"/>
</div>
<div id="menuitems">
<ul id="items">
<xsl:for-each select="items">
<li>
<xsl:element name="a">
<xsl:attribute name="href"><xsl:value-of select="@url"/></xsl:attribute>
<xsl:attribute name="title"><xsl:value-of select="@tip"/></xsl:attribute>
<xsl:value-of select="."/>
</xsl:element>
</li>
</xsl:for-each>
</ul>
</div>
</div>
</xsl:for-each>
</div>
</body>
</Html>
</xsl:template>
</xsl:stylesheet>
-------------------
JBuild 2005 + Windows文本編輯器創建。