java 如何将原始 html 传递给 Play 框架视图?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14743199/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to pass raw html to Play framework view?
提问by Click Upvote
I'm trying to pass a simple URL to a view within a play framework app, however when passed as a string, the &
in the url is changed to &
which causes the URL to not work.
我正在尝试将一个简单的 URL 传递给播放框架应用程序中的视图,但是当作为字符串传递时,&
url 中的 更改为&
导致 URL 不起作用。
If I change the argument to Html, i.e @(url: Srting)
to @(url: Html)
, then I get an error when I try to pass the url as string to view.render()
method.
如果我将参数更改为 Html,即更改@(url: Srting)
为@(url: Html)
,那么当我尝试将 url 作为字符串传递给view.render()
方法时会出现错误。
How can I convert the url into Html and pass it as that?
如何将 url 转换为 Html 并将其作为传递?
回答by estmatic
To prevent the default escaping that happens for dynamic content on views you need to wrap the String
with @Html(String)
function:
为了防止视图上动态内容发生的默认转义,您需要包装String
with@Html(String)
函数:
View:
看法:
@(url: String)
<div class="myLink">
Go to: @Html(url) <br>
not to: @url
</div>
Controller:
控制器:
public static Result displayLink(){
return ok(view.render("<a href='http://stackoverflow.com/'>Stack Overflow</a>"));
}
See The template enginepage on the documentation for more info (specifically the "Escaping" section at the very bottom).
有关更多信息,请参阅文档中的模板引擎页面(特别是最底部的“转义”部分)。
回答by Hanxue
If you want to render HTML content instead of displaying it as raw text, return the content .as("text/html")
. Example:
如果要呈现 HTML 内容而不是将其显示为原始文本,请返回 content .as("text/html")
。例子:
WS.url("http://www.stackoverflow.com").get().map(resp => Ok(resp.body).as("text/html"))