Java Reference
In-Depth Information
Since everything revolves around the user, it's also convenient to have
a getUser ( ) method in BaseActionBean that returns the current user:
Download email_23/src/stripesbook/action/BaseActionBean.java
protected User getUser() {
return getContext().getUser();
}
Since the session is used to remember the current user, the method
delegates to the action bean context, MyActionBeanContext , which stores
the user ID in the session and retrieves the User object with help from
the UserDao :
Download email_23/src/stripesbook/ext/MyActionBeanContext.java
private UserDao userDao = new UserDaoImpl();
public void setUser(User user) {
setCurrent(USER, user.getId());
}
public User getUser() {
Integer userId = getCurrent(USER, null );
return userDao.read(userId);
}
Action beans now have easy access to the current user as well as all
the DAOs. For example, here's how the ContactFormActionBean saves a
contact:
Download email_23/src/stripesbook/action/ContactFormActionBean.java
public Resolution save() {
Contact contact = getContact();
contact.setUser(getUser());
contactDao.save(contact);
contactDao.commit();
getContext().getMessages().add(
getLocalizableMessage("contactSaved", contact)
);
return new RedirectResolution(ContactListActionBean. class );
}
Notice the call to commit ( ) after saving the contact. Commits are done
in action beans (or wherever the DAOs are used), not in the DAO s them-
selves, so that multiple objects can be created, updated, or deleted
within a single transaction.
The webmail application is now using a real database. Our model has a
few annotations, our DAOs are simple, and we're not tied to any specific
JPA implementation. Life is good.
 
 
Search WWH ::




Custom Search