John Topley’s Weblog

Jakarta Struts Demystified Part 4

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:

  <td class="topics">
    <html:link forward="ViewTopic" name="params">
      <bean:write name="topic" property="subject" filter="true" />

—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());
      <td class="topics">
        <html:link forward="ViewTopic" name="params">
          <bean:write name="topic" property="subject" filter="true" />

The actual hyperlinks end up looking like this:

<a href="../../../../webforum/">Some Topic</a>
<a href="../../../../webforum/">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.

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.


There are 12 comments on this post. Comments are closed.

  • avatar vachor
    16 October 2004 at 23:16

    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.

  • avatar John Topley
    17 October 2004 at 17:05

    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.

  • avatar Ron
    18 October 2004 at 17:18

    no need for the scriptlet

  • avatar John Topley
    19 October 2004 at 08:38

    You can't just say that, Ron! You have to show the alternative way of doing it...

  • avatar Manish Gupta
    05 May 2005 at 06:36

    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.

  • avatar John Topley
    05 May 2005 at 19:16

    What code's that, Manish? Don't be shy!

  • avatar soufiane
    13 July 2005 at 22:40

    Hi John! Thank you for your nice initiative! But when would shox the code for listing a topic with its replies?

  • avatar John Topley
    14 July 2005 at 07:34

    The code is in the zip file. As for the my home page.

  • avatar soufiane
    14 July 2005 at 14:44

    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 :)

  • avatar soufiane
    14 July 2005 at 15:07

    Here is the excat exception I got java.lang.NullPointerException at at com.johntopley.webforum.controller.action.ViewTopicAction.execute( hey! really nice idea to print exceptions in error.jsp

  • avatar Pierre
    24 October 2005 at 19:31

    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.

  • avatar Rich
    23 February 2006 at 20:20

    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.

Unfortunately I couldn’t work out how to eliminate the scriptlet code, but at least it’s only two lines.


  • Jan
  • Feb
  • Mar
  • Apr
  • May
  • Jun
  • Jul
  • Aug
  • Sep
  • Oct
  • Nov
  • Dec
  • 2019
  • 2018
  • 2017
  • 2016
  • 2015
  • 2014

More Archives

Sign In