play framework 2.1.3 extend play.api.templates.Html

依然范特西╮ 提交于 2019-12-08 03:41:42

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!