Monday 12 August 2013

Custom Form Handlers in oracle ATG



Form Handlers:

            In many web applications users input are taken from the forms.A form may need to handle user input in a variety of formats, check input for validity, handle errors, and pass input to a servlet for processing or to a database for storage.  ATG java classes that process these forms are none other than Formhandlers.
         We can create forms in JSP Pages that set values of properties of Nucleus components directly when the form is submitted.
         For more complex form handling operations, setting the values of properties directly is not always desirable. It is generally preferable to first submit the form to a form handler.
         The form handler can evaluate the validity of the data, check for errors, and determine what action to take (e.g., submit the data, direct the user to a different page, display an error message, etc.)
         Often when you use a form handler, the form input fields are associated with properties of the form handler rather than the component you ultimately want to modify
A form handler class must include one or more handler methods. A handler method is typically invoked when the user clicks the submit button, and handles the processing of the form. Depending on the purpose of the form handler, it can have several different handler methods that each perform a different operation. For example, a form handler that works with user profiles might have separate handler methods for creating the profile, modifying the profile, and logging the user in.
If the form uses FormHandler, the exceptions that occurred while processing the form are stored in the following properties.
         formError : A Boolean that is true if any errors occurred when the form was processed.
         formExceptions : A vector of the exceptions that occurred when the form is processed.
         propertyExceptions : A read-only property that returns a Dictionary of subproperties, one for each property set by the form. For any property that generated an exception, the corresponding subproperty in the propertyExceptions Dictionary contains that exception. For any property that did not generate an exception, the corresponding subproperty in the propertyExceptions Dictionary is unset.
If a form handler returns “false” it will redirect to the same page. And if the form handler returns “true” it will redirect it to the success URL.

For every handle method in a formhandler the order of execution is:

          1.beforeSetXXX() of the property
          2.tag convertors (if any) will be applied(Note: Tag converters are converter classes that are used to convert the data from input/ to output)
          3.preXXX()
          4.handleXXX()
          5.postXXX()
          6.afterSetXXX()
        

Displaying Error Messages in Form Handlers:

Use this droplet to loop through and display the error messages that are generated by a form:
<dsp: droplet name=“/atg/dynamo/droplet/ErrorMessageForEach”>
<dsp:oparam name=“output”>
            <dsp:valueof param=“message”/><br>
</dsp:oparam>
</dsp:droplet>

ATG Form Handlers base classes:

Oracle ATG Web Commerce form handler classes all implement the interface atg.droplet.DropletFormHandler. Three form handler base classes implement this interface:
·         atg.droplet.EmptyFormHandler
·         atg.droplet.GenericFormHandler
·         atg.droplet.TransactionalFormHandler

EmptyFormHandler

atg.droplet.EmptyFormHandler implements the DropletFormHandler interface and provides empty implementations of the methods in this interface.

GenericFormHandler

atg.droplet.GenericFormHandler extends EmptyFormHandler. It provides simple implementations of DropletFormHandler interface methods and adds basic error handling logic. If errors occur in processing a form that uses GenericFormHandler, the errors are saved and exposed as properties of the form handler component. This form handler is included in the public API

TransactionalFormHandler

atg.droplet.TransactionalFormHandler extends atg.droplet.GenericFormHandler; it treats the form processing operation as a transaction. Although this form handler methods are processed discretely, their results are saved simultaneously. The transaction management occurs in the beforeGet and afterGet methods. This establishes the transaction before any of properties are set or handler methods are called, rather than in the handler methods themselves.
You can create a form handler by extending one of these classes or any of their subclasses.
Some of the classes that extend the base class in ATG API are:
Ø  SimpleSQLFormHandler for working with form data that is stored in a SQL database.
Ø  RepositoryFormHandler for saving repository data to a database.
Ø  ProfileFormHandler class to connect forms with user profiles stored in a profile repository
Ø  SearchFormHandler for specifying properties available to a search engine.

Steps in Writing a Custom FormHandler:

Non Java Steps:

Step i: Create a JSP file calling the from handler.
In The above example the submit is given to LoginBean.login (note “l” is small in “login”) so the LoginBean java class will call handleLogin() method (note “l” is Caps for “handle Method”).
Step ii: create properties file for the java class. Content will be similar to:
$class=managedBeans.LoginBean
$scope=global  

JAVA STEPS:

Step 1: Create a java class extending the atg.droplet.GenericFormHandler base class.
Step 2: Provide the appropriate handlexxx methods. Each Handler method will be triggered by
corresponding dsp:input submit button from the jsp page.
Step 3: Provide either a Map data structure to hold the form data or provide individual fields
with apprppriate set/get methods.
Step 4: Write the desired codes and return the Boolean codes appropriately.
Step 5: Populate validation errors if any
Step 6: Forward the user to the appropriate success URL.




11 comments:

Anonymous said...

create properties file for the java class. Content will be similar to:
$class=managedBeans.LoginBean
$scope=global

I have a question
Where i create the property file and how to name the property file

Unknown said...

you can give any name. but make sure this name is the component you refer to in jsp

Unknown said...

if u create ur properties file at /atg/formhandler/MyFormHandler.properties, in jsp the reference wil b /atg/formhandler/MyFormHandler.

Tech Craft Tips said...

you have to place it in config folder. and the path must be the same path u specified in your jsp.

Say you refer /xx/yy/zz/sampleBean in your jsp, then in "config" folder create xx folder, in that xx folder create yy folder, again in that yy folder create zz folder and keep your properties file there.
Here in this scenario sampleBean.properties file must be created.

in that if u specify
$class=somePackage.SomeClass
in that properties file you have to create
"SomeClass" in "somePackage" in source(src folder).

Anonymous said...

I am getting below error. can you please help me to fix it

atg.droplet.DropletException: Can't find property: username in class com.verizon.zubair.LoginBean

this is my class file
package com.verizon.zubair;
public class LoginBean extends GenericFormHandler {
private String username;
private String password;
private String message;

@SuppressWarnings("unused")
private String getUsername() {
return username;
}
@SuppressWarnings("unused")
private void setUsername(String username) {
this.username = username;
}
@SuppressWarnings("unused")
private String getPassword() {
return password;
}
@SuppressWarnings("unused")
private void setPassword(String password) {
this.password = password;
}
@SuppressWarnings("unused")
private String getMessage() {
return message;
}
private void setMessage(String message) {
this.message = message;
}
......
}

in jsp
dsp:input type="text" bean="/com/verizon/zubair/LoginBean.username" dsp:input

Properties
$class=com.verizon.zubair.LoginBean
$scope=global

Anonymous said...

Everything looks fine... Can you check your component in dyn/admin and see if the properties you defined are available?

Anonymous said...

I think you need to include the constructor in your java class. Please try this and let me know the results.

Anonymous said...

nice work... thanks for posting...

Varun Jain said...

Properties file should be named as same as the class file and the package should follow the same structure as of class. And the properties file should be in "config folder.

Monis Yousuf said...

property file name can be anything.. you just need to refer to your class and you can access it using your config path.. you can also have a look here for more details:-
http://learnoracleatg.blogspot.in/2014/11/art113-how-to-usecreate-formhandlers-in.html

Anonymous said...

Hi,
when i have some error in the form while submitting it should redirect to same form page but the values entered should be retained except for the field there was an error.How to do that?

Post a Comment

 
;