Dashboard > Struts University > Home > MailReader Training Course > Topics > Register-Notes
Register-Notes
Added by Ted Husted, last edited by Ted Husted on Jul 14, 2007  (view change)
Labels: 
(None)


Forms play a strong role in most web applications. How does Struts help us harvest data submitted by a form?

Presentation - Capturing Input

How do HTML forms capture input? What do dynamic forms add? How do we handle input errors? How does input get to the database?

We look at how dynamic forms improve on static HTML forms.

  • HTML Forms
  • Dynamic Forms
  • Error Handling
  • Finding the Database

Workshop - Register

Story: Subscribers can store login credentials and a primary email contact with the system.

  • Create input form
  • Create Action class
  • Utilize a base "Support" class
  • Utilize a business facade
  • Utilize ActionErrors

Prerequisites

  • form, textfield, password, submit, actionError
  • action/method alias, input, error
  • DatabaseListener

Exercises

  • Continue using code from a prior workshop (or import "register" from CD as new MailReader project)
  • Review Register use case
  • Implement Main Success Scenario only
    • Be optimistic and assume all data is valid
    • Validation is covered in the "register2" workshop
  • Create a Register action mapping to invoke the standard input method
  • Create a Register Action and mapping to store the form data using a save method (see Hint Code)
  • For now, On SUCCESS, goto the "Missing" action

Hint Code

  • Action mappings can reference alternate methods to invoke instead of execute.
<action name="Register" class="actions.Register" method="input">
    <result name="input">/pages/Register.jsp</result>
    </action>
    
    <action name="Register_save" class="actions.Register" method="save">
    <result name="input">/pages/Register.jsp</result>
    <result>/pages/Missing.jsp</result>
    </action>
  • Copy the register/Source/actions/DatabaseListener.java class from the CD to your project
  • Insert an entry to load the listener in your web.xml
<listener>
    <listener-class>
    actions.DatabaseListener
    </listener-class>
    </listener>
    
    <welcome-file-list>
  • Create or copy a starter "database.xml" file under Sources/
<?xml version='1.0'?>
    <database>
    <user username="user" fromAddress="John.User@somewhere.com"
    fullName="John Q. User" password="pass">
    <subscription host="mail.hotmail.com" autoConnect="false"
    password="bar" type="pop3" username="user1234">
    </subscription>
    <subscription host="mail.yahoo.com" autoConnect="false" password="foo"
    type="imap" username="jquser">
    </subscription>
    </user>
    </database>
  • A best practice is to use a base "Support" class to share utility code between Actions.
    • Copy the starter MailReaderSupport class from the CD to your project
    • Extend your Register Action from MailReaderSupport
  • Utilize the findUser, createUser, and saveUser methods to store the Register form input.

Other Hints

  • If you change a JSP, you can make the module to copy over the JSP, and reload the page without restarting Tomcat.
  • Do not overuse the stack (think local thread)
  • Check the WEB-INF/database.xml file in the deployed web application to see what is being saved. To find the deployed copy, Search for "database.xml" under the .metadata folder in your workspace. For example:
C:\MRTC\labs\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\ ..
    profile\WEB-INF\classes\database.xml

Accomplishments

  • Created input form
  • Created Action class
  • Utilized a base "Support" class
  • Utilized a business facade
  • Utilized ActionErrors

Extra Credit

  • Set the focus to the first field using a standard JavaScript like

onload="self.focus();document.Register_save.username.focus()

  • Try creating the same user twice. See what happens. Why?
    • Change to Debug perspective, stop and start the Server, and try again. Did the user account persist across sessions?
  • Set a breakpoint in CreateUser and step through the operation. Confirm that the User is being created correctly.
  • Write a unit test to exercise Register Action (if you haven't already).
    • Change some of the tests so that you can see them fail too. (And then change them back!)

Site running on a free Atlassian Confluence Open Source Project License granted to OSS. Evaluate Confluence today.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.5.5 Build:#811 Jul 25, 2007) - Bug/feature request - Contact Administrators