Java Reference
In-Depth Information
public void
init() {
if
(roleDao.read().isEmpty()) {
roleDao.save(
new
Role("User"));
roleDao.save(
new
Role("Administrator"));
roleDao.commit();
}
}
}
Stripersist finds
StripersistInit
implementations through the extension
packages mechanism or with the
StripersistInit.Classes
initialization para-
meter to
StripesFilter
in
web.xml
.
Next, we'll add a
roles
property to the
User
class. Users can have more
than one role, and more than one user can have the same role, so it's a
many-to-many relationship:
package
stripesbook.model;
@Entity
public class
User
extends
ModelBase {
private
String firstName;
private
String lastName;
private
String username;
private
String password;
@ManyToMany
private
List<Role> roles;
/
*
getters and setters...
*
/
@Override
public
String toString() {
return
String.format("%s %s", firstName, lastName);
}
}
The roles are ready to go.
A Page with Restricted Access
To manage assigning roles to users and demonstrate the use of roles
on the following page. The page shows the users with their roles and
allows us to change which users have which roles. Only administrators
are allowed to use this page. The application loads an administrator as
a starting point; when users register, they are initially given the
User
role.
Search WWH ::
Custom Search