Tutorials : Development Standards in Apache Struts :

Adding Validation

Struts offers a validator package to simplify the validation of the controls in a form. For the Detail page, it would be natural to check for a DVD title and runtime length when updating or creating a DVD. If validation fails, the flow must go back to the Detail page. This is easily coded in struts-config using the input-parameter:
<action path="/updateDVD"
    type="keld.playground.UpdateDVDAction"
    name="detailForm"
    input="/detailDVD.do"
    validate="true"
    scope="request">
  <forward name="OK" path="/detailDVD.do"/> 
</action>
The problem is that when you hit the Detail page action this time, there is no key on the request! But you really don't need the key, because Struts has already placed the form bean you need on the request. So, the page action may safely skip form bean population. It must, however, still retrieve the actors' information (keys and names), which is needed for the actor drop down.

Adding a New Page

To see if your "architecture" is waterproof, end by adding yet another action. From the Detail page, it should also be possible to add new actors:

Clicking the "New Actor" button will lead us to this page:

This brings up a challenge: what if the user has entered some data in the Details form, and then clicks "New Actor"? If you don't do something, you'll lose the data. A straightforward solution is to save the form bean in session scope, and when we return to the Details page we simply fill the form bean from the session bean.

If you make a modification to the Detail page action, so it looks for a form bean on the session, you can now safely flow back from the New Actor page to the Detail page. To preserve memory space, it's a good idea to null the bean saved in the session when it's no longer of any use.

The most significant flows are shown on this figure:

Note that most of the advice given can be seen on this figure: the Page and Relay actions, chaining the actions, and placing data on the request.

Tracing the Action Classes

Because you chain many action classes, it's very important to be able to see if this actually works. The trace mentioned in the beginning of the article helps to test this. In the box below, you can see the trace observed when the application flows from the List page to the Detail page and further on to the New Actor page:
Entering action: /listDVD (class=keld.playground.ListDVDAction)
Exiting action : /listDVD (forward=list, path=/list.jsp)
Entering action: /listDVDGotoDetail (class=keld.playground.ListDVDGotoDetailAction)
Exiting action : /listDVDGotoDetail (forward=OK, path=/detailDVD.do)
Entering action: /detailDVD (class=keld.playground.DetailDVDAction)
Exiting action : /detailDVD (forward=detail, path=/detail.jsp)
Entering action: /detailDVDRelay (class=keld.playground.DetailDVDRelayAction)
Exiting action : /detailDVDRelay (forward=newactor, path=/detailDVDNewActor.do)
Entering action: /detailDVDNewActor (class=keld.playground.DetailDVDNewActorAction)
Exiting action : /detailDVDNewActor (forward=OK, path=/newActor.do)
Entering action: /newActor (class=keld.playground.NewActorAction)
Exiting action : /newActor (forward=OK, path=/newactor.jsp)

A Utility for Debugging .jsp Pages

When developing Struts applications, it's extremely important to keep track of the objects placed in the request and session objects. When I make a new .jsp page, I often place some code at the top of the page that'll list all contents in these objects. For completeness, I also include all parameters passed. Here's what you see in the browser when you enter the Detail page:

(The picture has been cut to the right to fit the page).

If you also find this useful, see the code that prints the overview here.

Makes Your Life Easier

For larger applications, it's important to standardize the implementation of the Struts architecture. This will lead to a more robust and maintainable application. This article has shown you some examples of situations where you can implement standards, but there are many more. For your own project, you'll want to look for other areas where standards may also be appropriate. And don't forget to standardize where it's really easy to do it—for example defining naming standards for classes, packages, actions, forward names, etc.

Happy coding!

Related Resources

How to Add Java Applets to Your Site

New on the Java Boutique:

New Review:

Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling API boasts simplicity, ease-of-integration, a well-rounded feature set, and it's free!

New Applet:

Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA sequences into three useful formats.

Elsewhere on internet.com:

WebDeveloper Java
Lots of Java information on webdeveloper.com

WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.

ScriptSearch Java
Hundreds of free Java code files to download.

jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.