How can I render a simple html page using play framework?

14,603

Solution 1

Here is my solution:

in routes: I do some configurations as following.

GET     /hello.html                 controllers.Assets.at(path="/public/html", file="hello.html")
GET     /public/javascripts/jquery-1.9.0.min.js     controllers.Assets.at(path="/public/javascripts", file="jquery-1.9.0.min.js")
GET     /public/stylesheets/bootstrap.css           controllers.Assets.at(path="/public/stylesheets", file="bootstrap.css")

and then the file structure is as below:

public->HTML->hello.html
public->javascripts->jquery-1.9.0.min.js
public->stylesheets->bootstrap.css

for hello.html , here is its content.

<!DOCTYPE html>    
<html lang="en">
<head>
    <meta charset="utf-8">
    <link rel='stylesheet' type='text/css' href='/public/stylesheets/bootstrap.css'>
</head>    
<body>
    <script src="/public/javascripts/jquery-1.9.0.min.js" type="text/javascript"></script>
</body>
</html>

After these three steps, you can use outside HTML directly. No need to follow Play template to do front-end development work. So now, Play only is responsible for back-end. Front-end developer only needs to operate this public file to do development.

Solution 2

Of course, put your whole static html ie. in index.scala.html and use simplest possible way:

public static Result index(){
    return ok(index.render());
}

That are the basics, you should go trough Play's documentation and samples

Solution 3

GET / controllers.Assets.at(path="/public/html", file="index.html")

This is working in my case with play 2.0.1. Hierarchy is public - html ---index.html

Share:
14,603
nightograph
Author by

nightograph

Updated on June 05, 2022

Comments

  • nightograph
    nightograph almost 2 years

    Is it a way to render a pure html file with play framework version 2? I don't want to put it in public/ folder because later on there will be some dynamic information added to it.

  • blackbox
    blackbox almost 12 years
    Just want to mention that this is the java version... Scala version wouldn't be much different though.
  • biesior
    biesior almost 12 years
    @blackbox: You're absolutely right, nightograph didn't mention which version is he using, however these topics can be easily learned with Play's documentation and samples, for both - Java and Scala
  • Somatik
    Somatik almost 12 years
    I think he's looking for a way to render a html page in /public
  • Somatik
    Somatik almost 12 years
    sorry, somehow missed that, should I remove bad comments?
  • biesior
    biesior almost 12 years
    Well mistake is not a sin, as you wish, I think, that we can remove all comments that doesn't bring something new to the topic
  • nightograph
    nightograph almost 12 years
    Hey guys, I actually mentioned it in the question that I don't want the files in the public folder since they will be turned into templates in the future. Coming from play 1.2 I was kind of looking for the same functionality e.g renderTemplate("foo.html") - blackbox answer was sufficient. i was hoping there was less restriction for the naming
  • nightograph
    nightograph almost 12 years
    @somatik actually that's a interesting question too! is it possible to render a html file in the public folder without redirecting the url?