日期:2014-05-18  浏览次数:20796 次

关于静态页面生成问题。简单。
据说有个freemarker的这个东西可以生成静态页面。
请问有没有例子,或者用法? 给个连接就行。。谢谢各位了。

------解决方案--------------------
不难,一般大型网站用这个。给你讲一下原理。
正确的说,freemarker只是完成了其中重要的一步。
生成静态的页面要有两个重要的参数,一个是生成页面的模板(一个简单的页面,将要显示的数据位置用freemarker标签站位),生成页面的位置(物理位置,如c:\tomtcat\...)。
使用freemarker是因为freemarker有一个方法,整个方法的参数有两个,一个是模板的地址,一个是要显示的数据(类型为Map),这个方法会返回一个字符串,这是已经生成的页面的内容,你再用文件流以生成页面的位置为参数,将字符串写入文件,这样,所有的操作就完成了。
------解决方案--------------------
import java.io.*;
import java.net.*;

public class Tools {
final static Object lock = new Object();

public static void makeHtml(String page, String filePath) {
makeHtml(page, filePath, "UTF-8");
}

public static void makeHtml(String page, String filePath, String chartset) {
synchronized (lock) {
HttpURLConnection huc = null;
BufferedReader br = null;
BufferedWriter bw = null;
try {
huc = (HttpURLConnection) new URL(page).openConnection();
System.setProperty("sun.net.client.defaultConnectTimeout",
"30000");
System
.setProperty("sun.net.client.defaultReadTimeout",
"30000");
huc.connect();
InputStream stream = huc.getInputStream();
bw = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(filePath), chartset));
br = new BufferedReader(new InputStreamReader(stream, chartset));
String line;
while ((line = br.readLine()) != null) {
if (line.trim().length() > 0) {
bw.write(line);
bw.newLine();
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
br.close();
bw.close();
huc.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}

其中:page是要生成静态页面的网页地址
filePath是生成静态页面服务器的物理地址

自己试一下吧,很好用的
------解决方案--------------------
顶楼上的 拿来试试看看
------解决方案--------------------
探讨
import java.io.*;
import java.net.*;

public class Tools {
final static Object lock = new Object();

public static void makeHtml(String page, String filePath) {
makeHtml(page, filePath, "UTF-8");
}

public static void makeHtml(String page, String filePath, String chartset) {
synchronized (lock) {
HttpURLConnection huc = null;
BufferedReader br = null;
BufferedWriter bw =…

------解决方案--------------------
"d:/dzh/ddd.html"改成"d://dzh//ddd.html"试试
------解决方案--------------------
例子:
使用FreeMarker生成Html静态文件(实例)
http://www.jscud.com/srun/news/viewhtml/2_2005_4/36.htm

FreeMarker官网也有例子
http://www.freemarker.org/index.html