「生成 JSP 文件」通常指靜態頁生成或代碼生成器場景:讀取數據庫元數據或業務數據,按模板輸出 `.jsp` 文件到項目目錄。
// 1. 查數據
List tables = jdbcTemplate.query("SELECT ...", new TableMetaRowMapper());
// 2. 渲染模板
Configuration cfg = new Configuration(Configuration.VERSION_2_3_31);
cfg.setClassForTemplateLoading(this.getClass(), "/templates");
Template template = cfg.getTemplate("jsp_template.ftl");
Map model = new HashMap<>();
model.put("tables", tables);
StringWriter out = new StringWriter();
template.process(model, out);
// 3. 寫文件
Files.write(Paths.get("src/main/webapp/generated/list.jsp"), out.toString().getBytes(StandardCharsets.UTF_8));
Java 讀取 MySQL 生成 JSP 文件,FreeMarker 模板引擎,靜態頁生成,代碼生成器工程化實踐。