Chinaunix首页 | 论坛 | 博客
  • 博客访问: 30465108
  • 博文数量: 708
  • 博客积分: 12163
  • 博客等级: 上将
  • 技术积分: 8240
  • 用 户 组: 普通用户
  • 注册时间: 2007-12-04 20:59
文章分类

全部博文(708)

分类: Java

2008-11-26 17:09:32

Menu.JSP源码

<%@page contentType="text/html; charset=GBK"%>
<%@taglib uri="" prefix="c"%>


menu







请指定XML文件:
<%--文档路径--%>









${xmlfile} 下的菜单结构:

<%--通过jstl标准标签的foreach循环输出menu--%>
<%--foreach里的items值是从request里得到的menus_name-arraylist-bean--%>


菜单:






<%--遍历输出子items菜单信息--%>

<%--如果该bean内的menu_name等于父元素menu_name则将items增加到这个menu下--%>



子菜单名称:

子菜单URL:

子菜单链接说明:














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




搜狐
雅虎
中国人
猫扑
MSN
新浪


土豆
我乐
偶偶
新浪视频
猫扑视频
Youtube


中关村
PChome
InTo中关村
PConlie
数码


娱乐网站1


menus.xsl








<BR>









-------------------

JBuild 2005 + windows文本编辑器创建。

阅读(2864) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~