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%3Fpid=1&r=1.html">Some Topic</a> <a href="../../../../webforum/ViewTopic.do%3Fpid=2&r=18.html">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)
Next Time
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.
Comments
There are 12 comments on this post. Comments are closed.
Do you think it's time to learn struts ??? You'd better get on with JSF. IMHO it's far better, and is supported by many companies. More functional, no unneeded complexity. Better error handling. That's the way to go.
If you were to jump on every new bandwagon then it wouldn't be possible to get anything done. Struts is established, whereas Faces is still quite new. Besides, they're not mutually exclusive.
no need for the scriptlet
You can't just say that, Ron! You have to show the alternative way of doing it...
The scriptlet code can be replaced by following code Remember to include following taglib declaration in your jsp and mapping taglib entry in web.xml.
What code's that, Manish? Don't be shy!
Hi John! Thank you for your nice initiative! But when would shox the code for listing a topic with its replies?
The code is in the zip file. As for the article...read my home page.
I am really sorry to bother you. When I tried to work with your application, I was not able to list a specific topic with its replies. Really sorry for bothering you, but you guess I am not that good at programming with this stuff! Can you please check if it is working? If not can you email me how to get it to work. Thanks a lot anyway! You are a nice guy :)
Here is the excat exception I got java.lang.NullPointerException at com.johntopley.webforum.data.PostsDAO.getReplies(PostsDAO.java:238) at com.johntopley.webforum.controller.action.ViewTopicAction.execute(ViewTopicAction.java:52) hey! really nice idea to print exceptions in error.jsp
Hi John, I have implemented the web forum on my server and it can be accessed from my Website. The only problem I had was to establish the JNDI connection pool in tomcat's server.xml It has permitted me to learn about Struts. I had already spent a few months learning JSF and it has helped me a great deal with Struts. Regards.
To omit the scriptlet I imagine you can use EL (Expression Language) as provided in JSP 2.0. Note that to use EL in JSTL tags, you need JSTL 1.1.