您的位置:首页 > 运维架构 > 网站架构

jsp+javaScript结合dom4j动态生成网站配置文件

2007-11-09 17:19 609 查看
前两天项目经理给我个问题让我玩,在jsp程序中动态生成xml网站配置程序,大概的模样是
Common.xml
<?xml version="1.0" encoding="UTF-8"?>
<page>
<item>
<lanmu>
<module>首 页</module>
<superlink>../cn/index.jsp</superlink>
</lanmu>
<lanmu>
<module>人才信息</module>
<superlink>../personal/more.jsp?type=1</superlink>
</lanmu>
<lanmu>
<module>招聘信息</module>
<superlink>../enterprise/more.jsp?type=1</superlink>
</lanmu>
<lanmu>
<module>企业风采</module>
<superlink>../qiye/more.jsp?type=1</superlink>
</lanmu>
<lanmu>
<module>新闻中心</module>
<superlink>../index/index.jsp</superlink>
</lanmu>
<lanmu>
<module>建议留言</module>
<superlink>../sony/index.jsp</superlink>
</lanmu>
<lanmu>
<module>使用说明</module>
<superlink>../jianjie/index.jsp</superlink>
</lanmu>

<warning>
<manual>服务声明 </manual>
<link>../cn/index.jsp</link>
</warning>
<warning>
<manual>| 联络我们 </manual>
<link>http://wangrunxin</link>
</warning>
<warning>
<manual>| 关于我们 </manual>
<link>../personal/more.jsp?type=1</link>
</warning>
<warning>
<manual>| 法律声明 </manual>
<link>../enterprise/more.jsp?type=1</link>
</warning>
<warning>
<manual>| 帮助 </manual>
<link>../index/index.jsp</link>
</warning>
<warning>
<manual>| 更多友情链接</manual>
<link>../index/index.jsp</link>
</warning>
</item>
<toppage>
<advt>images/wanjie_banner.gif </advt>
</toppage>
<webhead>
<title>中铁十三局集团第五工程有限公司财务部</title>
</webhead>
<webfoot>
<company>中铁十三局集团第五工程有限公司财务部</company>
<tel>电话:传真:邮编:130033 </tel>
<email>wangrunxin@hotmail.com</email>
<copyright>地址:长春市 吉ICP备05002347号</copyright>
</webfoot>
<bgcolor>
<value>#ffffff</value>
</bgcolor>
<background>
<value>#ffffff</value>
</background>
<friendship>
<linkto1>
<home>银河公司</home>
<homepage>http://www.yhweb.com</homepage>
</linkto1>
<linkto2>
<home>大连IT人才网</home>
<homepage>http://www.dlithr.com</homepage>
</linkto2>
<linkto3>
<home>中华英才网</home>
<homepage>http://www.chinahr。com</homepage>
</linkto3>
<linkto4>
<home>招聘网</home>
<homepage>http://www.51job.com</homepage>
</linkto4>
</friendship>
<websub>
<sub1>药 品 招 商</sub1><link1>../comm/index.jsp</link1>
<sub2>生 产 技 术</sub2><link2>../jianjie/shengchanjishu.jsp</link2>
<sub3>荣 誉 证 书</sub3><link3>../comm/index.jsp</link3>
</websub>
</page>

这个common.xml中重复的节点是动态的,单独的节点是静态的,研究了很久,也找了很多资料,
现在把心得给大家分享一下o(∩_∩)o…

首先们建立这么一个common.jsp
<%@ page language="java" contentType="text/html; charset=GBK" pageEncoding="GBK"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>Insert title here</title>
</head>
<body>
<script language="javascript">
var i=0;
function addFile() {
i++;
currRow=conditionTable.insertRow();
cellc=currRow.insertCell();
cellcContext= '<input type="text" NAME="lanmu">栏目:输入格式为:(首页@index.jsp 冷漠大神@Iloveyou.jsp)<br>';
cellc.innerHTML=cellcContext;
}
function addwarning() {
i++;
currRow=conditionTable.insertRow();
cellc=currRow.insertCell();
cellcContext= '<input type="text" NAME="warning">warning:输入格式为:(首页@index.jsp 冷漠大神@Iloveyou.jsp)<br>';
cellc.innerHTML=cellcContext;
}
function addyouq() {
i++;
currRow=conditionTable.insertRow();
cellc=currRow.insertCell();
cellcContext= '<input type="text" NAME="youq">添加友情连接:输入格式为:(冷漠大神@http://qiulovehack.skpay.net)<br>';
cellc.innerHTML=cellcContext;
}
function addwebsub() {
i++;
currRow=conditionTable.insertRow();
cellc=currRow.insertCell();
cellcContext= '<input type="text" NAME="websub">添加websub:输入格式为:(药品招商@../comm/index.jsp)<br>';
cellc.innerHTML=cellcContext;
}
</script>
<form action="common2.jsp" name="form" method="post">
<a href="javascript:void(addFile());" >添加栏目</a>
<a href="javascript:void(addwarning());" >添加warning</a>
<a href="javascript:void(addyouq());" >添加友情连接</a>
<a href="javascript:void(addwebsub());" >添加websub</a>
<div id="fileDiv" style='height:150px;top:150px;left:0px;overflow-x:auto;overflow-y:auto ;border-style:outset;border-width:1pt;border-color: black;'>
<table id=conditionTable border=1 >
</table>
</div>
toppage_advt<input type="text" name="toppage_advt">
web_head<input type="text" name="web_head">
<hr width="500" align="left">
foot_company<input type="text" name="foot_company">
foot_tel<input type="text" name="foot_tel">
foot_email<input type="text" name="foot_email">
foot_copyright<input type="text" name="foot_copyright">
<hr width="500" align="left">
bgcolor<input type="text" name="bgcolor">
background<input type="text" name="background">
<input type="submit" value="生成common.xml文件"/>
</form>
</body>
</html>
这个common.jsp中有javaScript因为要动态的往xml里添加节点嘛,所以必须得用到javaScript啦,form表单会提交到common2.jsp

common2.jsp
<%@page contentType="text/html"%>
<%@page pageEncoding="gbk"%>
<%@ page import="cn.CCmingzhou.jsp.Chinese,test.common,java.util.*" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
Chinese qiu=new Chinese();
String lanmu2[] = request.getParameterValues("lanmu");
String warning2[]=request.getParameterValues("warning");
String youq2[]=request.getParameterValues("youq");
String websub2[]=request.getParameterValues("websub");
String toppage_advt=request.getParameter("toppage_advt");
String web_head=request.getParameter("web_head");
String foot_company=request.getParameter("foot_company");
String foot_tel=request.getParameter("foot_tel");
String foot_email=request.getParameter("foot_email");
String foot_copyright=request.getParameter("foot_copyright");
String bgcolor=request.getParameter("bgcolor");
String background=request.getParameter("background");

List lanmu=new ArrayList();
for(int i=0;i<lanmu2.length;i++)
{
lanmu.add(qiu.getGBK(lanmu2[i]));
out.println()
}
List warning=new ArrayList();
for(int i=0;i<warning2.length;i++)
{
warning.add(qiu.getGBK(warning2[i]));
}
List youq=new ArrayList();
for(int i=0;i<youq2.length;i++)
{
youq.add(qiu.getGBK(youq2[i]));
}
List websub=new ArrayList();
for(int i=0;i<websub2.length;i++)
{
websub.add(qiu.getGBK(websub2[i]));
}
common c=new common();
c.createCommon(lanmu,warning,toppage_advt,qiu.getGBK(web_head),qiu.getGBK(foot_company),qiu.getGBK(foot_tel),qiu.getGBK(foot_email),qiu.getGBK(foot_copyright),bgcolor,background,youq,websub);

%>
</body>
</html>
cn.CCmingzhou.jsp.Chinese这个类是处理中文
test.common这个类就是重点的啦,我们就靠它去实现生成common.xml文件了

common.java
/*
* common.java
*
* Created on 2007年11月6日, 上午10:02
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/

package test;
import java.util.ArrayList;
import org.dom4j.*;
import org.dom4j.io.*;
import java.io.*;
import java.util.Iterator;
import java.util.List;

/**
*冷漠大神 qq 361619004
* @author Angel
*/
public class common {

/** Creates a new instance of common */
public common() {
}

/**
*创建common.xml文件
*
*/
public void createCommon(List lanmu,List warning,
String toppage_advt,String web_head,
String foot_company,String foot_tel, String foot_email,String foot_copyright,
String bgcolor,String background,
List friendship,List websub)
{

String path=null;
path=this.getClass().getResource("/").toString();
path=path.substring(6,path.length()-8)+"common.xml";
//System.out.println("love "+path);

Document document=DocumentHelper.createDocument();
Element page=document.addElement("page");
Element item=page.addElement("item");
for(int i=0;i<lanmu.size();i++)
{
String qian="";
String hou="";
String zong=(String)lanmu.get(i);
int zz=zong.indexOf("@");
qian=zong.substring(0,zz);
hou=zong.substring(zz+1);
Element lanmu2=item.addElement("lanmu");
Element module=lanmu2.addElement("module");
module.setText(qian);
Element superlink=lanmu2.addElement("superlink");
superlink.setText(hou);
}
for(int i=0;i<warning.size();i++)
{
String qian="";
String hou="";
String zong=(String)warning.get(i);
int zz=zong.indexOf("@");
qian=zong.substring(0,zz);
hou=zong.substring(zz+1);
Element lanmu2=item.addElement("warning");
Element module=lanmu2.addElement("manual");
module.setText(qian);
Element superlink=lanmu2.addElement("link");
superlink.setText(hou);
}
Element toppage=page.addElement("toppage");
Element advt=toppage.addElement("advt");
advt.setText(toppage_advt);

Element webhead=page.addElement("webhead");
Element title=webhead.addElement("title");
title.setText(web_head);

Element foot=page.addElement("webfoot");
Element company=foot.addElement("company");
company.setText(foot_company);
Element tel=foot.addElement("tel");
tel.setText(foot_tel);
Element email=foot.addElement("email");
email.setText(foot_email);
Element copyright=foot.addElement("copyright");
copyright.setText(foot_copyright);

Element bg=page.addElement("bgcolor");
Element bgv=bg.addElement("value");
bgv.setText(bgcolor);
Element back=page.addElement("background");
Element backv=back.addElement("value");
backv.setText(background);

Element youq=page.addElement("friendship");
for(int i=0;i<friendship.size();i++)
{
int s=i+1;
Element linkto=youq.addElement("linkto"+s);
String qian="";
String hou="";
String zong=(String)friendship.get(i);
int zz=zong.indexOf("@");
qian=zong.substring(0,zz);
hou=zong.substring(zz+1);
Element home=linkto.addElement("home");
home.setText(qian);
Element homepage=linkto.addElement("homepage");
homepage.setText(hou);
}
Element sub=page.addElement("websub");
for(int i=0;i<websub.size();i++)
{
int aa=i+1;
String qian="";
String hou="";
String zong=(String)websub.get(i);
int zz=zong.indexOf("@");
qian=zong.substring(0,zz);
hou=zong.substring(zz+1);
Element sub2=sub.addElement("sub"+aa);
sub2.setText(qian);
Element link=sub.addElement("link"+aa);
link.setText(hou);
}
/*
StringWriter sw = new StringWriter();
XMLWriter writer = null;
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("GBK");
try {
writer = new XMLWriter(format);
writer.setWriter(sw);
writer.write(document);
} catch (Exception ex) {
ex.printStackTrace();
}

*/
try{
XMLWriter output = new XMLWriter(
new FileWriter( new File(path)));
output.write( document );
output.close();
//System.out.println(path);
}
catch(IOException e){System.out.println(e.getMessage());}

}

public static void main(String args[])
{
List lanmu=new ArrayList();
lanmu.add("首页@index.jsp");
lanmu.add("新闻中心@news.jsp");
lanmu.add("图片管理@images.jsp");
lanmu.add("体育频道@tiyu.jsp");
lanmu.add("帮助@help.jsp");
lanmu.add("新闻@love.jsp");

List warning=new ArrayList();
warning.add("服务声明@fuwu.jsp");
warning.add("联系我们@wangrunxin.jsp");
warning.add("更多友情连接@lianjie.jsp");

String toppage_advt="images/wanjie_banner.gif";
String web_head="中铁十三局集团第五工程有限公司财务部";

String foot_company="中铁十三局集团第五工程有限公司财务部";
String foot_tel="电话:传真:邮编:130033 ";
String foot_email="wangrunxin@hotmail.com";
String foot_copyright="地址:长春市 吉ICP备05002347号";

String bgcolor="#ffffff";;
String background="#ffffff";

List youq=new ArrayList();
youq.add("银河公司@http://www.yhweb.com");
youq.add("大连IT人才网@http://www.dlithr.com");
youq.add("中华英才网@http://www.chinahr.com");

List websub=new ArrayList();
websub.add("药品招商@../comm/index.jsp");
websub.add("生产技术@../jianjie/shengchanjishu.jsp");
websub.add("荣誉证书@../comm/index.jsp");

new common().createCommon(lanmu,warning,toppage_advt,web_head,foot_company,foot_tel,foot_email,foot_copyright,bgcolor,background,youq,websub);
}

}

这个执行之后会把common.xml生成到WEB-INF/目录,大家如果想看看结果的话那就自己把这些东西都考过去自己运行一下吧 o(∩_∩)o…

作者:高秋
网名:冷漠大神
Qq:361619004
Mail:hackq@163.com
Time:2007年11月9日星期五
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: