页面静态化技术 : jsp和thymeleaf及其他模板页面静态化工具类

import java.io.File;
import java.io.FileWriter;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;

/**
 * 页面静态化
 * @author guyinyihun
 *
 */
public class StaticizeUtil {
	
	public static void jspIndexToStatic() {
		try {
			HttpClient client = new HttpClient();
			GetMethod getMethod = new GetMethod("http://127.0.0.1:9090/"); //首页访问路径
			String path=StaticizeUtil.class.getClassLoader().getResource("").getPath();
			String path2 = path.replace("/target/classes/", "/src/main/webapp/static/");
			client.executeMethod(getMethod);
			File file = new File(path2+"index.html");//存到webapp的static目录下
			FileWriter writer = new FileWriter(file);
			writer.write(getMethod.getResponseBodyAsString());
			writer.flush();
			System.err.println("首页静态化已完成...");
		} catch (Exception e) {
			e.printStackTrace();
		} 
	}
	
	public static void thyIndexToStatic() {
		try {
			HttpClient client = new HttpClient();
			GetMethod getMethod = new GetMethod("http://127.0.0.1:9004/");//首页访问路径
			String path=StaticizeUtil.class.getClassLoader().getResource("").getPath();
			String path2 = path.replace("/target/classes/", "/src/main/resources/templates/");
			client.executeMethod(getMethod);
			File file = new File(path2+"index.html");//存到/src/main/resources/templates 下
			FileWriter writer = new FileWriter(file);
			writer.write(getMethod.getResponseBodyAsString());
			writer.flush();
			System.err.println("首页静态化已完成...");
		} catch (Exception e) {
			e.printStackTrace();
		} 
	}
}

 

需引入依赖

 

<dependency>
			<groupId>commons-httpclient</groupId>
			<artifactId>commons-httpclient</artifactId>
			<version>3.1-rc1</version>
		</dependency>

 

 

 

 

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值