Java Reference
In-Depth Information
We'll call this interface
ContactDao
:
package
stripesbook.dao;
public interface
ContactDao {
public
List<Contact> read();
public
Contact read(Integer id);
public void
save(Contact contact);
public void
delete(Integer id);
}
You can retrieve all contacts in a list, or you can retrieve a single con-
tact by ID. The
save
( ) method combines both creating and updating a
contact. This way, you don't need to worry about whether a contact is
new or existing. Just call
save
( ) and let the DAO figure it out. Finally,
you can delete a contact by ID.
Now we need an implementation of
ContactDao
. I don't want to bog you
down with the details of setting up a database, a JDBC abstraction
layer, and so on. So to keep things simple, I wrote a
MockContactDao
class that manages objects in memory using plain Java data structures.
The code is not important (you can always go check it out if you're
curious). All you need to know here is that
MockContactDao.getInstance()
gives you a working implementation of
ContactDao
.
2
We're done with the model and data access layers. Now it's time to
return to Stripes and write some code that supports the action beans
and JSPs that we'll be adding as we build the webmail application.
3.2
Writing a Base for a Stripes Application
Started, we wrote a very simple application just to get your feet wet. But
when you write more complex Stripes applications with several action
beans and JSPs, you'll want a base of reusable code: a base class for
the action beans, a JSP for the tag libraries, and a JSP for a common
page layout. You need to write this code only once, and it's worth the
trouble: you'll benefit each time you add an action bean or a JSP, as well
as when you need to add or modify behavior for the whole application.
Search WWH ::
Custom Search