advertisement
javaboutique
Search Tips
Articles  |   Tutorials  |   Reviews  |   Tools  |   by Category  |   by Date  |   by Name  |   Submit  |   Source  |   Forums  |  
javaboutique
Browse DevX


Partners & Affiliates











advertisement

Tutorials : Development Standards in Apache Struts :

Pages With and Without Forms

The purpose of Web applications is to show pages in a browser. Pages can be divided into two groups: either they contain one or more "forms" or they are form-less. A form contains "controls"—like text input fields, drop downs, checkboxes, and buttons. The form-less pages are, by far, the most simple. As a programmer, you only need to put your data in one or more beans, or collection of beans, and then use the bean:write tag to show this data. More interesting are the form pages, and the rest of the article will give advice on techniques and standards for coding these pages. For simplicity, assume that you only have one form per page. It's not difficult to extend the techniques to handle more than one form.

Every form must have an accompanying "form bean." Think of this bean as a buffer between the action classes and the browser. To get the data entered in the form, the action class retrieves it from the form bean. To place data in the form's controls, Struts gives you the option of using any bean you like, so this gives us room for:

Rule #1: Data that must be shown in a form control should be made available through the form bean.

This makes the scenario "symmetric"—data is entered and retrieved from the same source. A common situation in applications is a page flow from one page to the same page. Using Rule #1, the data is not lost from the form during the flow.

Here's an example that shows how to place and retrieve data from the form. To show the text "JAVA" in a text field called "language", you code something like this in the action class:

// Using real form beans
DetailForm df = (DetailForm)form;
df.setLanguage("JAVA");
- - -
// Using dynamic form beans:
DynaValidatorForm df = (DynaValidatorForm) form;
df.set("language", "JAVA");
In the .jsp page, write:
<html:text property="language"/>
Since you take the language value from the form bean, you don't have to use the "name" parameter of the html:text tag.

Keep the form bean in request scope if possible. This saves memory space and helps to avoid name clashes in the request namespace. So the purpose of Rule #1 is simplicity and consistency.

Now, take a look at the action class that invokes the .jsp page. It should be considered a special action. It is responsible for:

  • Checking "preconditions": Are you able to show the data you want on the page?
  • Copying data to the form bean.
  • Storing other data (in the request or the session objects) to be used by the .jsp page. This could be read-only data or the data that appears in drop downs.
This kind of action class will be called a "Page Action."

Rule #2: Make a "Page Action" for every page.

Use time to make it clear what the preconditions are and note them in the source code (and other documentation). A page action that shows "dynamic data," for example a specific customer's data, will need the customer key or id passed to it in some way.

Rule #3: Use the request object to pass data to a page action.

Again, this advice is to keep things simple and to save memory space. Every Java class working in the servlet environment can place data on the request object, so it's a general technique. Often you may want to pass data through parameters, for example in the URL, but you should remember that there is no easy way to create new parameters on the request.

All this advice gives you solid control over how pages are built, but what about submission of forms? This is also an area that can benefit from implementing standards. With a standard Struts setup, you submit the form to the class mentioned in the html:form's action parameter. If the page only offers one type of functionality, then it's simple to let the receiving action process the request. Often, however, a page offers more than one function. An example is a page that can update, delete, or create some entity. Struts has some helper classes (DispatchAction, etc.) that can direct various functions to specific Java methods. I prefer to create a "relay-action" that simply sends the request to specific, separate action classes. The commonly used technique to recognize the various functions is to place the name of the function in a hidden form field. This can be done with simple JavaScript. If you prefer a JavaScript-less solution, you may use form buttons with different names and then test for the presence of these names in the relay action.

Here's what is needed in the .jsp page:

. . .
<script>
function go(action) {
document.forms[0].action.value=action;
}
</script>
. . .
<html:form ...
. . .
<html:submit onclick="go('update')" value="Update"/>
<input type=hidden name=action>
</html:form>
. . .
This example shows how a submit button will call the JavaScript function "go," which inserts the action name in the hidden field called "action." A relay action class becomes very simple:
public final class DetailDVDRelayAction extends MyAction {

  public ActionForward myExecute(
    ActionMapping mapping,
    ActionForm form,
    HttpServletRequest request,
    HttpServletResponse response) throws Exception {

    // Forward control to the given action
    return doForward(mapping, request.getParameter("action"));
  }

}
The corresponding struts-config file must list all the actions the relay action can receive, for example:
<action path="/detailDVDRelay"
    type="keld.playground.DetailDVDRelayAction"
    name="detailForm"
    validate="false"
    scope="request">
  <forward name="create" path="/createDVD.do"/> 
  <forward name="update" path="/updateDVD.do"/> 
  <forward name="delete" path="/deleteDVD.do"/> 
  <forward name="cancel" path="/cancelDVD.do"/> 
</action>

Rule #4: Use a relay action class for each form.

Now it's time to apply these rules to a real application.

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.

 DevX Skillbuilding from IBM developerWorks
 RIA Run Contest: Build Next-Gen Apps in Microsoft Silverlight 2
 Avaya DevConnect Center
 Intel Go Parallel Portal
 Internet.com eBook Library
 Microsoft RIA Development Center
 Destination .NET
XML error: not well-formed (invalid token) at line 53
advertisement
Receive Articles via our XML/RSS feed
Receive Articles via our XML/RSS feed

JavaBytes
Internet Cyclone
This powerful, easy-to-use, internet optimizer is for Windows 95, 98, ME, NT, 2000 and XP. It's designed to automatically optimize your Windows settings, boosting your Internet connection up to 200%.

SaaS Tool Offers Custom Database Development
Microsoft’s Automated Agent: Can We Talk?
Borland Finally Sells CodeGear
Red Hat Heads For The JON 2.0
Out with the Old, in with the New at JavaOne
Trolltech Expands WebKit Footprint
Oracle: Eating its Own Open Source Food
Big Money and Open Source May Not Compute
Open Source Embrace Gives Sun New Fans
NetBeans, OpenSolaris Also in Spotlight at JavaOne

Taming Trees: Building Branching Structures
Clean Up Function Syntax Mess with decltype
Sutter Speaks: The Future of Concurrency
INTEL SCAVENGER HUNT, LENOVO X300 AND APPLE IPOD TOUCH GIVEAWAY (the "Giveaway")
Comparing Multi-Core Processors for Server Virtualization
Intel® Desktop Business Computing Solutions
Intel: What Downturn?
Managing the Evolving Data Center
Implement Drag and Drop in Your Windows Forms Applications
Processing Linked Web Data with XSLT

Advertising Info  |   Member Services  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About



JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
Microsoft Article: HyperV-The Killer Feature in WinServer ‘08
Avaya Article: How to Feed Data into the Avaya Event Processor
Microsoft Article: Install What You Need with Win Server ‘08
HP eBook: Putting the Green into IT
Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
Avaya Article: Setting Up a SIP A/S Development Environment
IBM Article: How Cool Is Your Data Center?
Microsoft Article: Managing Virtual Machines with Microsoft System Center
HP eBook: Storage Networking , Part 1
Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Intel Video: Are Multi-core Processors Here to Stay?
On-Demand Webcast: Five Virtualization Trends to Watch
HP Video: Page Cost Calculator
Intel Video: APIs for Parallel Programming
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Sun Download: Solaris 8 Migration Assistant
Sybase Download: SQL Anywhere Developer Edition
Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
Red Gate Download: SQL Compare Pro 6
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
IBM Article: Collaborating in the High-Performance Workplace
HP Demo: StorageWorks EVA4400
Intel Featured Algorhythm: Intel Threading Building Blocks--The Pipeline Class
Microsoft How-to Article: Get Going with Silverlight and Windows Live
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES