我希望我的Java应用程序在文件中编写HTML代码.现在,我正在使用java.io.BufferedWriter类对HTML标签进行硬编码.例如:
BufferedWriter bw = new BufferedWriter(new FileWriter(file));
bw.write("<html><head><title>New Page</title></head><body><p>This is Body</p></body></html>");
bw.close();
Run Code Online (Sandbox Code Playgroud)
有没有更简单的方法来做到这一点,因为我必须创建表格,它变得非常不方便?
Mar*_*vic 43
如果你想自己做,不使用任何外部库,一个干净的方法是创建一个template.html包含所有静态内容的文件,例如:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>$title</title>
</head>
<body>$body
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
$tag为任何动态内容添加标记,然后执行以下操作:
File htmlTemplateFile = new File("path/template.html");
String htmlString = FileUtils.readFileToString(htmlTemplateFile);
String title = "New Page";
String body = "This is Body";
htmlString = htmlString.replace("$title", title);
htmlString = htmlString.replace("$body", body);
File newHtmlFile = new File("path/new.html");
FileUtils.writeStringToFile(newHtmlFile, htmlString);
Run Code Online (Sandbox Code Playgroud)
注意:为简单起见,我使用了org.apache.commons.io.FileUtils.
Mig*_*boa 11
几个月前,我遇到了同样的问题,我找到的每个库都为我的最终目标提供了太多的功能和复杂性.所以我最终开发了自己的库 - HtmlFlow - 它提供了一个非常简单直观的API,允许我以流畅的方式编写HTML.在这里查看:https://github.com/fmcarvalho/HtmlFlow(它还支持动态绑定到HTML元素)
以下是将Task对象的属性绑定到HTML元素的示例.考虑一个TaskJava类具有三个属性:Title,Description和Priority,然后我们就可以生成HTML文档的Task按以下方式对象:
import htmlflow.HtmlView;
import model.Priority;
import model.Task;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
public class App {
private static HtmlView<Task> taskDetailsView(){
HtmlView<Task> taskView = new HtmlView<>();
taskView
.head()
.title("Task Details")
.linkCss("https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css");
taskView
.body().classAttr("container")
.heading(1, "Task Details")
.hr()
.div()
.text("Title: ").text(Task::getTitle)
.br()
.text("Description: ").text(Task::getDescription)
.br()
.text("Priority: ").text(Task::getPriority);
return taskView;
}
public static void main(String [] args) throws IOException{
HtmlView<Task> taskView = taskDetailsView();
Task task = new Task("Special dinner", "Have dinner with someone!", Priority.Normal);
try(PrintStream out = new PrintStream(new FileOutputStream("Task.html"))){
taskView.setPrintStream(out).write(task);
Desktop.getDesktop().browse(URI.create("Task.html"));
}
}
}
Run Code Online (Sandbox Code Playgroud)
jsoup的示例代码: -
Document doc = Jsoup.parse("<html></html>");
doc.body().addClass("body-styles-cls");
doc.body().appendElement("div");
System.out.println(doc.toString());
Run Code Online (Sandbox Code Playgroud)
版画
<html>
<head></head>
<body class=" body-styles-cls">
<div></div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
wffweb的示例代码: -
Html html = new Html(null) {{
new Head(this);
new Body(this,
new ClassAttribute("body-styles-cls"));
}};
Body body = TagRepository.findOneTagAssignableToTag(Body.class, html);
body.appendChild(new Div(null));
System.out.println(html.toHtmlString());
//directly writes to file
html.toOutputStream(new FileOutputStream("/home/user/filepath/filename.html"), "UTF-8");
Run Code Online (Sandbox Code Playgroud)
打印(缩小格式): -
<html>
<head></head>
<body class="body-styles-cls">
<div></div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)