要制作一个下面这样的CMS标签,可以实现 多个自定义参数,带参数名和参数值
#articlelist(channelid=10,pagenumber=1,pagesize=10)
<li> <a href="#(article.getArticleUrl())">#(article.title)</a></li>
#end
---------------------------------------------------
第一步,修改一下jfinal源码 (最新版本不用修改,波总改过了)
package com.jfinal.template.expr.ast;
import java.util.List;
import java.util.Map;
import com.jfinal.template.TemplateException;
import com.jfinal.template.stat.Location;
import com.jfinal.template.stat.ParseException;
import com.jfinal.template.stat.Scope;
/**
* Assign
*
* 支持三种赋值,其中第二种如果括号中是 ID 或 STR 则演变为第三种是对 map 赋值:
* 1:ID = expr
* 2:ID [ expr ] = expr
* 如果 expr 为 int 或 long 型,则是对 array 赋值
* 如果 expr 为 ID、STR 型,则是对 map 进行赋值
* 否则抛异常出来
* 3:ID [ ID ] = expr 或者 ID [ STR ] = expr
* 4:支持无限连:id = array[ i = 0 ] = array[1] = 123
*/
public class Assign extends Expr {
private String id;
private Expr index; // index 用于支持 ID [ expr ] = expr 这种形式
private Expr right;
/**
* 数组赋值表达式
*/
public Assign(String id, Expr index, Expr right, Location location) {
if (index == null) {
throw new ParseException("The index expression of array assignment can not be null", location);
}
if (right == null) {
throw new ParseException("The expression on the right side of an assignment expression can not be null", location);
}
this.id = id;
this.index = index;
this.right = right;
this.location = location;
}
/**
* 普通赋值表达式
*/
public Assign(String id, Expr right, Location location) {
if (right == null) {
throw new ParseException("The expression on the right side of an assignment expression can not be null", location);
}
this.id = id;
this.index = null;
this.right = right;
this.location = location;
}
/**
* 获取 assign 表达式左侧标识符 id
* 在自定义指令中得到 id 值,可以得知该赋值表达式是针对哪个变量在操作,有助于扩展
* 需求来源:http://www.jfinal.com/share/379
*/
public String getId() {
return id;
}
public Expr getIndex() {
return index;
}
public Expr getRight() {
return right;
}
/**
* 赋值语句有返回值,可以用于表达式计算
*/
public Object eval(Scope scope) {
if (index == null) {
return assignVariable(scope);
} else {
return assignElement(scope);
}
}
Object assignVariable(Scope scope) {
Object rightValue = right.eval(scope);
if (scope.getCtrl().isWisdomAssignment()) {
scope.set(id, rightValue);
} else if (scope.getCtrl().isLocalAssignment()) {
scope.setLocal(id, rightValue);
} else {
scope.setGlobal(id, rightValue);
}
return rightValue;
}
/**
* 数组或 Map 赋值
*/
@SuppressWarnings({"unchecked", "rawtypes"})
Object assignElement(Scope scope) {
Object target = scope.get(id);
if (target == null) {
throw new TemplateException("The assigned targets \"" + id + "\" can not be null", location);
}
Object idx = index.eval(scope);
if (idx == null) {
throw new TemplateException("The index of list/array and the key of map can not be null", location);
}
Object value;
if (target instanceof Map) {
value = right.eval(scope);
((Map)target).put(idx, value);
return value;
}
if ( !(idx instanceof Integer) ) {
throw new TemplateException("The index of list/array can only be integer", location);
}
if (target instanceof List) {
value = right.eval(scope);
((List)target).set((Integer)idx, value);
return value;
}
if (target.getClass().isArray()) {
value = right.eval(scope);
java.lang.reflect.Array.set(target, (Integer)idx, value);
return value;
}
throw new TemplateException("Only the list array and map is supported by index assignment", location);
}
}
-----------------------------------------------------------------
第二步
public class ArticleListDirective extends Directive {
Expr[] exprArray=null;
@Override
public void setExprList(ExprList exprList) {
// TODO Auto-generated method stub
super.setExprList(exprList);
this.exprArray = exprList.getExprArray();
}
@Override
public void exec(Env env, Scope scope, Writer writer) {
Kv kv=Kv.create();//存放各个参数
for (int i=0;i<exprArray.length;i++)
{ //循环参数列表,并把参数放到 Kv 里面
Assign obj= (Assign)exprArray[i];
//System.out.println(obj.id+" "+obj.right.eval(scope));
kv.set(obj.getId().toLowerCase(), obj.eval(scope));
}
// 获取到的参数名和参数值 ,都在KV里面,该干啥干啥吧,这里省略了
Page<Article> page = Article.dao.paginateByCache(xxxxxxxxxx);//根据上面得到的参数,自行组合SQL查询
List list=page.getList();
for (int i=0;i<list.size();i++)
{
scope.set("article",list.get(i));
stat.exec(env, scope, writer);//执行自定义标签中包围的 html
}
}
@Override
public boolean hasEnd() {
return true;
}
}
----------------------------------------------------------------
第三步
@Override
public void configEngine(Engine me) {
me.setBaseTemplatePath(PathKit.getWebRootPath());
me.addDirective("articlelist", new ArticleListDirective());
}
-----------------------------------------------------------------------
结束
有个小建议,Assign.java 源代码其实不需要改变,在面对任何表达式的时候,只需要调用它的 eval 方法就可以获取到值了,例如对于 Assign 来说获取 Assign.right 这个 right 变量表达式的值可以是这样:
Object value = assign.eval(scope);
因为 assign 表达式整体的值就等于 assign.right 的值