繁体   English   中英

由于某些原因,我的playframework快速标签未被提取

[英]my playframework fast tag is not being picked up for some reason

我正在从playframework复制选择标签,以测试创建标签以及快速标签(将选项作为快速标签)。 但是唯一的问题是当我应该寻找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/}

这里的代码意味着它永远不会查找一个快速标签(其中隐藏了查找快速标签的代码)...

 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“类”而不是html文件的代码在哪里?

为什么这不起作用?

好的,我无法真正回答这个问题,因为就我而言,您的FastTag可以正常工作。 我无法重现您的错误。 我只做了一些细微的调整,例如添加一个主体,这样我就不会出现任何错误,但它们不应成为您错误的原因。 但是只是为了确保,使用此标记的正确方法是:

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

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

我想你看着在一些代码endTag()的方法GroovyTemplateCompiler在它的最后一个catch块 ,你会发现下面的代码片段,这并试图尝试之前加载FastTags invokeTag() 为了清楚起见,我还添加了一些额外的注释。

// 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 + ");");
}

我有同样的问题。 我以前有一个同名的自定义标签,但是在views / tags目录中实现为HTML文件。 我想做些复​​杂的事情,所以我将标签重新实现为FastTag子类。 我得到了你犯的错误。

解决的方法就是简单地进行play clean 我想Play已将HTML标记模板作为类文件缓存在其中...(?)

希望这对您有帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM