你想要做的是定義你自己的快速標籤。有一個很好的例子here。 查看該答案爲well。
的代碼可能會是這樣的:
@FastTags.Namespace("mytags")
public class MyTags extends play.templates.FastTags{
public static void _a(Map<?, ?> args, Closure body, PrintWriter out, ExecutableTemplate template, int fromLine) {
ActionDefinition actionDef = (ActionDefinition) args.get("arg");
if (actionDef == null) {
actionDef = (ActionDefinition) args.get("action");
}
if (!("GET".equals(actionDef.method))) {
if (!("POST".equals(actionDef.method))) {
String separator = actionDef.url.indexOf('?') != -1 ? "&" : "?";
actionDef.url += separator + "x-http-method-override=" + actionDef.method;
actionDef.method = "POST";
}
String id = Codec.UUID();
out.print("<form method=\"POST\" id=\"" + id + "\" " +(args.containsKey("target") ? "target=\"" + args.get("target") + "\"" : "")+ " style=\"display:none\" action=\"" + actionDef.url + "\">");
_authenticityToken(args, body, out, template, fromLine);
out.print("</form>");
out.print("<a href=\"javascript:document.getElementById('" + id + "').submit();\" " + serialize(args, "href") + ">");
out.print(JavaExtensions.toString(body));
out.print("</a>");
} else {
out.print("<a href=\"" + actionDef.url + "\" " + JavaExtensions.slugify(serialize(args, "href")) + ">");
out.print(JavaExtensions.toString(body));
out.print("</a>");
}
}
}
,並在您的模板被稱爲像這樣:
#{mytags.a @Controller.Action(parameter) /}