jfinal3.0中的模板能支持#catChildren( #(x.cat_id), 3) 吗?

如标题,就是一个指令中的参数是动态的.我在运用时,报错:

Caused by: com.jfinal.template.stat.ParseException: #catChildren parameter exists illegal char: '#'
Template: "/index.html". Line: 241
 at com.jfinal.template.stat.Lexer.scanPara(Lexer.java:257)
 at com.jfinal.template.stat.Lexer.scanDire(Lexer.java:163)
 at com.jfinal.template.stat.Lexer.scan(Lexer.java:62)
 at com.jfinal.template.stat.Parser.parse(Parser.java:88)
 at com.jfinal.template.Engine.buildTemplateByFileStringSource(Engine.java:151)
 at com.jfinal.template.Engine.getTemplate(Engine.java:137)
 at com.jfinal.render.TemplateRender.render(TemplateRender.java:61)
 ... 33 more

评论区

Irin.Chan

2017-03-29 11:33

#catChildren(x.cat_id , 3) 。#() 里面不需要再#吧?

goodjfinalx

2017-03-29 12:03

freemarker是可以的,如果是这样#catChildren(x.cat_id , 3) ,后台会报错,因为后台无法识别x.cat_id

goodjfinalx

2017-03-29 12:06

它会把x.cat_id解释为 where c.cat_id =com.jfinal.template.expr.ast.Field@7a831c61

goodjfinalx

2017-03-29 12:08

freemarker的做法:[@catChildren cat_id = x.id ],这样后台能解释出来

JFinal

2017-03-29 13:32

指令中的参数是表达式,表达式不需要 # 字符的存在,这样用: #catChildren(x.cat_id, 3)) @Irin.Chan 是对的

goodjfinalx

2017-03-29 14:31

感谢波总和@Irin.Chan 的回复,可能我功力还不够,或者我的表达不够清晰,如果我直接前端直接#catChildren(x.cat_id , 3) 调用,后台真的会报错

goodjfinalx

2017-03-29 14:31

我后台是这么写的
public class CatChildren extends Directive {

public void setExprList(ExprList exprList){
super.setExprList(exprList);
}
@Override
public void exec(Env env, Scope scope, Writer writer) {

//this.exprList.getExprArray()[0]为前台传入第一个参数
String sql = "select c.* from mall_cat c where c.cat_level = "
+ this.exprList.getExprArray()[0]
+ " limit "
+ this.exprList.getExprArray()[1];
System.out.println("catchildren_sql:"+sql);
List list = MallCat.dao.find(sql);
scope.setGlobal("catChildrenList", list);
}
public boolean headEnd(){
return true;
}
}

goodjfinalx

2017-03-29 14:32

执行后,输出的sql语句为:
catchildren_sql:select c.* from mall_cat c where c.cat_level = com.jfinal.template.expr.ast.Field@27f8040d limit 3
所以会报错:
2017-03-29 14:28:31
[ERROR]-[Thread: qtp615634843-28]-[com.jfinal.core.ActionHandler.handle()]: /
com.jfinal.render.RenderException: com.jfinal.plugin.activerecord.ActiveRecordException: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.expr.ast.Field@27f8040d limit 3' at line 1

goodjfinalx

2017-03-29 14:52

我找到问题了,是后台参数转换的问题!

goodjfinalx

2017-03-29 14:53

应该这么写:
public class CatChildren extends Directive {

public void setExprList(ExprList exprList){
super.setExprList(exprList);
}
@Override
public void exec(Env env, Scope scope, Writer writer) {
Object[] ret = this.exprList.evalExprList(scope);
System.out.println("ret:"+ret[0]);
//this.exprList.getExprArray()[0]为前台传入第一个参数
String sql = "select c.* from mall_cat c where c.cat_level = "
+ ret[0]
+ " limit "
+ ret[1];
System.out.println("catchildren_sql:"+sql);
List list = MallCat.dao.find(sql);
scope.setGlobal("catChildrenList", list);
}
public boolean headEnd(){
return true;
}
}

热门反馈

扫码入社