1 定义DatabaseSource
public class DatabaseSource implements ISource { private String tableName; private String keyFieldName; private String contentFieldName; private String keyValue; public DatabaseSource(String tableName, String keyFieldName, String contentFieldName, String keyValue) { if (StrKit.isBlank(tableName)) { throw new IllegalArgumentException("tableName can not be blank"); } if (StrKit.isBlank(keyFieldName)) { throw new IllegalArgumentException("keyFieldName can not be blank"); } if (StrKit.isBlank(contentFieldName)) { throw new IllegalArgumentException("contentFieldName can not be blank"); } if (StrKit.isBlank(keyValue)) { throw new IllegalArgumentException("keyValue can not be blank"); } this.tableName = tableName; this.keyFieldName = keyFieldName; this.contentFieldName = contentFieldName; this.keyValue = keyValue; } @Override public boolean isModified() { return false; } @Override public String getKey() { return keyValue; } @Override public StringBuilder getContent() { Record record = Db.findFirst("SELECT * FROM " + tableName + " WHERE " + keyFieldName + "=? LIMIT 1", keyValue); return new StringBuilder(record.getStr(contentFieldName)); } @Override public String getEncoding() { return EngineConfig.DEFAULT_ENCODING; } public void setTableName(String tableName) { this.tableName = tableName; } public void setKeyFieldName(String keyFieldName) { this.keyFieldName = keyFieldName; } public String toString() { StringBuilder sb = new StringBuilder(); sb.append("Table Name: ").append(tableName).append("\n"); sb.append("Key Field Name: ").append(keyFieldName).append("\n"); sb.append("Content Field Name: ").append(contentFieldName).append("\n"); sb.append("Key Value: ").append(keyValue).append("\n"); return sb.toString(); } }
2 定义DatabaseSourceFactory
public class DatabaseSourceFactory implements ISourceFactory { private String tableName; private String keyFieldName; private String contentFieldName; public DatabaseSourceFactory(String tableName, String keyFieldName, String contentFieldName) { this.tableName = tableName; this.keyFieldName = keyFieldName; this.contentFieldName = contentFieldName; } @Override public ISource getSource(String baseTemplatePath, String fileName, String encoding) { return new DatabaseSource(tableName, keyFieldName, contentFieldName, fileName); } }
3 测试请求
public void test() { Engine engine = Engine.use("database"); if(null == engine) { engine = Engine.create("database"); engine.setBaseTemplatePath(null); engine.setSourceFactory(new DatabaseSourceFactory("t_template", "template_key", "template_content")); } engine.setDevMode(true); //com.jfinal.template.Template template = engine.getTemplate(new DatabaseSource("t_template", "template_key", "template_content", "/index")); com.jfinal.template.Template template = engine.getTemplate("/index"); String result = template.renderToString(Kv.by("name", "Adam")); renderText(result); }
4 测试数据模板/layout
#define layout() <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"> <title>佰亿互联管理系统 - tbynet.com</title> <link rel="stylesheet" type="text/css" href="/UI-INF/layui/css/layui.css"> <link rel="stylesheet" type="text/css" href="/UI-INF/admin/css/admin.css"> </head> <body> #@content() </body> </html> #end
5 测试数据模板/index
#include("/layout") #@layout() #define content() my name is #(name)! #end
(已解决)解析后只显示结果:#@layout
(已解决)现在报异常了:Template function not defined: layout,是不是我思路不对?
项目:JFinal