java - 由于某种原因,我的 Play 框架 Fastag 没有被选中

标签 java playframework

我正在从 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“class”而不是查找 html 文件的代码在哪里?

最佳答案

为什么这不起作用?

好吧,我无法真正回答这个问题,因为就我而言,您的 FastTag 有效。我无法重现您的错误。我只做了一些小的调整,例如添加主体,这样我就不会收到任何错误,但它们不应该是导致您错误的原因。但为了确保,使用此标签的正确方法是:

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

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

我认为您在 最后一个 catch block 中查看了 GroovyTemplateCompilerendTag() 方法中的一些代码,您会发现以下内容代码片段,它会在尝试 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 + ");");
}

关于java - 由于某种原因,我的 Play 框架 Fastag 没有被选中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9657666/

相关文章:

Java:尝试让我的代码打印一行而不是五行

java - 缺少应用程序资源

java - 直接在模型上检查验证

java - 安卓。 fragment 不保存它们的状态

Java LDAP - 确定用户是否在给定组中?

java - 获得正确的接口(interface)粒度级别

json - 在 Play 中,渲染 JSON 时可以使用匿名类型和/或对象初始值设定项吗?

scala - 编译 JavaScript 并仅在生产模式下激活的自定义 sbt 任务

java - 如何级联删除属于 JPA 实体的集合?

javascript - 未捕获的语法错误 : Invalid or unexpected token in javascript with scala