您的位置:首页 > 编程语言 > Java开发

Java 常用工具类(16) : jsp和thymeleaf及其他模板页面静态化工具类

2018-04-03 10:21 666 查看
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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐