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.

 Avaya DevConnect Center
 Service Component Architecture/Service Data Objects Solution Center
 Intel Go Parallel Portal
 Internet.com eBook Library
 IBM Software Construction Toolbox
 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%.

Is .NET on Linux Finally Ready?
Red Hat Takes on HPC Market, Microsoft
Python's New Release Bridges the Gap
No Flash Seen on iPhone Horizon
Apple Yields to Complaints Over iPhone NDA
Microsoft Shows Some Ankle With Visual Studio
Gentoo Linux Cancels Distribution
It's Official: Windows 7 at PDC, WinHEC
Oracle Keeps Building on Spoils From BEA
Intel, Oracle Head For 'The Cloud'

C++Ox: The Dawning of a New Standard
Getting Started with Virtualization
Master Complex Builds with MSBuild
eCryptfs: Single-File Encryption in Linux
CCXML in Action: A CCXML Auto Attendant
Ballmer: Current Woes Won't Halt Tech, Microsoft
Microsoft Uses VMworld to Hype Its Hypervisor
Microsoft Charges Ahead in Virtualization
Microsoft Shows Some Ankle With Visual Studio
Top 5 Reasons to Adopt SQL Server 2008

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
IBM Whitepaper: Innovative Collaboration to Advance Your Business
Internet.com eBook: Real Life Rails
Avaya Article: Call Control XML - Powerful, Standards-Based Call Control
Internet.com eBook: The Pros and Cons of Outsourcing
Go Parallel Article: Scalable Parallelism with Intel(R) Threading Building Blocks
Internet.com eBook: Best Practices for Developing a Web Site
IBM CXO Whitepaper: The 2008 Global CEO Study "The Enterprise of the Future"
Avaya Article: Call Control XML in Action - A CCXML Auto Attendant
Go Parallel Article: James Reinders on the Intel Parallel Studio Beta Program
IBM CXO Whitepaper: Unlocking the DNA of the Adaptable Workforce--The Global Human Capital Study 2008
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
Go Parallel Article: Getting Started with TBB on Windows
HP eBook: Storage Networking , Part 1
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Go Parallel Video: Intel(R) Threading Building Blocks: A New Method for Threading in C++
HP Video: Is Your Data Center Ready for a Real World Disaster?
Microsoft Partner Portal Video: Microsoft Gold Certified Partners Build Successful Practices
HP On Demand Webcast: Virtualization in Action
Go Parallel Video: Performance and Threading Tools for Game Developers
Rackspace Hosting Center: Customer Videos
Intel vPro Developer Virtual Bootcamp
HP Disaster-Proof Solutions eSeminar
HP On Demand Webcast: Discover the Benefits of Virtualization
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Microsoft Download: Silverlight 2 Software Development Kit Beta 2
30-Day Trial: SPAMfighter Exchange Module
Red Gate Download: SQL Toolbelt
Iron Speed Designer Application Generator
Microsoft Download: Silverlight 2 Beta 2 Runtime
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
IBM IT Innovation Article: Green Servers Provide a Competitive Advantage
Microsoft Article: Expression Web 2 for PHP Developers--Simplify Your PHP Applications
Featured Algorithm: Intel Threading Building Blocks - parallel_reduce
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES