Skip to content

Commit

Permalink
fixed: custom directive is contaminated by other same one in a page
Browse files Browse the repository at this point in the history
yangfuhai committed Dec 14, 2017
1 parent 1f6c479 commit 4ad5a3e
Showing 4 changed files with 47 additions and 22 deletions.
6 changes: 2 additions & 4 deletions src/main/java/io/jboot/db/model/JbootModel.java
Original file line number Diff line number Diff line change
@@ -16,10 +16,7 @@
package io.jboot.db.model;

import com.jfinal.core.JFinal;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Table;
import com.jfinal.plugin.activerecord.TableMapping;
import com.jfinal.plugin.activerecord.*;
import com.jfinal.plugin.ehcache.IDataLoader;
import io.jboot.Jboot;
import io.jboot.db.dialect.IJbootModelDialect;
@@ -683,4 +680,5 @@ public boolean equals(Object o) {

return id.equals(get(getPrimaryKey()));
}

}
22 changes: 20 additions & 2 deletions src/main/java/io/jboot/web/directive/base/JbootDirectiveBase.java
Original file line number Diff line number Diff line change
@@ -15,16 +15,23 @@
*/
package io.jboot.web.directive.base;

import com.google.common.collect.Sets;
import com.jfinal.template.Directive;
import com.jfinal.template.expr.ast.Assign;
import com.jfinal.template.expr.ast.Expr;
import com.jfinal.template.expr.ast.ExprList;
import com.jfinal.template.stat.Scope;
import io.jboot.Jboot;

import java.util.Set;

/**
* Jfinal 指令的基类
*/
public abstract class JbootDirectiveBase extends Directive {

private Set ids = Sets.newHashSet();

public JbootDirectiveBase() {
Jboot.injectMembers(this);
}
@@ -33,19 +40,30 @@ public JbootDirectiveBase() {
@Override
public void setExprList(ExprList exprList) {
super.setExprList(exprList);
for (Expr expr : exprList.getExprArray()) {
if (expr instanceof Assign) {
Assign assign = (Assign) expr;
ids.add(assign.getId());
}
}
}


/**
* 先调用 initParams 后,才能通过 getParam 获取
*
* @param scope
*/
public void initParams(Scope scope){
public void initParams(Scope scope) {
scope.getCtrl().setLocalAssignment();
exprList.eval(scope);
}

public <T> T getParam(String key, T defaultValue, Scope scope) {
Object data = scope.get(key);
if (!ids.contains(key)) {
return defaultValue;
}
Object data = scope.getLocal(key);
return (T) (data == null ? defaultValue : data);
}

35 changes: 20 additions & 15 deletions src/test/java/DirectiveTest.java
Original file line number Diff line number Diff line change
@@ -4,9 +4,7 @@
import com.jfinal.template.stat.Scope;
import io.jboot.web.directive.annotation.JFinalDirective;
import io.jboot.web.directive.base.JbootDirectiveBase;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import service.User;

@JFinalDirective("testDirective")
public class DirectiveTest extends JbootDirectiveBase {
@@ -17,27 +15,34 @@ public class DirectiveTest extends JbootDirectiveBase {

@Override
public void exec(Env env, Scope scope, Writer writer) {
String abc = service.hello("aabbcc");
initParams(scope);

try {
writer.write("testDirective : " + getParam("c",scope) +"<br />");
} catch (IOException e) {
e.printStackTrace();
}
initParams(scope);

String d = getParam("d",scope);

String content = service.hello(d);

User user = new User();
user.setName(content);

scope.set("user",user);
stat.exec(env,scope,writer);

System.out.println((HttpServletRequest) getParam(0,scope));
System.out.println((String) getParam(1,scope));
System.out.println((String) getParam("c",scope));

System.out.println(this);
//
// System.out.println((HttpServletRequest) getParam(0,scope));
// System.out.println((String) getParam(1,scope));
// System.out.println((String) getParam("c",scope));
// System.out.println((String) getParam("d",scope));
//
// System.out.println("DirectiveTest:"+this);
// System.out.println("scope:"+scope);


}


@Override
public boolean hasEnd() {
return true;
}
}
6 changes: 5 additions & 1 deletion src/test/resources/test.html
Original file line number Diff line number Diff line change
@@ -7,9 +7,13 @@
<body>


#testDirective(REQUEST,"b",c="xuz")
#testDirective(REQUEST,"b",c="xuz",d="aa")
#(user.name) <br />
#end

#testDirective(REQUEST,"bbbbbb",c="xuzzzzzz")
#(user.name) <br />
#end

</body>
</html>

0 comments on commit 4ad5a3e

Please sign in to comment.