John Topley’s Weblog

Jakarta Struts Demystified Part 1

Just over a year ago I started to learn how to use the Jakarta Struts J2EE web application framework. During this journey it became apparent to me that there were some aspects of Struts that I found confusing at first, and that many other developers were going through the same difficulties. This series of articles is an attempt to address this problem.

There are now many good books on the framework and the online documentation is quite mature for an open-source project; but in spite of this, I have found myself having to refer to lots of resources to build a good understanding of Struts. Moreover, there are still some areas where the documentation is sparse. However, this occasional series isn't an attempt to provide a definitive source of documentation; I don't claim that my knowledge of Struts is at the level needed to be able to produce that. Rather, it's intended to allow newcomers to Struts to hit the ground running and to get a feel for what the framework is like and what it can and can't do.

The Struts example applications I have seen, I have found to be too trivial or too complex, with the complex ones having lots of external dependencies that have to be resolved before the example can even be compiled and run. With my example I'm attempting to hit a sweet spot between the two extremes, and I've deliberately created an application that has the minimum of external dependencies, so you should be able to actually run the code without too much difficulty! Everything you'll need is open-source and can be downloaded for free—one of the advantages of J2EE.

The example application that we're going to be building is an online discussion forum—which I've given the rather generic name of “Web Forum”. I've chosen an application of this type because it should be familiar and because you can create quite a lot of functionality without the complexity getting out of hand. It's fully functional, reasonably well-debugged and hopefully it should be fun learning how it all fits together.

It's important to state at the start that this code shouldn't be used as an example of a production-quality J2EE system. In particular, the exception handling is extremely and deliberately simplistic. This isn't a tutorial on persistence strategies, OO design or web application GUI design, it's simply a vehicle for learning Struts. There are doubtless aspects of what I've created that could be improved upon, but that's not important right now. So without further ado, let's crack on!


Ingredients

I'm going to start by talking about the ingredients you'll need to create the application. I developed it using Oracle JDeveloper 9.0.3 because that's the IDE I'm familiar with from my day job. You can use your tool of choice for writing the code. Note that throughout this series I'll be providing the source code as a download in both JDeveloper and non-JDeveloper form. The JDeveloper version will simply include JDeveloper's workspace and project files, so the project can be opened quickly and easily within that IDE.

This is a list of what else you will need:

*I use the embedded OC4J server that comes with JDeveloper. I also use the MySQL Control Centre for creating the database and controlling the database server, although the MySQL command line can be used if preferred.


The Application

I'm now going to give a brief tour of the Web Forum's functionality. Some of this I'd thought about before I wrote a line of code, other aspects become apparent as I was building it. It helped that there are plently of existing forums to serve as inspiration!

Upon starting the application, the user is presented with a list of topics and information about each topic, such as the date and time it was posted, the number of replies and the topic's author. The most recent sixteen topics are displayed, ordered from the newest to the oldest:

The Topics screen. Click to enlarge.
A picture of the Topics screen

At this point the user can click on a topic to view it and any follow-up replies:

The Topic screen. Click to enlarge.
A picture of the Topic screen

The date and time of the post are displayed, as well as the author's name, the date they registered with the system and the number of posts they've made. The user can register if they are a new user, or log in if they are an existing user. A user must be logged in before they can create a new topic or a new reply. They can elect to automatically log in during registration or at the Login screen:

The Login screen. Click to enlarge.
A picture of the Login screen

The auto-log in feature uses a cookie that expires after thirty days. During registration they must provide a unique user name, their real name (or screen alias) and a password. All of these fields are mandatory apart from surname and they must confirm their chosen password by entering it twice:

The Registration screen. Click to enlarge.
A picture of the Registration screen

After they have successfully registered they are taken back to the topics screen where they can create a new topic:

The New Topic screen. Click to enlarge.
A picture of the New Topic screen

Both the subject and message fields are mandatory. They are allowed to use the HTML <b>, <i> or <u> tags for basic text variations (bold, italic or underline) in the message field. Once they have created a new topic they are taken back to the Topics screen. The hyperlink to a topic contains an encoding of the number of replies to that topic, which ensures that the user can see which topics they have read, because the hyperlinks to those topics are displayed using the browser's visited link colour. Therefore, new replies to a topic cause that topic's hyperlink to be displayed using the browser's unvisited colour. Thanks to Joel Spolsky for this brilliantly simple idea.

When viewing a topic they can compose a new reply and again, the message field is mandatory and accepts basic formatting tags as before:

The New Reply screen. Click to enlarge.
A picture of the New Reply screen

Next Time

This article defined the boundaries within which this tutorial will operate and introduced the example application that we'll end up creating. Next time I'll introduce the data model and the class model that do the work behind the scenes, and we'll get practical and write the code to display the list of topics.

Comments

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

  • avatar fred
    24 June 2004 at 14:46

    Cool, good idea and good example. Hope you keep up with it.

  • avatar John Topley
    24 June 2004 at 18:42

    Fred, I will keep up with it because it's something I've been planning to do for a long time and I've just finished the application after several weeks of part-time effort. All I need to do now is write the articles, which takes just as long!

  • avatar shay
    28 June 2004 at 15:15

    You might want to try out the application and see how it looks in JDeveloper 10g. The Struts page flow diagram in there should be very helpful for people to get up to speed with Struts.

  • avatar John Topley
    28 June 2004 at 19:19

    Shay, I don't have access to JDeveloper 10g at home. Also, I don't want this to be a JDeveloper-specific tutorial and I believe that people should understand what the code means before they get involved with visual tools.

  • avatar Duncan Mills
    30 June 2004 at 09:19

    I guess Shay's point is not relating to the visual stuff so much as the (possible) use of the ADF framework to simplify the Data Binding side of the Struts integration - I look forward to the next installment...

  • avatar John Topley
    30 June 2004 at 21:00

    Unfortunately I haven't had an opportunity to look at ADF yet. However, the point still stands - this is purely going to be a Struts tutorial. I forgot to say, the installments aren't going to be sequential, because that would be boring for those who've never heard of Struts, and because each article is going to take a while to write...

  • avatar Brett
    14 July 2004 at 09:59

    Good work. Any idea when the next one is coming out? Also, can anyone recommend to me a good book for learning the ins and outs of struts (or for that matter other web tutorials).

  • avatar John Topley
    14 July 2004 at 17:34

    Thanks Brett. The next article in the series should come out very soon, in fact, I'm working on it at the moment! I found that I had to refer to several books because each covers slightly different ground. The most recent one I've seen is the O'Reilly pocket reference and that's excellent.

The example application that we're going to be building is an online discussion forum.


Archives

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

More Archives


Sign In