Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Put Some Ajax4JSF Magic into Data Validation : Page 4

Apply an open source framework that gives you the functionality of AJAX and the best of JSF to validate data entries dynamically on the server side.


advertisement
Send a Request
To try out the application send an AJAX request from the JSF JSP page, input.jsp, using the Ajax4JSF framework. Add a <a4j:support/> element to the Catalog ID field. Use the event attribute to specify the event that initiates an AJAX request, and then specify a inputText_action() backing bean method to invoke using the action attribute when an AJAX request is sent. Use the reRender attribute to specify the components that are to be updated with the AJAX response. The h:inputText element for the Catalog ID field is:

<h:inputText binding="#{backing_input.inputText1}" id="inputText1"> <a4j:support event="onkeyup" action= "#{backing_input.inputText_action}" reRender= "inputText2,inputText3,inputText4,inputText5,inputText6, commandButton1,outputText1" /> </h:inputText>

To use the Ajax4JSF component library, add this taglib directive to the JSF JSP page, input.jsp (see Listing 2):

<%@ taglib uri="https://ajax4jsf.dev.java.net/ajax" prefix="a4j"%>

To create a catalog entry when the form is submitted by clicking the command button, add an action attribute to the command button to invoke the commandButton_action() method when the command button is clicked. Here is the command button element:


<h:commandButton value="Submit" binding="#{backing_input.commandButton1}" action= "#{backing_input.commandButton_action}" id="commandButton1"/>

Modify catalog.jsp to output a message that a catalog entry has been created, and modify error.jsp to output a message that an error has been generated in updating the database.

The value entered in the Catalog ID field is validated on the server side where the database table Catalog resides. If the ID value already exists in the database, the message, "Catalog Id is not valid" displays on the input form to tell the user that the number already exists and therefore the entry is not "valid" as a new ID number. If the entry in the Catalog ID field doesn't already exist in the database, the validation message, "Catalog Id is valid" displays on the input form. The business logic that defines a valid ID value can be specified on the server side.

The inputText_action() method is invoked when a value is specified in the Catalog ID field. An AJAX request is sent with each modification in the Catalog ID field—that is, each time a new character is typed. Obtain a connection with the Oracle database in the inputText_action() method using the Oracle data source configured in JDeveloper:

InitialContext initialContext = new InitialContext(); javax.sql.DataSource ds = (javax.sql.DataSource)initialContext.lookup( "java:comp/env/jdbc/OracleDBConnectionDS"); java.sql.Connection connection = ds.getConnection();

Run a Statement object in a SQL statement:

Statement stmt = connection.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);

You can obtain the Catalog ID value that is specified in the form and create a SQL query to run in the Oracle database:

String catalogID = (String)inputText1.getValue(); String query = "SELECT * from Catalog WHERE CATALOGID=" + "'" + catalogID + "'";

Run the SQL query and obtain a result set:

rs = stmt.executeQuery(query);

If the result set isn't empty, set the validation message to "Catalog Id is not valid," set the field values, and disable the command button:

if (rs.next()) { inputText2.setValue(rs.getString(2)); inputText3.setValue(rs.getString(3)); inputText4.setValue(rs.getString(4)); inputText5.setValue(rs.getString(5)); inputText6.setValue(rs.getString(6)); outputText1.setValue(new String("Catalog Id is not Valid")); commandButton1.setDisabled(true); }

If the result set is empty, implying that the ID value specified in the input form is not already in the database, set the validation message to, "Catalog Id is valid," set the field values to empty Strings, and enable the Submit button:

else { inputText2.setValue(new String()); inputText3.setValue(new String()); inputText4.setValue(new String()); inputText5.setValue(new String()); inputText6.setValue(new String()); outputText1.setValue(new String("Catalog Id is Valid")); commandButton1.setDisabled(false); }

If the ID value is valid—the value in the field doesn't exist already in the database—retrieve the form field values, obtain a connection with the database, and create a new catalog entry. The backing bean class, Input.java, is shown in Listing 3.

Process the Response
Now you can run the application to test validating the ID values you specify in the form against the data in the Catalog database table. If the ID value you enter already exists, the application interprets the entry as invalid, a message indicating that result displays, the other form field values for the existing record display, and the Submit button is disabled.

If the ID value you enter doesn't already exist in the database table, the application interprets it as valid, a message indicating that result displays, the other form fields are set to empty String values, and the Submit button is enabled.

For a valid ID, you can create a catalog entry and use the Submit button to store it. The JSF components set in the reRender attribute of the <a4j:support> tag specify the components to be updated with the AJAX response.

Now run the JSF page by right-clicking input.jsp and selecting Run. Specify a value for the Catalog ID field. An AJAX request is sent with each modification to the field (as each character is typed), and the validation message indicating that the ID number is unique (valid) displays.

Next, specify an ID value that is already in the database—for example, catalog2. Notice that the message "Catalog Id is not valid" appears. Because the value exists in the database, autocompletion fills out the form field with values and the Submit button is disabled.

As you can see, the Ajax4JSF component library adds AJAX functionality to JSF applications, which precludes requiring JavaScript code to add the AJAX functionality.



Deepak Vohra is an O'Reilly technical reviewer, who reviewed the book WebLogic: The Definitive Guide. He's also a NuBean consultant and web developer, and is a Sun Certified Java 1.4 Programmer and Sun Certified Web Component Developer for J2EE.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap