I’ve decided to make a couple of changes to this series. First off, I’ve had requests to make the full source code for the Web Forum application available for download. Initially I didn’t want to do this because I only wanted the relevant code to be associated with each installment. I’ve changed my mind because it’s going to save me a lot of time if I don’t have to strip out code every time I write an article. Secondly, I’ve decided to split the series up into much shorter, bite-size chunks. I’d wanted each article to cover a particular aspect of the application and Struts, but it’s just taking me too long to write this way. Hopefully it won’t seem too disjointed. I do have something planned for later that will bring it all together again, but now’s not the time to mention that. So without further ado, let’s press on.
Last time I explained the persistence and business object layers and we wrote the code to retrieve the list of topics from the database and represent them as objects. We used a Struts ActionMapping and an ActionForward to get as far as the JSP that will actually display the topics on the screen.
Displaying The Topics
Struts comes with a bean taglib that provides JavaBeans-related functionality, and an html taglib that renders common HTML attributes and JavaScript event handlers. Both are used within topics.jsp, as is the logic taglib mentioned earlier.
The following code fragment from topics.jsp illustrates the use of the bean:message tag, which is used to render an internationalised message based on a message key and locale. This means that the user interface string constants within the application can be separated out, rather than being hard-coded in the JSPs. These strings are looked up in a Java properties file specified using the message-resources tag in the Struts configuration file.
<bean:message key="app.doctype" /> <html:html locale="true" xhtml="true"> <head> <bean:message key="app.content.language" /> <bean:message key="app.content.type" /> <link rel="stylesheet" type="text/css" href="%3Fhtml:rewrite%2520page=.html"/assets/css/webforum.css" />" /> <title> <bean:message key="topics.title" /> - <bean:message key="app.title" /> </title> </head>
—This code uses bean:message tags to render the DOCTYPE declaration as well as the HTML Content-Type and Content-Language meta declarations. The html:html tag is used to render the HTML tag at the start of the document, using an XHTML format declaration and appropriate locale.
The declaration in the struts-config file for the properties file containing the string constants is:
<message-resources parameter="com.johntopley.webforum.view.Resources" null="false"/> <message-resources parameter="com.johntopley.webforum.view.GlobalErrors" null="false" key="GlobalErrors"/>
—This section must come after the <action-mappings> section. It tells Struts to expect to find all of the user interface string constants in the files Resources.properties and GlobalErrors.properties, and that both of these are located in the com.johntopley.webforum.view package.
The null attribute isn’t very intuitive, but setting it to false means that any errors caused by not being able to find a message resource within the properties file are displayed on the JSP, which is useful when debugging. I’ve also declared that I’ll be using a separate properties file for storing error string constants. The key attribute tells Struts that I want these to be stored in the HTTP request under the key GlobalErrors. Note that storing the error strings in a separate properties file to the rest of the user interface text is entirely optional. I do it because I like the extra separation.
The html:rewrite tag is used in the stylesheet link statement to generate the correct URL for the path to the stylesheet, which is then passed to the HTML link tag as normal.
Taking a look at the Resources.properties file, a convention I like to use is to prefix application-wide string constants with “app”. All other string constants I prefix with the name of the JSP in which they’re used. For example, these are the constants for the Topics page:
#– topics.jsp – topics.guest.welcome.1=Welcome Guest. topics.guest.welcome.2=Register topics.guest.welcome.3= or topics.guest.welcome.4=Log in topics.guest.welcome.5=to create a new topic. topics.heading.column1=Subject topics.heading.column2=Replies topics.heading.column3=Author topics.heading.column4=Posted topics.newtopic=New Topic topics.notopics=There are no topics to display. topics.table.summary=A list of topics and summary information about them topics.title=Topics topics.viewtopics=View Topics
Recall from last time that we have a Posts class stored in the HTTP request that contains an ordered collection of Post objects. We somehow need to loop through this collection and retrieve the relevant information from each Post object. Struts comes to our rescue with three tags within the logic taglib.
We use the iterate tag to iterate through the collection, but first of all we need to account for the fact that we might be dealing with an empty collection. In other words, there may not be any topics to display. The notEmpty tag and its opposite number, the empty tag, allow conditional processing based upon whether a particular variable is null or an empty String, Collection or Map object. Here’s the outline code from topics.jsp:
<logic:notEmpty name="com.johntopley.webforum.postlist" property="posts"> <logic:iterate id="topic" name="com.johntopley.webforum.postlist" type="com.johntopley.webforum.model.Post" property="posts" length="16"> . <%– There are posts, so display the details. –%> . </logic:iterate> </logic:notEmpty> <logic:empty name="com.johntopley.webforum.postlist" property="posts"> . <%– No posts, display a message. –%> . </logic:empty>
There’s quite a lot going on here. The name attribute used in the notEmpty, iterate and empty tags tells Struts the name of a JavaBean that holds the collection to be iterated over. In this case, it’s com.johntopley.webforum.postlist, which you may remember is the HTTP request key that we stored the Posts objects under in the ViewTopicsAction Action class. We used a bit of indirection there because we didn’t refer to the key directly, but instead by the POST_LIST_KEY public static variable in the KeyConstants class. By the way, notice how I prefix the key with the package name to help avoid namespace collisions within the HTTP request.
The property attribute is also used in all three tags. It tells Struts which property on the bean referred to by name contains the collection. The value for this attribute should be set to the name of the accessor (getter) method in the collection class, but without the “get”, or for boolean properties, without the “is”. Our Posts class has a getPosts method, so we simply set property to the value posts.
The id attribute in the iterate tag creates a local variable within the tag body that we can use to refer to the current row within the loop. I’ve called it topic. Think of it as being similar to the “i” variable within a for loop. I’ve also used the length attribute to specify that we only want to display the top sixteen topics.
Finally, the type attribute is the fully-qualified class name that we want to downcast the object representing each row to. This is important because without doing this cast we won’t be able to access the properties of each individual Post object within the collection.
Notice that the combination of the notEmpty and empty tags effectively allow use to create an if/else structure, but without having to resort to scriptlet code. It’s good practice to try to avoid scriptlets in JSPs, because they should only contain markup and not bare Java code. This conditional if/else processing pattern is repeated with other tags within the logic taglib.
Adding the body to the loop gives us the list of topics at last:
<logic:notEmpty name="com.johntopley.webforum.postlist" property="posts"> <logic:iterate id="topic" name="com.johntopley.webforum.postlist" type="com.johntopley.webforum.model.Post" property="posts" length="16"> <tr> <td class="topics"> <bean:write name="topic" property="subject" filter="true" /> </td> <td class="replies"> <bean:write name="topic" property="replyCount" /> </td> <td class="author"> <bean:write name="topic" property="author" /> </td> <td class="posted"> <bean:write name="topic" property="timestamp" formatKey="app.date.format" /> </td> </tr> </logic:iterate> </logic:notEmpty> <logic:empty name="com.johntopley.webforum.postlist" property="posts"> <tr> <td class="topics"> <bean:message key="topics.notopics" /> </td> <td class="replies"></td> <td class="author"></td> <td class="posted"></td> </tr> </logic:empty>
—The most important point to note here is the use of the bean:write tags to render the contents of the JavaBean referred to by that tag’s name attribute. What joins it all together is that we set the value of this attribute to the topic bean provided by the iterate tag. And because we’ve cast the objects within the collection to the correct type, the write tag can access the properties of each Post object using the JavaBeans convention explained earlier.
Source Code Downloads
To reduce the download sizes, I’ve split the Struts JAR files that go into WEB-INF/lib into a separate download. As stated earlier, I’m now providing the full source code for the application, in both JDeveloper and non-JDeveloper form.
- Download struts_webforum_jars.zip (855 KB)
- Download struts_webforum_jdev.zip (55 KB)
- Download struts_webforum.zip (50.9 KB)
Next Time
It still seemed like quite a large bite, at least to me writing it! The important thing is that it didn’t take me several days to get there. Next time we’ll finally get around to adding the hyperlinks to the listed topics that allow the user to click on a topic to view it.
Comments
There are 4 comments on this post. Comments are closed.
Great article. Are you going to cover using JSTL tags instead of the traditional Struts tags for drawing the page? (i.e instead of using the use
Thanks. Not this time around because a) the application is already written and doesn't use JSTL and b) I've not used JSTL myself yet. I do need to take a look at JSTL (it's a question of finding the time) and the Web Forum application would be a good vehicle for me to do that. So I guess I'm saying that I haven't ruled out writing about using JSTL with this app in the future. I'll keep it in mind.
Nice article but... In order to work I needed to change this: * remove page session="false" in jsp's in order not to loose the user saved in session * add resource-ref of datasource in web.xml * in topic.jsp cast to (String)subject and (String)postcount * in BaseDAO.java change JNDI_DATASOURCE = "java:comp/env/jdbc/WebForumDS" (I'm using tomcat-5.0.24
Interesting, thanks Ruben. I've not tested it using Tomcat. It works as-is using Oracle JDeveloper/OC4J.