-
Notifications
You must be signed in to change notification settings - Fork 0
JavaGuide4
We can now display a dashboard, but before we can go on to allow users to create, work on and assign tasks, we need a way for users to identify themselves.
To start off, let's implement a screen that allows a user to login. Create a new route in conf/routes for the login
screen:
GET /login controllers.Application.login()
And now add the login action to app/controllers/Application.java:
public static Result login() {
return ok(
login.render()
);
}In our action we have referred to a new login template, let's write a skeleton for that template now, in
app/views/login.scala.html:
<html>
<head>
<title>Zentasks</title>
<link rel="shortcut icon" type="image/png" href="@routes.Assets.at("images/favicon.png")">
<link rel="stylesheet" type="text/css" media="screen" href="@routes.Assets.at("stylesheets/login.css")">
</head>
<body>
<header>
<a href="@routes.Application.index" id="logo"><span>Zen</span>tasks</a>
</header>
</body>
</html>Now visit <http://localhost:9000/login> in your browser to check that our route is working. Apart from the title, the
page should be blank.
Our login page needs to contain a form, which will of course, hold an email address (username) and password.
Play provides a forms API for handling the rendering, decoding and validation of forms. Let's start off by implementing
our form as a Java object. Open the app/controllers/Application.java class, and declare a static inner class called Login at the end of it:
public static class Login {
public String email;
public String password;
}Now we need to pass this form into our template, to render. Modify the login method in
app/controllers/Application.java to pass this form to the template:
public static Result login() {
return ok(
login.render(form(Login.class))
);
}And now declare the form as a parameter for the login template to accept, in app/views/login.scala.html:
@(form: Form[Application.Login]
<html>
...Now we need to render our form. Add the form to the login template:
@helper.form(routes.Application.authenticate) {
<h1>Sign in</h1>
<p>
<input type="email" name="email" placeholder="Email" value="@form("email").value">
</p>
<p>
<input type="password" name="password" placeholder="Password">
</p>
<p>
<button type="submit">Login</button>
</p>
}The important thing to notice here is the @helper.form call. We have passed to it a route,
routes.Application.authenticate. This tells Play where this form should be submitted to. Notice that there are no
hard coded URLs here? This means we can change our URL structure without silently breaking our application.
Of course, the authenticate route hasn't been implemented yet, so let's implement it now. First of all, add the route
to conf/routes:
POST /login controllers.Application.authenticate()
Now implement the method in app/controllers/Application.java:
public static Result authenticate() {
Form<Login> loginForm = form(Login.class).bindFromRequest();
return ok();
}Make sure you add an import statement for
play.data.*toApplication.java
Play has a
- Starting up the project
- A first iteration for the data model
- Building the first screen
- Adding authentication
- Adding some AJAX actions
- Invoking actions from Javascript
- HTTP programming
- Asynchronous HTTP programming
- The template engine
- HTTP form submission and validation
- Working with JSON
- Working with XML
- Handling file upload
- Accessing an SQL database
- Using the Cache
- Calling web services
- Integrating with Akka
- Internationalization
- The application Global object
- Testing your application