2013-04-28 46 views
1

如何在不使用外部模板文件的情况下从Play 2渲染内联HTML?使用Scala从Play Framework渲染内联HTML?

def checkStatus = Action { 
    val status = ... 
    if (status.ok) { 
    Ok("Status OK") 
    } else { 
    // Oops, this renders the literal text instead of the HTML I wanted: 

    Ok("Bad status, check out <a href='http://help.us.com/'>our help page</a>") 
    } 
} 

回答

4

Ok("Hello World!")设置Content-Typetext/plain除非明确规定:

Ok("Bad status, check out <a href='http://help.us.com/'>our help page</a>").as(HTML) 

Docs

2

当你渲染视图,播放识别它的类型(至少对HTML,XML,以及txt),但是当你想返回普通的String时,你需要指示它是哪种类型(否则假设为text/plain

根据Manipaliting response doc您需要的类型与返回:

BadRequest("Bad status, check out <a href='http://help.us.com/'>our help page</a>").as("text/html")