簡體   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