2017-04-05 19:40

我也遇到类似的问题,没办法,我只好把数据库中的模板拿出来,单独#define,这样就搞定了!

2017-04-02 11:42

这个模板的确好用,主要是波总创造的,大家都愿意追随,要是有本书就更好了

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;
}
}

2017-03-29 14:52

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

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

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;
}
}

2017-03-29 14:31

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

2017-03-29 12:08

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

2017-03-29 12:06

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

2017-03-29 12:03

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