This post originated from an RSS feed registered with Java Buzz
by John Topley.
Original Post: Jakarta Struts De-Mystified Part 4
Feed Title: John Topley's Weblog
Feed URL: http://johntopley.com/posts.atom
Feed Description: John Topley's Weblog - some articles on Ruby on Rails development.
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:
—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:
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.