enjoy 3.4
This commit is contained in:
parent
2ed806c296
commit
ef7b0da917
@ -162,6 +162,13 @@ public class Engine {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Get template by string content and do not cache the template
|
* Get template by string content and do not cache the template
|
||||||
|
*/
|
||||||
|
public Template getTemplateByString(String content) {
|
||||||
|
return getTemplateByString(content, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get template by string content
|
||||||
*
|
*
|
||||||
* 重要:StringSource 中的 key = HashKit.md5(content),也即 key
|
* 重要:StringSource 中的 key = HashKit.md5(content),也即 key
|
||||||
* 与 content 有紧密的对应关系,当 content 发生变化时 key 值也相应变化
|
* 与 content 有紧密的对应关系,当 content 发生变化时 key 值也相应变化
|
||||||
@ -170,13 +177,7 @@ public class Engine {
|
|||||||
*
|
*
|
||||||
* 当 getTemplateByString(String, boolean) 中的 String 参数的
|
* 当 getTemplateByString(String, boolean) 中的 String 参数的
|
||||||
* 数量可控并且确定时,才可对其使用缓存
|
* 数量可控并且确定时,才可对其使用缓存
|
||||||
*/
|
*
|
||||||
public Template getTemplateByString(String content) {
|
|
||||||
return getTemplateByString(content, false);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get template by string content
|
|
||||||
* @param content 模板内容
|
* @param content 模板内容
|
||||||
* @param cache true 则缓存 Template,否则不缓存
|
* @param cache true 则缓存 Template,否则不缓存
|
||||||
*/
|
*/
|
||||||
|
@ -16,6 +16,10 @@
|
|||||||
|
|
||||||
package com.jfinal.template;
|
package com.jfinal.template;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.io.Writer;
|
import java.io.Writer;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@ -108,6 +112,32 @@ public class Template {
|
|||||||
return fsw.getBuffer();
|
return fsw.getBuffer();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 渲染到 File 中去
|
||||||
|
* 适用于代码生成器类似应用场景
|
||||||
|
*/
|
||||||
|
public void render(Map<?, ?> data, File file) {
|
||||||
|
FileOutputStream fos = null;
|
||||||
|
try {
|
||||||
|
fos = new FileOutputStream(file);
|
||||||
|
render(data, fos);
|
||||||
|
} catch (FileNotFoundException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
} finally {
|
||||||
|
if (fos != null) {
|
||||||
|
try {fos.close();} catch (IOException e) {e.printStackTrace(System.err);}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 渲染到 String fileName 参数所指定的文件中去
|
||||||
|
* 适用于代码生成器类似应用场景
|
||||||
|
*/
|
||||||
|
public void render(Map<?, ?> data, String fileName) {
|
||||||
|
render(data, new File(fileName));
|
||||||
|
}
|
||||||
|
|
||||||
public boolean isModified() {
|
public boolean isModified() {
|
||||||
return env.isSourceListModified();
|
return env.isSourceListModified();
|
||||||
}
|
}
|
||||||
|
@ -23,7 +23,7 @@ import com.jfinal.template.stat.Scope;
|
|||||||
*/
|
*/
|
||||||
public class Id extends Expr {
|
public class Id extends Expr {
|
||||||
|
|
||||||
private String id;
|
private final String id;
|
||||||
|
|
||||||
public Id(String id) {
|
public Id(String id) {
|
||||||
this.id = id;
|
this.id = id;
|
||||||
|
@ -24,8 +24,10 @@ import com.jfinal.template.stat.Scope;
|
|||||||
* Map
|
* Map
|
||||||
*
|
*
|
||||||
* 1:定义 map 常量
|
* 1:定义 map 常量
|
||||||
* {k1:123, k2:"abc", 'k3':true, "k4":[1,2,3], k5:1+2}
|
* {k1:123, k2:"abc", 'k3':true, "k4":[1,2,3], k5:1+2, 1:12, true:"Y", null:"abc"}
|
||||||
* 如上所示,map定义的 key 可以为 String 或者 id 标识符,而右侧的 value 可以是任意的常量与表达式
|
* 如上所示,map定义的 key 可以为 id 标识符或者 String、Integer、Long、Boolean、null
|
||||||
|
* 等常量值 (详见 ExprParser.buildMapEntry(...) 方法),
|
||||||
|
* 右侧的 value 可以是任意的常量与表达式
|
||||||
*
|
*
|
||||||
* 2:取值
|
* 2:取值
|
||||||
* 先将 Map 常量赋值给某个变量: #set(map = {...})
|
* 先将 Map 常量赋值给某个变量: #set(map = {...})
|
||||||
@ -35,6 +37,10 @@ import com.jfinal.template.stat.Scope;
|
|||||||
* map.get("k1")
|
* map.get("k1")
|
||||||
* map.k1
|
* map.k1
|
||||||
*
|
*
|
||||||
|
* 3:不通过中间变量取值
|
||||||
|
* {1:'自买', 2:'跟买'}.get(1)
|
||||||
|
* {1:'自买', 2:'跟买'}[2]
|
||||||
|
*
|
||||||
* 如上所示,当以下标方式取值时,下标参数可以是 string 与 expr,而 expr 求值以后的值必须也为 string类型
|
* 如上所示,当以下标方式取值时,下标参数可以是 string 与 expr,而 expr 求值以后的值必须也为 string类型
|
||||||
* 当用 map.k1 这类 field 字段取值形式时,则是使用 id 标识符,而不是 string 形参数
|
* 当用 map.k1 这类 field 字段取值形式时,则是使用 id 标识符,而不是 string 形参数
|
||||||
*
|
*
|
||||||
|
@ -32,7 +32,7 @@ import com.jfinal.template.stat.Scope;
|
|||||||
*/
|
*/
|
||||||
public class NowDirective extends Directive {
|
public class NowDirective extends Directive {
|
||||||
|
|
||||||
public void setExrpList(ExprList exprList) {
|
public void setExprList(ExprList exprList) {
|
||||||
if (exprList.length() > 1) {
|
if (exprList.length() > 1) {
|
||||||
throw new ParseException("#now directive support one parameter only", location);
|
throw new ParseException("#now directive support one parameter only", location);
|
||||||
}
|
}
|
||||||
|
@ -16,22 +16,29 @@
|
|||||||
|
|
||||||
package com.jfinal.template.ext.directive;
|
package com.jfinal.template.ext.directive;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
import com.jfinal.template.Directive;
|
import com.jfinal.template.Directive;
|
||||||
import com.jfinal.template.Env;
|
import com.jfinal.template.Env;
|
||||||
|
import com.jfinal.template.TemplateException;
|
||||||
import com.jfinal.template.io.Writer;
|
import com.jfinal.template.io.Writer;
|
||||||
import com.jfinal.template.stat.Scope;
|
import com.jfinal.template.stat.Scope;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 输出随机数
|
* 输出 int 型随机数
|
||||||
*/
|
*/
|
||||||
public class RandomDirective extends Directive {
|
public class RandomDirective extends Directive {
|
||||||
|
|
||||||
private java.util.Random random = new java.util.Random();
|
private java.util.Random random = new java.util.Random();
|
||||||
|
|
||||||
public void exec(Env env, Scope scope, Writer writer) {
|
public void exec(Env env, Scope scope, Writer writer) {
|
||||||
write(writer, String.valueOf(random.nextInt()));
|
try {
|
||||||
|
writer.write(random.nextInt());
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new TemplateException(e.getMessage(), location, e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -43,6 +43,14 @@ public class ByteExt {
|
|||||||
public Double toDouble(Byte self) {
|
public Double toDouble(Byte self) {
|
||||||
return self.doubleValue();
|
return self.doubleValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Short toShort(Byte self) {
|
||||||
|
return self.shortValue();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Byte toByte(Byte self) {
|
||||||
|
return self;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -43,6 +43,14 @@ public class DoubleExt {
|
|||||||
public Double toDouble(Double self) {
|
public Double toDouble(Double self) {
|
||||||
return self;
|
return self;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Short toShort(Double self) {
|
||||||
|
return self.shortValue();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Byte toByte(Double self) {
|
||||||
|
return self.byteValue();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -43,6 +43,14 @@ public class FloatExt {
|
|||||||
public Double toDouble(Float self) {
|
public Double toDouble(Float self) {
|
||||||
return self.doubleValue();
|
return self.doubleValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Short toShort(Float self) {
|
||||||
|
return self.shortValue();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Byte toByte(Float self) {
|
||||||
|
return self.byteValue();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -63,6 +63,14 @@ public class IntegerExt {
|
|||||||
public Double toDouble(Integer self) {
|
public Double toDouble(Integer self) {
|
||||||
return self.doubleValue();
|
return self.doubleValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Short toShort(Integer self) {
|
||||||
|
return self.shortValue();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Byte toByte(Integer self) {
|
||||||
|
return self.byteValue();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -43,6 +43,14 @@ public class LongExt {
|
|||||||
public Double toDouble(Long self) {
|
public Double toDouble(Long self) {
|
||||||
return self.doubleValue();
|
return self.doubleValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Short toShort(Long self) {
|
||||||
|
return self.shortValue();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Byte toByte(Long self) {
|
||||||
|
return self.byteValue();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -43,6 +43,14 @@ public class ShortExt {
|
|||||||
public Double toDouble(Short self) {
|
public Double toDouble(Short self) {
|
||||||
return self.doubleValue();
|
return self.doubleValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Short toShort(Short self) {
|
||||||
|
return self;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Byte toByte(Short self) {
|
||||||
|
return self.byteValue();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -79,6 +79,14 @@ public class StringExt {
|
|||||||
public Double toDouble(String self) {
|
public Double toDouble(String self) {
|
||||||
return StrKit.isBlank(self) ? null : Double.parseDouble(self);
|
return StrKit.isBlank(self) ? null : Double.parseDouble(self);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Short toShort(String self) {
|
||||||
|
return StrKit.isBlank(self) ? null : Short.parseShort(self);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Byte toByte(String self) {
|
||||||
|
return StrKit.isBlank(self) ? null : Byte.parseByte(self);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -59,10 +59,9 @@ public class JFinalView extends AbstractTemplateView {
|
|||||||
OutputStream os = response.getOutputStream();
|
OutputStream os = response.getOutputStream();
|
||||||
JFinalViewResolver.engine.getTemplate(getUrl()).render(model, os);
|
JFinalViewResolver.engine.getTemplate(getUrl()).render(model, os);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@SuppressWarnings({"unchecked", "rawtypes", "deprecation"})
|
@SuppressWarnings({"unchecked", "rawtypes", "deprecation"})
|
||||||
class InnerSession extends HashMap<Object, Object> implements HttpSession {
|
public static class InnerSession extends HashMap<Object, Object> implements HttpSession {
|
||||||
|
|
||||||
private static final long serialVersionUID = -8679493647540628009L;
|
private static final long serialVersionUID = -8679493647540628009L;
|
||||||
private HttpSession session;
|
private HttpSession session;
|
||||||
@ -155,6 +154,11 @@ class InnerSession extends HashMap<Object, Object> implements HttpSession {
|
|||||||
public void setMaxInactiveInterval(int maxInactiveInterval) {
|
public void setMaxInactiveInterval(int maxInactiveInterval) {
|
||||||
session.setMaxInactiveInterval(maxInactiveInterval);
|
session.setMaxInactiveInterval(maxInactiveInterval);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String toString() {
|
||||||
|
return session != null ? session.toString() : "null";
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -162,4 +166,3 @@ class InnerSession extends HashMap<Object, Object> implements HttpSession {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -77,6 +77,9 @@ public class FileSource implements ISource {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private String buildFinalFileName(String baseTemplatePath, String fileName) {
|
private String buildFinalFileName(String baseTemplatePath, String fileName) {
|
||||||
|
if (baseTemplatePath == null) {
|
||||||
|
return fileName;
|
||||||
|
}
|
||||||
char firstChar = fileName.charAt(0);
|
char firstChar = fileName.charAt(0);
|
||||||
String finalFileName;
|
String finalFileName;
|
||||||
if (firstChar == '/' || firstChar == '\\') {
|
if (firstChar == '/' || firstChar == '\\') {
|
||||||
|
@ -25,7 +25,7 @@ import com.jfinal.template.io.Writer;
|
|||||||
import com.jfinal.template.stat.Scope;
|
import com.jfinal.template.stat.Scope;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Text 输出纯文本块以及使用 "#[[" 与 "]]#" 指定的非解析块
|
* Text 输出纯文本块以及使用 "#[[" 与 "]]#" 定义的原样输出块
|
||||||
*/
|
*/
|
||||||
public class Text extends Stat implements IWritable {
|
public class Text extends Stat implements IWritable {
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user