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

 

 

 

   
   
前两天项目经理给我个问题让我玩,在jsp程序中动态生成xml网站配置程序,大概的模样是
Common.xml



 
    首  页
    ../cn/index.jsp
 

 
    人才信息
    ../personal/more.jsp?type=1
 

 
    招聘信息
    ../enterprise/more.jsp?type=1
 

 
    企业风采
    ../qiye/more.jsp?type=1
 

 
    新闻中心
    ../index/index.jsp
 

 
    建议留言
    ../sony/index.jsp
 

 
    使用说明
    ../jianjie/index.jsp
 

    
 
    服务声明
    ../cn/index.jsp
 

 
    | 联络我们
    http://wangrunxin
 

  
    | 关于我们
    ../personal/more.jsp?type=1
 

 
    | 法律声明
    ../enterprise/more.jsp?type=1
 
 
 
    | 帮助
    ../index/index.jsp
 
 
  
    | 更多友情链接
    ../index/index.jsp
 
 


    images/wanjie_banner.gif


    中铁十三局集团第五工程有限公司财务部


  中铁十三局集团第五工程有限公司财务部
  电话:传真:邮编:130033
  [email protected]
  地址:长春市 吉ICP备05002347号


 #ffffff


 #ffffff


 
    银河公司
    http://www.yhweb.com
 

 
    大连IT人才网
    http://www.dlithr.com
 

 
    中华英才网
    http://www.chinahr。com
 

 
    招聘网
    http://www.51job.com
 



药 品 招 商../comm/index.jsp
生 产 技 术../jianjie/shengchanjishu.jsp
荣 誉 证 书../comm/index.jsp

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


首先们建立这么一个common.jsp
<%@ page language="java" contentType="text/html; charset=GBK" pageEncoding="GBK"%>




Insert title here




添加栏目
添加warning
添加友情连接
添加websub




toppage_advt
web_head


foot_company
foot_tel
foot_email
foot_copyright


bgcolor
background




这个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.*" %>
   "http://www.w3.org/TR/html4/loose.dtd">


   
       
        JSP Page
   
   
    <%
       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       {
           lanmu.add(qiu.getGBK(lanmu2[i]));
           out.println()
       }
       List warning=new ArrayList();
       for(int i=0;i       {
           warning.add(qiu.getGBK(warning2[i]));
       }
       List youq=new ArrayList();
       for(int i=0;i       {
           youq.add(qiu.getGBK(youq2[i]));
       }
       List websub=new ArrayList();
       for(int i=0;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);
      
 %>
   

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        {
            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        {
            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        {
            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        {
            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日星期五 

你可能感兴趣的:(javascript,jsp,string,module,email,list)