Tutorials : Coding your second Jakarta Struts Application :

Multiple submits

On the detail page (detail.jsp) there are 4 different actions, all implemented through submit-buttons. This leaves us with a problem since only one action is specified in the <html:form> tag. In order for us to distinguish the 4 actions from each other I've added an onclick event to each button, for example:

onclick="go('update')"

where "go" is a JavaScript function that inserts the parameter in a hidden form field called "action":

function go(action) {
  document.forms[0].action.value=action;
}

The action used on the detail page is called "process" and its Action class simply acts as a relay to the proper command:

// extract the value of the hidden "action" field
return (mapping.findForward(request.getParameter("action")));

Since there are 4 possible action-values we must also define 4 forwards for the "process" action in the struts-config.xml file.

The struts-config.xml file

The global-forwards section

This section, which I did not mention in my first article, is used to define the "forwards" that are global to the application. Obvious candidates are of course "system error"-pages, which may be used by many actions, but in the DVD application I've also defined global forwards to the Detail and the List pages. These should only be used where there's no doubt about how the flow from one page to another should be. The "list" action is an example. It must go to the List page.

<global-forwards>
    <forward name="error" path="/error.jsp"/>
    <forward name="list" path="/list.jsp"/>
    <forward name="detail" path="/detail.jsp"/>
    
</global-forwards>

The form-beans section

This section only contains the DetailForm class:

<form-beans>

  <form-bean name="detailForm"
             type="hansen.playground.DetailForm"/>

</form-beans>

The action-mappings section

This section contains the 7 actions--plus the "process" action. Click here to display the section in a separate window.

<action-mappings>

    <action   path="/list"
              type="hansen.playground.ListDVDAction"
              parameter="d:\\dvds.xml">
    </action>

    <action   path="/detail"
              type="hansen.playground.DetailDVDAction"
              name="detailForm"
              validate="false"
              scope="request">
    </action>

    <action   path="/save"
              type="hansen.playground.SaveDVDAction">
    </action>

    <action   path="/process"
              type="hansen.playground.ProcessDVDAction"
              name="detailForm"
              scope="request"
              validate="false">
        <forward name="create" path="/create.do"/>          
        <forward name="update" path="/update.do"/>          
        <forward name="delete" path="/delete.do"/>          
        <forward name="cancel" path="/cancel.do"/>          
    </action>

    <action   path="/create"
              type="hansen.playground.CreateDVDAction"
              name="detailForm"
              input="/detail.jsp"
              scope="request">
        <forward name="OK" path="/detail.jsp"/>          
    </action>

    <action   path="/update"
              type="hansen.playground.UpdateDVDAction"
              name="detailForm"
              input="/detail.jsp"
              scope="request">
       <forward name="OK" path="/detail.jsp"/>          
    </action>

    <action   path="/delete"
              type="hansen.playground.DeleteDVDAction"
              name="detailForm"
              scope="request"
              validate="false">
        <forward name="OK" path="/list.jsp"/>          
    </action>

    <action   path="/cancel"
              forward="/list.jsp">
</action>

Some comments to these definitions:

action

comments

list

The "parameter" keyword can be used to transfer data to the Action class. In this example we give the name of the xml file to read.
The Action class may retrieve the value like this:
String filename = mapping.getParameter();

detail

The "DetailDVDAction" class fills data in the "DetailForm" instance for detail.jsp to display.
The detail command is given as a hyperlink:
<a href="detail.do?index=<%=i%>">
An "index" member variable is therefore added to the "DetailForm" class.
The hyperlink acts precisely as a form submit with method=get.

save

Is implemented without a submit. A button with an onclick event is used to invoke the save action.

process

Since the process action is defined in an HTML form we need an ActionForm. The ProcessDVDAction does not use it however.

create and update

"validate" defaults to "true". Note that by defining the "forward" with the action we can change the target page without having to recompile the Action class.

cancel

Since "cancel" only directs the browser to the List page we use "forward" instead of "type" to avoid defining an Action class.

Here's the complete struts-config.xml file.

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.