【问题标题】:Play! 2.3.1 - displaying HTML source code instead of page玩! 2.3.1 - 显示 HTML 源代码而不是页面
【发布时间】:2014-08-30 02:18:53
【问题描述】:

我的应用程序类如下所示:

public class Application extends Controller {

public static Result index() {
    return ok(index.render("<p>This is a paragraph</p>"));
}

我的index.scala.html 文件看起来像:

@(htmlcode: String)

@main("Example") {
    @htmlcode
}

我的main.scala.html 文件相当简单,带有标准的!DOCTYPE 声明、html、head、body 标签等。

...
<body>@content</body>
...

但是当我运行我的应用程序时,索引页面会显示源代码&lt;p&gt;This is a paragraph&lt;/p&gt; 而不仅仅是This is a paragraph。源文件看起来像

...
<body>
&lt;p&gt;This is a paragraph&lt;/p&gt;
</body>
...

如何让页面呈现代码而不是仅仅显示它?

【问题讨论】:

    标签: java html scala playframework-2.0


    【解决方案1】:

    它只是转换/保护你给它的值。所有的 HTML 标记都应该在模板 (scala.html) 文件中完成,而不是在控制器中。

    顺便说一句,这个问题与另一个问题非常接近:How to pass raw html to Play framework view?

    【讨论】:

    • 那么控制器没有办法编写HTML代码吗?我正在使用 Tumblr API 并检索博客帖子信息,该 API 以 Java 字符串的 HTML 形式返回每个帖子,我想在页面上显示。
    • 啊,我看到你的后续了。非常感谢。
    • @ypan 从第三方 API 渲染原始 HTML 时要非常小心。你对脚本注入攻击敞开心扉。
    • @acjay 这是什么意思?我可以在哪里阅读更多相关信息?
    • @ypan Google 跨站脚本 (XSS)。
    【解决方案2】:

    只需使用 Html() 函数

    @main("Example") {
    @Html(htmlcode)
    }
    

    【讨论】:

      猜你喜欢
      • 2014-02-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-19
      • 2019-02-24
      • 2014-03-18
      相关资源
      最近更新 更多