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.

 Microsoft RIA Development Center
 IBM Rational Resource Center
 Destination .NET
XML error: not well-formed (invalid token) at line 33
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%.

JUNOS: Open, but Not Open Source
Sun Gives Enterprises a New Mobility Option
iPhone Knocking on the Enterprise
Linux 2.6.26 Opens Up to Debugging
Borland Launches ALM Management Tools
Nominations Open for CEO Vision Awards
A Look at Google's Open Source Protocol Buffer
MySpace Catches iPhone Fever
Software Drives the iPhone 3G
Novell's Linux Build Service Goes Open

Introduction to Scrum
Semantic Search Arrives at the Web
Using the New ListView Control in ASP.NET 3.5
SMS Messaging Using the .NET Compact Framework
Security Enhancements Abound in Windows Server 2008
Design and Use of Moveable and Resizable Graphics, Part 2
Control Your Linux Server Remotely with GnuPG, procmail, and PHP
The Baker's Dozen: 13 Tips for Building Database Web Applications Using ASP.NET 3.5, LINQ, and SQL Server 2005 Reporting Services
Probe and Modify Your Types' Alignment Programmatically
.NET Building Blocks: Custom User Controls in Practice

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