lizongbo 的 编程学习

http://618119.com

BlogJava 首页 新随笔 联系 聚合 管理
  23 Posts :: 1 Stories :: 78 Comments :: 0 Trackbacks

由于原有模板是以.htm方式存在的,
在转换成jsp方式时,对其中很多通用的代码,可以通过替换的方式直接转换为jstl语法的.
步骤如下:

1.首先将所有的htm文件名替换成jsp,
在命令行下运行 rename *.htm *.jsp即可.

2.将bbs\forumdata\cache\style_1.php中的css变量TABLEWIDTH等,
替换成类似${crtStyles['TABLEWIDTH']}的jstl语法.
全部只能手工替换

3.将*.jsp中的{lang forum_favorite}等替换成类似 <fmt:message key="faq" bundle="${forum_favorite}"/>
使用正则表达式进行替换:
editplus中的 查找内容为: {lang (.+)},替换内容为:<fmt:message key="faq" bundle="${\1}"/>
Jbuilder中的查找内容为 \{lang (.+)\},Pattern为:Regular Expressions,
替换内容为:(暂时未写出来,打算写程序进行替换操作)

用java程序替换的核心代码为:

 //替换样式变量
             content = content.replaceAll("FORMHASH", "formhash");
             //替换样式变量 ${crtStyle['TABLEWIDTH']}
       content = content.replaceAll("\\{([A-Z0-9]+)\\}", "\\${crtStyle\\['$1'\\]}");
             //替换国际化定义
             //content = content.replaceAll("\\{lang (.+?)\\}",
             //                             "<fmt:message key=\"$1\" bundle=\"\\$\\{templates\\}\"/>");
             //对标签属性里的值暂时不替换
             content = content.replaceAll("([^\"])\\{lang (.+?)\\}",
       "$1<fmt:message key=\"$2\" bundle=\"\\$\\{templates\\}\"/>");

             //替换单层的属性访问
             content = content.replaceAll("\\$([a-z]+?)\\[([a-z]+?)\\]",
                                   "\\$\\{$1\\['$2'\\]\\}");
             //替换标题部分的声明
             content = content.replaceAll("\\{template header\\}",
                                   "<%@page pageEncoding=\"UTF-8\" " +
                                   "contentType=\"text/html;" +
                                   " charset=UTF-8\"%>\n"
                                   + "<%@include file=\"/WEB-INF/" +
                                   "inc/taglibs.jspf\"%>\n" +
                                   "<jsp:include flush=\"true\" " +
                                   "page=\"header.jsp\"/>\n");
             ////替换底部部分的声明
             content = content.replaceAll("\\{template footer\\}",
                                   "\n<jsp:include flush=\"true\" " +
                                   "page=\"footer.jsp\"/>");

             //替换其它引用声明
             content = content.replaceAll("\\{template (.+?)\\}",
                                   "\n<jsp:include flush=\"true\" " +
                                   "page=\"$1.jsp\"/>");
             //替换url定义
       content = content.replaceAll("\\$indexname", "\\${settings.indexname}");
             //替换网站名字
             content = content.replaceAll("\\$bbname", "\\${settings.bbname}");
             //替换导航标签
             content = content.replaceAll("\\$navigation", "\\${navigation}");
             //替换一些变量
             //content = content.replaceAll("\\$pid", "\\${pid}");
       content = content.replaceAll("\\$([a-z_]+)(\"|<|\\))", "\\${$1}$2");

posted on 2007-08-29 09:42 lizongbo 的编程学习 阅读(740) 评论(1)  编辑  收藏 所属分类: java discuz