我的playframework快速标签由于某种原因没有被选中

时间:2012-03-11 18:29:17

标签: java playframework

我正在从playframework复制select标签以测试创建标签以及fasttags(将该选项作为fasttag)。但唯一的问题是我应该在寻找fasttag时遇到这个错误...

The template tags/alvazan/option.html or tags/alvazan/option.tag does not exist.

我的FastTags类位于app / tags目录中,并且是以下代码....

package tags;

import groovy.lang.Closure;

import java.io.PrintWriter;
import java.util.Map;

import play.templates.FastTags;
import play.templates.JavaExtensions;
import play.templates.TagContext;
import play.templates.GroovyTemplate.ExecutableTemplate;

@FastTags.Namespace("alvazan")
public class TagHelp extends FastTags {

        public static void _option(Map<?, ?> args, Closure body, PrintWriter out, ExecutableTemplate template, int fromLine) {
            Object value = args.get("arg");
            TagContext ctx = TagContext.parent("alvazanselect");
            Object selectedValue = ctx.data.get("selected");
            boolean selected = selectedValue != null && value != null && (selectedValue.toString()).equals(value.toString());
            out.print("<option value=\"" + (value == null ? "" : value) + "\" " + (selected ? "selected=\"selected\"" : "") + " " + FastTags.serialize(args, "selected", "value") + ">");
            out.println(JavaExtensions.toString(body));
            out.print("</option>");
        }
    }

我的html然后有这个找不到...

#{alvazan.option/}

这里的代码暗示它永远不会查找fasttag(查找fasttags隐藏的代码在哪里)......

 public void invokeTag(Integer fromLine, String tag, Map<String, Object> attrs, Closure body) {
            String templateName = tag.replace(".", "/");
            String callerExtension = "tag";
            if (template.name.indexOf(".") > 0) {
                callerExtension = template.name.substring(template.name.lastIndexOf(".") + 1);
            }
            BaseTemplate tagTemplate = null;
            try {
                tagTemplate = (BaseTemplate)TemplateLoader.load("tags/" + templateName + "." + callerExtension);
            } catch (TemplateNotFoundException e) {
                try {
                    tagTemplate = (BaseTemplate)TemplateLoader.load("tags/" + templateName + ".tag");
                } catch (TemplateNotFoundException ex) {
                    if (callerExtension.equals("tag")) {
                        throw new TemplateNotFoundException("tags/" + templateName + ".tag", template, fromLine);
                    }
                    throw new TemplateNotFoundException("tags/" + templateName + "." + callerExtension + " or tags/" + templateName + ".tag", template, fromLine);
                }
            }
            TagContext.enterTag(tag);
            Map<String, Object> args = new HashMap<String, Object>();
            args.put("session", getBinding().getVariables().get("session"));
            args.put("flash", getBinding().getVariables().get("flash"));
            args.put("request", getBinding().getVariables().get("request"));
            args.put("params", getBinding().getVariables().get("params"));
            args.put("play", getBinding().getVariables().get("play"));
            args.put("lang", getBinding().getVariables().get("lang"));
            args.put("messages", getBinding().getVariables().get("messages"));
            args.put("out", getBinding().getVariable("out"));
            args.put("_attrs", attrs);
            // all other vars are template-specific
            args.put("_caller", getBinding().getVariables());
            if (attrs != null) {
                for (Map.Entry<String, Object> entry : attrs.entrySet()) {
                    args.put("_" + entry.getKey(), entry.getValue());
                }
            }
            args.put("_body", body);
            try {
                tagTemplate.internalRender(args);
            } catch (TagInternalException e) {
                throw new TemplateExecutionException(template, fromLine, e.getMessage(), template.cleanStackTrace(e));
            } catch (TemplateNotFoundException e) {
                throw new TemplateNotFoundException(e.getPath(), template, fromLine);
            }
            TagContext.exitTag();
        }

2个问题

  1. 为什么这不起作用?
  2. playframework源代码中的代码在哪里查找fasttag“class”而不是查找html文件?

2 个答案:

答案 0 :(得分:3)

为什么这不起作用?

好的,我无法回答这个问题,因为在我看来你的FastTag工作正常。我无法重现您的错误。我只做了一些小的调整,比如添加一个体,所以我不会得到任何错误,但它们不应该是你的错误的原因。但只是为了确保使用此标记的正确方法是:

#{select name:'dropdown'}
    #{alvazan.option "valueHere"}This is an option#{/alvazan.option}
#{/select}

Play中的代码在哪里!查找FastTags“类”而不是查找html文件的框架源?

我认为您查看了endTag() GroovyTemplateCompiler方法中的一些代码,其中最后一个阻止您将找到以下代码段,它会尝试加载尝试invokeTag()之前的FastTag。为了清晰起见,我还添加了一些额外的评论。

// Use fastTag if exists
List<Class> fastClasses = new ArrayList<Class>();
try {
    // Will contain your TagHelp class
    fastClasses = Play.classloader.getAssignableClasses(FastTags.class);
} catch (Exception xe) {
    //
}
// Add FastTags class in first spot (takes precedence over your fasttags, 
// so tags with the same name as in the FastTags class won't work)
fastClasses.add(0, FastTags.class);
// Will contain the tag method
Method m = null;
String tName = tag.name;
String tSpace = "";
// Check for namespace
if (tName.indexOf(".") > 0) {
    tSpace = tName.substring(0, tName.lastIndexOf("."));
    tName = tName.substring(tName.lastIndexOf(".") + 1);
}
for (Class<?> c : fastClasses) {
    // Check Namespace Annotation first
    if (!c.isAnnotationPresent(FastTags.Namespace.class) && tSpace.length() > 0) {
        continue;
    }
    if (c.isAnnotationPresent(FastTags.Namespace.class) && !c.getAnnotation(FastTags.Namespace.class).value().equals(tSpace)) {
        continue;
    }
    // Try to find the FastTag
    try {          
        m = c.getDeclaredMethod("_" + tName, Map.class, Closure.class, PrintWriter.class, GroovyTemplate.ExecutableTemplate.class, int.class);
    } catch (NoSuchMethodException ex) {
        continue;
    }
}
if (m != null) {
    // If it did find a FastTag (m != null)
    print("play.templates.TagContext.enterTag('" + tag.name + "');");
    print("_('" + m.getDeclaringClass().getName() + "')._" + tName + "(attrs" + tagIndex + ",body" + tagIndex + ", out, this, " + tag.startLine + ");");
    print("play.templates.TagContext.exitTag();");
} else {
    // If it didn't find any FastTags (m == null)
    // Now it will try to look up an html / tag file
    print("invokeTag(" + tag.startLine + ",'" + tagName + "',attrs" + tagIndex + ",body" + tagIndex + ");");
}

答案 1 :(得分:2)

我遇到了同样的问题。我以前有一个具有相同名称的自定义标记,但在views / tags目录中实现为HTML文件。我想做一些更复杂的事情,所以我重新实现了标签作为FastTag的子类。我收到你的错误。

解决方案只是运行play clean。我猜Play已将HTML标记模板缓存为那里的类文件...(?)

希望这会对你有所帮助。

相关问题