enjoy 3.4

This commit is contained in:
James 2018-04-01 16:29:13 +08:00
parent 2ed806c296
commit ef7b0da917
16 changed files with 215 additions and 109 deletions

View File

@ -162,6 +162,13 @@ public class Engine {
/**
* 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
* content 有紧密的对应关系 content 发生变化时 key 值也相应变化
@ -170,13 +177,7 @@ public class Engine {
*
* getTemplateByString(String, boolean) 中的 String 参数的
* 数量可控并且确定时才可对其使用缓存
*/
public Template getTemplateByString(String content) {
return getTemplateByString(content, false);
}
/**
* Get template by string content
*
* @param content 模板内容
* @param cache true 则缓存 Template否则不缓存
*/

View File

@ -16,6 +16,10 @@
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.Writer;
import java.util.Map;
@ -108,6 +112,32 @@ public class Template {
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() {
return env.isSourceListModified();
}

View File

@ -23,7 +23,7 @@ import com.jfinal.template.stat.Scope;
*/
public class Id extends Expr {
private String id;
private final String id;
public Id(String id) {
this.id = id;

View File

@ -24,8 +24,10 @@ import com.jfinal.template.stat.Scope;
* Map
*
* 1定义 map 常量
* {k1:123, k2:"abc", 'k3':true, "k4":[1,2,3], k5:1+2}
* 如上所示map定义的 key 可以为 String 或者 id 标识符而右侧的 value 可以是任意的常量与表达式
* {k1:123, k2:"abc", 'k3':true, "k4":[1,2,3], k5:1+2, 1:12, true:"Y", null:"abc"}
* 如上所示map定义的 key 可以为 id 标识符或者 StringIntegerLongBooleannull
* 等常量值 (详见 ExprParser.buildMapEntry(...) 方法)
* 右侧的 value 可以是任意的常量与表达式
*
* 2取值
* 先将 Map 常量赋值给某个变量 #set(map = {...})
@ -35,6 +37,10 @@ import com.jfinal.template.stat.Scope;
* map.get("k1")
* map.k1
*
* 3不通过中间变量取值
* {1:'自买', 2:'跟买'}.get(1)
* {1:'自买', 2:'跟买'}[2]
*
* 如上所示当以下标方式取值时下标参数可以是 string expr expr 求值以后的值必须也为 string类型
* 当用 map.k1 这类 field 字段取值形式时则是使用 id 标识符而不是 string 形参数
*

View File

@ -32,7 +32,7 @@ import com.jfinal.template.stat.Scope;
*/
public class NowDirective extends Directive {
public void setExrpList(ExprList exprList) {
public void setExprList(ExprList exprList) {
if (exprList.length() > 1) {
throw new ParseException("#now directive support one parameter only", location);
}

View File

@ -16,22 +16,29 @@
package com.jfinal.template.ext.directive;
import java.io.IOException;
import com.jfinal.template.Directive;
import com.jfinal.template.Env;
import com.jfinal.template.TemplateException;
import com.jfinal.template.io.Writer;
import com.jfinal.template.stat.Scope;
/**
* 输出随机数
* 输出 int 随机数
*/
public class RandomDirective extends Directive {
private java.util.Random random = new java.util.Random();
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);
}
}
}

View File

@ -43,6 +43,14 @@ public class ByteExt {
public Double toDouble(Byte self) {
return self.doubleValue();
}
public Short toShort(Byte self) {
return self.shortValue();
}
public Byte toByte(Byte self) {
return self;
}
}

View File

@ -43,6 +43,14 @@ public class DoubleExt {
public Double toDouble(Double self) {
return self;
}
public Short toShort(Double self) {
return self.shortValue();
}
public Byte toByte(Double self) {
return self.byteValue();
}
}

View File

@ -43,6 +43,14 @@ public class FloatExt {
public Double toDouble(Float self) {
return self.doubleValue();
}
public Short toShort(Float self) {
return self.shortValue();
}
public Byte toByte(Float self) {
return self.byteValue();
}
}

View File

@ -63,6 +63,14 @@ public class IntegerExt {
public Double toDouble(Integer self) {
return self.doubleValue();
}
public Short toShort(Integer self) {
return self.shortValue();
}
public Byte toByte(Integer self) {
return self.byteValue();
}
}

View File

@ -43,6 +43,14 @@ public class LongExt {
public Double toDouble(Long self) {
return self.doubleValue();
}
public Short toShort(Long self) {
return self.shortValue();
}
public Byte toByte(Long self) {
return self.byteValue();
}
}

View File

@ -43,6 +43,14 @@ public class ShortExt {
public Double toDouble(Short self) {
return self.doubleValue();
}
public Short toShort(Short self) {
return self;
}
public Byte toByte(Short self) {
return self.byteValue();
}
}

View File

@ -79,6 +79,14 @@ public class StringExt {
public Double toDouble(String 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);
}
}

View File

@ -59,10 +59,9 @@ public class JFinalView extends AbstractTemplateView {
OutputStream os = response.getOutputStream();
JFinalViewResolver.engine.getTemplate(getUrl()).render(model, os);
}
}
@SuppressWarnings({"unchecked", "rawtypes", "deprecation"})
class InnerSession extends HashMap<Object, Object> implements HttpSession {
@SuppressWarnings({"unchecked", "rawtypes", "deprecation"})
public static class InnerSession extends HashMap<Object, Object> implements HttpSession {
private static final long serialVersionUID = -8679493647540628009L;
private HttpSession session;
@ -155,6 +154,11 @@ class InnerSession extends HashMap<Object, Object> implements HttpSession {
public void setMaxInactiveInterval(int 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 {

View File

@ -77,6 +77,9 @@ public class FileSource implements ISource {
}
private String buildFinalFileName(String baseTemplatePath, String fileName) {
if (baseTemplatePath == null) {
return fileName;
}
char firstChar = fileName.charAt(0);
String finalFileName;
if (firstChar == '/' || firstChar == '\\') {

View File

@ -25,7 +25,7 @@ import com.jfinal.template.io.Writer;
import com.jfinal.template.stat.Scope;
/**
* Text 输出纯文本块以及使用 "#[[" "]]#" 指定的非解析
* Text 输出纯文本块以及使用 "#[[" "]]#" 定义的原样输出
*/
public class Text extends Stat implements IWritable {