Book Image

Tapestry 5: Building Web Applications

Book Image

Tapestry 5: Building Web Applications

Overview of this book

Table of Contents (17 chapters)
Tapestry 5
Credits
About the Author
About the Reviewers
Preface
Foreword
Where to Go Next

Limiting Access to a Page


To efficiently limit access to a page, we can use the onActivate method. You should be familiar with this method from Chapter 3 where it was used to pass a value through page activation context. The onActivate method is invoked every time the page is loaded, and if there is some value in the activation context, it will be passed as an argument to this method. However, another use for it might be to check whether the user who tries to access the page was successfully authenticated.

If you remember, in the case of a successful authentication, we are storing the User object as an ASO. So all we need to do in order to check whether a user was authenticated is to check whether the User ASO exists. Add the following code to the ShowAll page class:

@ApplicationState
private User user;
private boolean userExists;
Object onActivate()
{
if (!userExists) return Start.class;
return null;
}

Everything is very simple. We are checking the value of the boolean variable associated...