现在呀,越来越多的网站要求做成多种语言,并且,大家好像都知道了html比动态的运行快,所以,也倾向自动生成静态页面这个功能,我就纳闷了,消息怎么传的那么快呀!好吧,说说多语言网站生成静态页面时候的主要瓶颈在哪些方面吧:
1.首先是列表类型的文章分页的生成
这个吧,肯定需要有自己的一套规则,这次我生成静态页面时候是这样规定的:
列表页:第n页:list_1_n.htm(其实,那个_1暂时没用,是预留的)
详细页:和list_1_n.html同级目录下建一个文件夹,文件夹命名为"n",也就是说,第n页的内容放到文件夹为"n"的文件夹内,文件名称命名为:0.html,1.html...一页下面用文章序号显示.(当然,我看到好多网站都用时间的,也可以)
2.生成网站的连接是个重点
开始的时候,其实忽略了这个问题,一直觉得,怎么生成内容并显示出来是最重要的,但是后来才发现,最烦人的地方时链接怎么放.我用的办法是在对应的实体类加上一个属性 String
href; 每次生成的时候,把href放进去.
考虑好了这两大方面,我觉得,整个网站就应该架构起来了,接下来,说一下生成静态页面的实现.
用的是freemarker作模板:
3.1 步骤:
3.1.1 myeclipse中新建一个webproject,引入struts2,最后目录结构:
3.1.2 工具类util包里面,是实现生成静态页面的类:
CreateHtml.java:
package com.guanghua.util;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Locale;
import java.util.Map;
import org.apache.struts2.ServletActionContext;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
public class CreateHtml {/*** @author xingyunpi* @param ftl 模板文件* @param htmlName 生成html文件* @param map * @param relaPath* @throws IOException* @throws TemplateException*/public void init (String ftl,String htmlName,Map map,String relaPath) throws IOException,TemplateException{Configuration cfg=new Configuration();cfg.setServletContextForTemplateLoading(ServletActionContext.getServletContext(), "/");cfg.setEncoding(Locale.getDefault(), "UTF-8");Template template=cfg.getTemplate(ftl);template.setEncoding("UTF-8");String path=ServletActionContext.getServletContext().getRealPath("/");File fileName=new File(path+htmlName);Writer out=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName),"UTF-8"));template.process(map, out);out.flush();out.close();}
}
3.1.3 测试类:
HtmlTest.java
package com.guanghua.action;import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;import com.guanghua.util.CreateHtml;
import com.opensymphony.xwork2.ActionSupport;import freemarker.template.TemplateException;
public class HtmlTest extends ActionSupport{private String msg;public String getMsg() {return msg;}public void setMsg(String msg) {this.msg = msg;}public String execute(){CreateHtml createHtml=new CreateHtml();List<Person> persons=new ArrayList<Person>();for(int i=0;i<5;i++){Person p=new Person();p.setAge(24);p.setName("xingyupi");persons.add(p);}Map<String,List<Person>> map=new HashMap<String, List<Person>>();map.put("personlist", persons);String htmlName="personlist.html";String ftl="person.ftl";String relaPath="index.jsp";try {createHtml.init(ftl, htmlName, map, relaPath);} catch (IOException e) {e.printStackTrace();} catch (TemplateException e) {e.printStackTrace();}this.msg=htmlName;return SUCCESS;}
}
3.1.4 实体类:
Person.java
package com.guanghua.action;public class Person {String name;int age;public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}}
3.1.5 模板页面:
person.ftl:
<html><head><title></title><meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="this is my page"><meta http-equiv="content-type" content="text/html; charset=UTF-8"><!--<link rel="stylesheet" type="text/css" href="./styles.css">--></head><body>
<table style="text-align:center;FONT-SIZE: 11pt; WIDTH: 600px; FONT-FAMILY: 宋体; BORDER-COLLAPSE: collapse" borderColor=#3399ff cellSpacing=0 cellPadding=0 align=center border=1> <tr> <td><b>name</b></td> <td><b>age</b></td> </tr> <#list personlist as person> <tr> <td>${person.name}</td> <td>${person.age}</td></tr> </#list></body>
</html>
3.1.6 struts.xml配置
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<constant name="struts.devMode" value="true" /><constant name="struts.i18n.encoding" value="UTF-8" />
<package namespace="/" name="default" extends="struts-default">
<action name="index" class="com.guanghua.action.HtmlTest"><result type="redirect">/${msg}</result>
</action>
</package>
</struts>
3.1.7 当然,还要引入freemarker.jar
3.2 最后附上freemarker.jar下载和项目下载:
freemarker.jar
CreateHtml.rar