问题
Just wondering if it's possible and if yes how hard it is to extend the play.api.templates.Html class.
import java.lang.StringBuilder;
public class MyHtml extends play.api.templates.Html {
}
gives me the error:
error: constructor Html in class Html cannot be applied to given types;
this gives the same:
public class MyHtml extends play.api.templates.Html {
String text;
public MyHtml(String text) {
this.text = text;
}
}
Thank you
回答1:
play.api.templates.Html is written in Scala not Java, so it uses not java.lang.StringBuilder but scala.collection.mutable.StringBuilder.
This compiles (I am not saying, that is useful or even good practice):
package views;
public class MyHtml extends play.api.templates.Html {
private String text;
public MyHtml(String text) {//please take care of HTML injection!!!
super(scala.collection.mutable.StringBuilder$.MODULE$.newBuilder());
this.text = text;
}
@Override
public String body() {
return text;
}
}
来源:https://stackoverflow.com/questions/19482375/play-framework-2-1-3-extend-play-api-templates-html