|
|
@@ -16,15 +16,66 @@
|
|
|
|
|
|
package com.jfinal.ext.render;
|
|
|
|
|
|
-import com.jfinal.render.Render;
|
|
|
+import com.jfinal.render.RenderException;
|
|
|
+import com.jfinal.render.TemplateRender;
|
|
|
+import com.jfinal.template.Template;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.OutputStream;
|
|
|
+import java.util.Enumeration;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
|
|
|
/**
|
|
|
* 生成静态 html
|
|
|
*/
|
|
|
-public class StaticHtmlRender extends Render {
|
|
|
-
|
|
|
+public class StaticHtmlRender extends TemplateRender {
|
|
|
+ protected File file;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param view 模板路径
|
|
|
+ * @param file 生成的静态html文件路径
|
|
|
+ */
|
|
|
+ public StaticHtmlRender(String view, File file) {
|
|
|
+ super(view);
|
|
|
+ this.file = file;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
public void render() {
|
|
|
- throw new RuntimeException("Not finish!!!");
|
|
|
+ response.setContentType(getContentType());
|
|
|
+
|
|
|
+ Map<Object, Object> data = new HashMap<>();
|
|
|
+ for (Enumeration<String> attrs = request.getAttributeNames(); attrs.hasMoreElements();) {
|
|
|
+ String attrName = attrs.nextElement();
|
|
|
+ data.put(attrName, request.getAttribute(attrName));
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ Template template = engine.getTemplate(view);
|
|
|
+
|
|
|
+ OutputStream os = response.getOutputStream();
|
|
|
+ template.render(data, os);
|
|
|
+ os.flush();
|
|
|
+
|
|
|
+ File parentFile = file.getParentFile();
|
|
|
+ if (! parentFile.exists()) parentFile.mkdirs();
|
|
|
+ template.render(data, file);
|
|
|
+
|
|
|
+ } catch (RuntimeException e) { // 捕获 ByteWriter.close() 抛出的 RuntimeException
|
|
|
+ Throwable cause = e.getCause();
|
|
|
+ if (cause instanceof IOException) { // ClientAbortException、EofException 直接或间接继承自 IOException
|
|
|
+ String name = cause.getClass().getSimpleName();
|
|
|
+ if ("ClientAbortException".equals(name) || "EofException".equals(name)) {
|
|
|
+ return ;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ throw e;
|
|
|
+ } catch (IOException e) {
|
|
|
+ throw new RenderException(e);
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|