Last time we wrote most of the JSP to display the list of topics and I introduced some of the Struts custom tags for looking up message resources, conditional processing and collection iteration. Without further delay, let’s write the code that makes each topic subject a hyperlink. As I mentioned before, each installment in this series is going to be much shorter from now on.
The Struts html:link tag is used to render a hyperlink, as the name suggests. The code below uses the tag’s forward attribute to specify the name of the global ActionForward that contains the URI of the hyperlink, in this case ViewTopic:
<tr> <td class="topics"> <html:link forward="ViewTopic" name="params"> <bean:write name="topic" property="subject" filter="true" /> </html:link> </td>
—I’ll explain the use of the name attribute in a moment. First of all, I want to explain what the filter attribute is doing in the bean:write tag. Quite simply, when set to true it replaces HTML reserved characters with their equivalent entities. Although the default value is true, I’ve included it within the code to make it explicit what’s going on.
Back to the task at hand. You may also recall from part one of the series that we want topic hyperlinks to be shown in the unvisited link colour when there are new replies to a topic. This means that the hyperlink needs to be subtly changed when there’s a new reply. Somehow we need to encode within the hyperlink not only the unique ID of the topic that we want to view, but also another query parameter indicating the number of replies to that topic.
The link tag lets us reference a JavaBean that contains a Map of query parameters, or has a property that itself contains such a Map. In our case, we can create such a bean and refer to it by using the name attribute within the link tag. The jsp:useBean tag is used outside the logic:iterate loop to create a page-scope bean called params that is a HashMap.
Within the loop, we have a two-line scriptlet that:
- Stores the number of replies to the topic in the HashMap under a key named r.
- Stores the post ID of the topic in the HashMap under a key named pid.
—Unfortunately I couldn’t work out how to eliminate the scriptlet code, but at least it’s only two lines. I’m sure it could be done using the JSTL, but I haven’t used that yet. Besides, this isn’t a JSTL tutorial. The complete code is shown below:
<jsp:useBean id="params" class="java.util.HashMap" scope="page" /> <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"> <% params.put("r", topic.getReplyCount()); params.put("pid", topic.getPostID()); %> <tr> <td class="topics"> <html:link forward="ViewTopic" name="params"> <bean:write name="topic" property="subject" filter="true" /> </html:link> </td> . . . </tr> </logic:iterate> </logic:notEmpty>
The actual hyperlinks end up looking like this:
<a href="/webforum/ViewTopic.do?pid=1&r=1">Some Topic</a> <a href="/webforum/ViewTopic.do?pid=2&r=18">Another Topic</a>
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. The files below are 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)
The application doesn’t do very much at the moment. Next time we’ll take a look at how we’re going to display an individual topic and its replies.