Java Reference
In-Depth Information
The model wrapper for ShowBoard
First, let's examine the changes to
PostListCommand
and
PostListController
.
We renamed the class to
ShowBoardCommand
because it is now limited to a sin-
gle board. Here are the changes:
private static final int NUMBER_COLUMN = 4;
protected Vector number = new Vector();
public String getNumber(int index)
throws
IndexOutOfBoundsException,
ArrayIndexOutOfBoundsException {
return (String) number.elementAt(index);
}
The command now supports
getNumber
so that the post number can be used
when we make the subject line linkable. In our database and in most bulletin
boards, the
post.number
attribute is the
key
on the table posts, meaning that it
alone is enough to uniquely identify a database row. To list all replies, we'll
look for all posts with the parent set to this number. To simplify the imple-
mentation, we'll pass this number field through in the link
URL
.
In
execute
, here are the changes (in bold):
result =
statement.executeQuery(
"SELECT subject, author, board, number from posts
where board = '"
+ getBoard() + "' and parent=0"
);
while (result.next()) {
subject.addElement(result.getString(SUBJECT_COLUMN));
author.addElement(result.getString(AUTHOR_COLUMN));
number.addElement(result.getString(NUMBER_COLUMN));
}
Our query is slightly different, because we are searching on a single board and
looking only for the top-level posts where there is no parent (
parent=0
). We
also had to add the code to populate the number vector. The rest of the com-
mand is the same.
The controller for ShowBoard
The controller is very similar. We changed only the names of the controller,
the command, and the return
JSP
. Here is the meat of
performTask
:
String board=request.getParameter ("board");
ShowBoardCommand postList = new ShowBoardCommand();
postList.setBoard(board);
postList.initialize();
postList.execute();
request.setAttribute("ShowBoardCommand", postList);