[ Team LiB ] 5.1 The Beans Generally the first step of any large project, data modeling, consists of deciding what data the system will need to maintain, how this data will be represented, and how it interrelates. Traditionally, such modeling takes place in the context of a database, discussed in Chapter 6. For current purposes, however, it is reasonable to model everything in terms of beans. As there is not yet anywhere to store all the data, the examples in this chapter use beans in which the data has been hard-coded, although this is never a good idea in real-world projects. Even when prototyping a system, it is better to use a small, simple database. However, this little cheat will not significantly change the way the pages work, so it will suffice for now. Java News Today has already identified a few beans it will need. In Chapter 3, JNT developed the QuizBean, which holds the question, options, and correct answer for the daily quiz, and created a UserInfoBean to hold users’ preferences for colors, as well as a name. At this time, JNT is ready to consider allowing users to register on the site, in order to store their preferences permanently. This will necessitate adding some logic to the UserInfoBean in order to handle logging users on the system. The fields added will be username, password, and isLoggedIn, a flag that will be true if the user is currently logged in and false otherwise. In addition to users and quizzes, the other major entities behind the JNT site are articles. An ArticleBean will hold the text of the story, a headline, and a date and time of publication. Each ArticleBean will also have a unique numeric identifier to identify and load that story. As with a physical newspaper, articles will be grouped into major sections covering broad categories, such as J2EE, the Java community, related technologies, and so on. Each section will have a name and a description and will also keep track of all the articles it contains. This containment will be managed by creating a SectionBean and giving each instance of SectionBean an array of ArticleBean instances. This should immediately suggest the use of the jsp:forEach tag to display all the stories in a section, and indeed such a page will be on the site. This illustrates how the data-modeling phase of a project can inspire and affect the page-designing phase. Similarly, sections will be grouped into an edition. At the moment, an edition will have only an array of sections; later, its role will be expanded to manage many of the personalization options Java News Today will offer. Finally, in order to make this a community site, the staff at Java News Today would like to allow users to add comments to stories. Some sites, notably http://www.slashdot.org, provide very sophisticated commenting systems that can include threaded discussions, moderation of comments, and a whole host of other features. For the moment, JNT will allow only a simple “flat” commenting system, whereby comments simply appear in the reverse order they were added. This suggests the need for a CommentBean and an array of such beans held by each ArticleBean. Figure 5.1 shows a sample of beans as they might exist in memory and their relationships to one another. Figure 5.1. The JNT beans. Page 90
Note: If you are looking for cheap and inexpensive provider to host and run your tomcat application check Actions tomcat hosting services