jsp+javaScript結合dom4j動態生成網站配置文件

 

 

 

   
   
前兩天項目經理給我個問題讓我玩,在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>[email protected]</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="[email protected]";
        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:[email protected]
Time:2007年11月9日星期五 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章