java - 模板 playframework 中的 html 语法

标签 java scala playframework

我在 Play 中创建了一个新 Controller !框架。我想将静态文本放入 Scala 模板中,但我得到 <br /> (所有 html 语法)呈现为文本。我怎样才能做到这一点?

public class Application extends Controller {

    public static Result index() {
        String page_content = "<strong>Lorem </strong>Ipsum is simply dummy text of the<br /> printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.";
        return ok(views.html.index.render(page_content, "index"));
    }

}

最佳答案

您的函数index正在将两个字符串传递给您的 View 函数:

public static Result index() {
    String page_content = "<strong>Lorem </strong>Ipsum is simply dummy text of the<br /> printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.";
    return ok(views.html.index.render(page_content, "index"));
}

所以你的模板应该是这样的:

@(controllerHtml: String, title: String)
@main(title) {
 @Html(controllerHTML)
}

请记住: 默认情况下,动态内容部分根据模板类型(例如 HTML 或 XML)规则进行转义。如果要输出原始内容片段,请将其包装在模板内容类型中。请参阅文档:http://www.playframework.com/documentation/2.2.x/JavaTemplates

关于java - 模板 playframework 中的 html 语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20421392/

相关文章:

postgresql - Evolutions 说我的 SQL 有语法错误

java - 如何使用cacheApi在play框架中设置清理缓存超时?

java - 如何在 Spring 属性中进行算术运算?

java - 在两个不同的固定位置用逗号格式化数字

java - 使用 onClickListener 将数据发送到下一个 Activity 的一些问题

scala - 验证 Datomic 实体 ID 是否有效

java - 为什么要扩展 Exception 类?

scala - 什么 lmcoursier.internal.shaded.coursier.error.FetchError$DownloadingArtifacts : Error fetching artifacts: means?

eclipse - 使用 Eclipse 为 Scala 创建新的 SBT 项目

user-interface - 需要设计类似 Gmail 的收件箱页面