2012-03-11 42 views
2

我從playframework複製選擇標記來測試創建標籤以及fasttags(做一個fasttag選項)。但唯一的問題是我得到這個錯誤時,應該尋找fasttag ...我的playframework快速標記沒有被拾取出於某種原因

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

我FastTags類是在app /標籤目錄下,下面的代碼....

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 source中的代碼在哪裏查找fasttag「class」而不是查找html文件?

回答

3

爲什麼這不起作用?

好吧,我真的不能回答這個問題,因爲在我的情況下,你的FastTag工作。我無法重現您的錯誤。我只做了一些小的調整,比如添加一個body,這樣我就不會有任何錯誤,但它們不應該是你錯誤的原因。但是,以確保公正,使用該標籤的正確方法應該是:

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

哪裏是在玩的代碼!查找FastTags「類」而不是查找html文件的框架源代碼?

我想你看了看一些代碼在GroovyTemplateCompilerendTag()方法是最後一個catch塊,你會發現下面的代碼片段,這也嘗試嘗試invokeTag()之前加載FastTags。爲了清晰起見,我還添加了一些額外的評論。

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

謝謝,我將不得不在那裏調試它然後找出爲什麼它沒有被發現....謝謝,這應該幫助我克服駝峯,我會回來。 – 2012-03-15 13:50:38

2

我有同樣的問題。我以前有一個具有相同名稱的自定義標籤,但在views/tags目錄中實現爲HTML文件。我想做一些更復雜的事情,所以我將標籤重新實現爲FastTag子類。我得到了你所犯的錯誤。

解決方案只是運行play clean。我猜Play已經將HTML標記模板作爲類文件緩存在那裏...(?)

希望這可以幫助你。

+0

+1這也是我的問題,我把我的FastTag移動到了一個模塊中,Play停止使用它,直到我運行'play clean' – 2012-12-30 00:18:56

相關問題