ソースを参照

新增支持 LocalDateTime、LocalDate、LocalTime

James 5 年 前
コミット
b8b8e7a71f

+ 40 - 48
src/main/java/com/jfinal/template/ext/directive/DateDirective.java

@@ -16,7 +16,7 @@
 
 
 package com.jfinal.template.ext.directive;
 package com.jfinal.template.ext.directive;
 
 
-import java.io.IOException;
+import java.time.temporal.Temporal;
 import java.util.Date;
 import java.util.Date;
 import com.jfinal.template.Directive;
 import com.jfinal.template.Directive;
 import com.jfinal.template.Env;
 import com.jfinal.template.Env;
@@ -38,72 +38,69 @@ import com.jfinal.template.stat.Scope;
  * 注意:
  * 注意:
  * 1:#date 指令中的参数可以是变量,例如:#date(d, p) 中的 d 与 p 可以全都是变量
  * 1:#date 指令中的参数可以是变量,例如:#date(d, p) 中的 d 与 p 可以全都是变量
  * 2:默认 datePattern 可通过 Engine.setDatePattern(...) 进行配置
  * 2:默认 datePattern 可通过 Engine.setDatePattern(...) 进行配置
+ * 3:jfinal 4.9.02 版新增支持 java 8 的 LocalDateTime、LocalDate、LocalTime
  */
  */
 public class DateDirective extends Directive {
 public class DateDirective extends Directive {
 	
 	
-	private Expr valueExpr;
-	private Expr datePatternExpr;
-	private int paraNum;
+	private Expr dateExpr;
+	private Expr patternExpr;
 	
 	
 	public void setExprList(ExprList exprList) {
 	public void setExprList(ExprList exprList) {
-		this.paraNum = exprList.length();
+		int paraNum = exprList.length();
 		if (paraNum > 2) {
 		if (paraNum > 2) {
 			throw new ParseException("Wrong number parameter of #date directive, two parameters allowed at most", location);
 			throw new ParseException("Wrong number parameter of #date directive, two parameters allowed at most", location);
 		}
 		}
 		
 		
 		if (paraNum == 0) {
 		if (paraNum == 0) {
-			this.valueExpr = null;
-			this.datePatternExpr = null;
+			this.dateExpr = null;
+			this.patternExpr = null;
 		} else if (paraNum == 1) {
 		} else if (paraNum == 1) {
-			this.valueExpr = exprList.getExpr(0);
-			this.datePatternExpr = null;
+			this.dateExpr = exprList.getExpr(0);
+			this.patternExpr = null;
 		} else if (paraNum == 2) {
 		} else if (paraNum == 2) {
-			this.valueExpr = exprList.getExpr(0);
-			this.datePatternExpr = exprList.getExpr(1);
+			this.dateExpr = exprList.getExpr(0);
+			this.patternExpr = exprList.getExpr(1);
 		}
 		}
 	}
 	}
 	
 	
 	public void exec(Env env, Scope scope, Writer writer) {
 	public void exec(Env env, Scope scope, Writer writer) {
-		if (paraNum == 1) {
-			outputWithoutDatePattern(env, scope, writer);
-		} else if (paraNum == 2) {
-			outputWithDatePattern(env, scope, writer);
+		Object date;
+		String pattern;
+		
+		if (dateExpr != null) {
+			date = dateExpr.eval(scope);
 		} else {
 		} else {
-			outputToday(env, writer);
-		}
-	}
-	
-	private void outputToday(Env env, Writer writer) {
-		write(writer, new Date(), env.getEngineConfig().getDatePattern());
-	}
-	
-	private void outputWithoutDatePattern(Env env, Scope scope, Writer writer) {
-		Object value = valueExpr.eval(scope);
-		if (value instanceof Date) {
-			write(writer, (Date)value, env.getEngineConfig().getDatePattern());
-		} else if (value != null) {
-			throw new TemplateException("The first parameter date of #date directive must be Date type", location);
+			date = new Date();;
 		}
 		}
-	}
-	
-	private void outputWithDatePattern(Env env, Scope scope, Writer writer) {
-		Object value = valueExpr.eval(scope);
-		if (value instanceof Date) {
-			Object datePattern = this.datePatternExpr.eval(scope);
-			if (datePattern instanceof String) {
-				write(writer, (Date)value, (String)datePattern);
+		
+		if (patternExpr != null) {
+			Object temp = patternExpr.eval(scope);
+			if (temp instanceof String) {
+				pattern = (String)temp;
 			} else {
 			} else {
-				throw new TemplateException("The sencond parameter datePattern of #date directive must be String", location);
+				throw new TemplateException("The second parameter datePattern of #date directive must be String", location);
 			}
 			}
-		} else if (value != null) {
-			throw new TemplateException("The first parameter date of #date directive must be Date type", location);
+		} else {
+			pattern = env.getEngineConfig().getDatePattern();
 		}
 		}
+		
+		write(date, pattern, writer);
 	}
 	}
 	
 	
-	private void write(Writer writer, Date date, String datePattern) {
+	private void write(Object date, String pattern, Writer writer) {
 		try {
 		try {
-			writer.write(date, datePattern);
-		} catch (IOException e) {
+			
+			if (date instanceof Date) {
+				writer.write((Date)date, pattern);
+			} else if (date instanceof Temporal) {		// 输出 LocalDateTime、LocalDate、LocalTime
+				writer.write((Temporal)date, pattern);
+			} else if (date != null) {
+				throw new TemplateException("The first parameter of #date directive can not be " + date.getClass().getName(), location);
+			}
+		
+		} catch(TemplateException e) {
+			throw e;
+		} catch(Exception e) {
 			throw new TemplateException(e.getMessage(), location, e);
 			throw new TemplateException(e.getMessage(), location, e);
 		}
 		}
 	}
 	}
@@ -111,8 +108,3 @@ public class DateDirective extends Directive {
 
 
 
 
 
 
-
-
-
-
-