Database Reference
In-Depth Information
Login Form
The HTML required for the user login form is shown in Listing 12-19 and can be found in the
{PROJECTROOT}/
WebContent/mustache/html/global/base-home.html
layout file. Again, an important item to note in the form is that
the
bean name
and
property
are used to specify what is passed to controller and, subsequently, to the service layer
for querying the database.
Listing 12-19.
The
login
Form
<form class="navbar-form navbar-right" action="/login" role="form" method="post">
<div class="form-group">
<input type="text" placeholder="Username" name="
user.username
" class="form-control">
</div>
<button type="submit" class="btn btn-success">Sign in</button>
</form>
Login Action
The
LoginAction
controller is used to handle the flow of the login process, as shown in Listing 12-20. Inside
LoginAction
, use the
checkLogin
method to check if the user exists in the database.
If the user was found during the login attempt, a cookie is added to the response and the request is redirected
via
redirectAction to
the social home page, shown in Figure
12-7
. Otherwise, the route specifies the HTML page to
return and adds the error messages that need to be displayed back to the view.
Listing 12-20.
The
checkLogin
Method in the
LoginAction
Controller
@Action(value = "login",
results = {
@Result(name = "success", type = "redirectAction", params = { "actionName",
"social", "namespace", "" }),
@Result(name = "loginfail", type = "mustache", location =
"/mustache/html/home/message.html")
})
public String checkLogin() {
try {
graphStory = graphStoryDAO.getUserDAO().login(graphStory);
if (noGraphStoryErrors()) {
response.addCookie(addCookie(GraphStoryConstants.graphstoryUserAuthKey,
graphStory.getUser().getUsername()));
return SUCCESS;
} else {
setTitle("Login Failed");
return "loginfail";
}
}