主题:htm 自定义标签 export属性的问题

float 2019年11月17日 33

我把之前想在标签外使用 标签重绑定的属性的这个问题简化了如下!

测试代码

Properties properties = new Properties();
properties.setProperty(Configuration.HTML_TAG_BINDING_ATTRIBUTE, "var,export");
Configuration cfg = new Configuration(properties);
GroupTemplate core = new GroupTemplate(new StringTemplateResourceLoader(), cfg);
core.registerTag("set", SetTag.class);
String temp="<#set data=\"1234\" export=\"a\" />${a}";
Template tempTemplate = core.getTemplate(temp);
System.out.println(tempTemplate.render());


SetTag 类简单实现

public class SetTag extends SimpleTag {

@Override
public void render() {
Object dataValue = this.getAttributeValue(ATTR_KEY_DATA);
this.binds(dataValue);
}
}

错误提示:

>>12:35:15:错误:htmltag 位于1行 资源:<#set data="1234" export="a" />${a}
tag执行抛错
1|<#set data="1234" export="a" />${a}
java.lang.RuntimeException: html标签没有定义绑定变量,但标签实现中试图绑定[1234]
	at org.beetl.core.GeneralVarTagBinding.binds(GeneralVarTagBinding.java:86)
	at cn.ifloat.brick.generator.tookit.beetl.tags.SetTag.render(SetTag.java:12)
	at org.beetl.ext.tag.HTMLTagSupportWrapper.callTag(HTMLTagSupportWrapper.java:117)
	at org.beetl.ext.tag.HTMLTagSupportWrapper.render(HTMLTagSupportWrapper.java:74)
	at org.beetl.core.statement.TagStatement.runTag(TagStatement.java:110)
	at org.beetl.core.statement.TagStatement.execute(TagStatement.java:87)
	at org.beetl.core.statement.Program.execute(Program.java:70)
	at org.beetl.core.Template.renderTo(Template.java:136)
	at org.beetl.core.Template.renderTo(Template.java:89)
	at org.beetl.core.Template.render(Template.java:76)
	at test.services.beetl.TestBeetl.test15(TestBeetl.java:27)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:497)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
	at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
	at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
joelli 2019年11月20日

我这里啊并没有错误,你的代码我改动成为`GeneralVarTagBinding`

public class SetTag extends GeneralVarTagBinding
joelli 2019年11月20日

你用的是beetl最新版本吗,最新才知道export关键字

float 2019年11月21日

@joelli 感谢! 是版本的问题 我用的是2.9.3