October 17, 2019
Hot Topics:

JavaServer Faces and ASP.NET - A Side by Side Look Part 2

  • February 27, 2006
  • By Michael Klaene
  • Send Email »
  • More Articles »

In the previous article, I discussed the similarities and differences between the ASP.NET and JavaServer Faces frameworks. Two sample two applications were shown. One of the applications was developed using JSF and the other used ASP.NET. Both applications served the same functional purpose: to allow employees to reserve conference rooms. Both web applications accessed the same MySql database. I used two popular IDEs (Integrated Development Environments) to assemble the applications: Microsoft's Visual Web Developer 2005 Express Edition (Get a copy) and Sun Microsystem's Java Studio Creator (Get a copy). During the development process, I tried to illustrate some of the basic concepts behind each framework.

In this article, a little more functionality will be added to these applications. Then, I will present some brief comparisons of these frameworks in a number of diverse areas which are not addressed by the applications, but which are of importance to application developers. As mentioned in the last article, the goal is to provide an overview of the features behind these frameworks without delving into too deeply. With some fundamental knowledge, you can further explore on your own if you wish.

Validating Input

In the last article, database data was displayed in read-only fashion to users and user interaction was limited. Most real world applications must allow users to input data into the system. Thus, validation requirements exist for almost all applications. Currently, the two applications display a list of available rooms and allow users to display any reservations associated with those rooms. A link accompanies each room which directs users to an online form where new reservations ca be made. The reservation forms for both applications are displayed below.

ASP.NET Reservations Form
JSF Reservations Form

In these forms, the user first enters a name of an individual or group that wants to make the reservation. Then, the user enters a start and end time for the new reservation. As a developer, you must account for all of the possible actions a user might take that do not conform to what the application expects. ASP.NET and JSF both provide components to help with input validation. ASP.NET Validator Controls are attached to the relevant form item and validate that item's data upon form submission. ASP.NET Validator Controls include RequiredFieldValidator, RegularExpressionValidator, RangeValidator, and CompareValidator. A CustomValidator component can also be used when the developer needs to write custom code in order to properly validate a component. In an ASP.NET Page's lifecycle, validation will occur, with the exception of CustomValidator, prior to the data being posted back to the server. This is because ASP.NET Validator Controls generate the necessary JavaScript needed to implement validation on the client side, thereby reducing server load. However, ASP.NET always performs this validation on the server as well to ensure validation always takes place.

JSF also provides some standard validation components, though not as numerous as the selections available in ASP.NET. To write your own validation logic, you can also override a JSF component's validate method or delegate validation to another class that implements the Validator interface. (You could also write your own validation component, but this is a more complex solution). JSF lacks the built-in JavaScript functionality that ASP.NET validation components offer. Validation in a JSF page takes place on the server after the component tree has been restored, but before any event handlers are called. Of course, you can always provide your own custom JavaScript validation. Given this overview, let us look at how a user's reservation data in each of these applications can be properly validated.

These forms are fairly simple as web forms go but there are still validation issues to consider. For starters, it would make little sense to allow a user to reserve a room and not enter a name specifying who is making the reservation. Therefore, the 'Reservation Team' field should be required. In ASP.NET this is done by dragging a RequiredFieldValidator control from the palette onto the form. The ControlToValidate property should read 'lblReserveTeam'. In the property palette you can specify the error text that the user should receive. I added the following error message : 'You must enter a Reservation Team.' The corresponding JSF Text Field component representing the reservation team does not require a separate validation component because this component already contains a property called 'required'. Simply checking this attribute will force the user to enter a value.

Next, you can limit the length of text in the 'Reservation Team' field. If you remember, the corresponding database field is only 45 characters in length. Therefore, the user should be limited to entering 45 characters or less. In the JSF application, a LengthValidator component can be dragged from the 'Validators' section of the component palette onto the field and specify 45 as its maximum length. For its error text property, type 'Reservation Team cannot exceed 45 characters'. ASP.NET has no standard validation control to restrict field length. Instead, you can rely on the RegularExpressionValidator control which allows the maximum length to be specified via a regular expression like '^[sS]{,45}$'.

The start and end times should be checked to make sure that the start time is equal to or less than the end time. ASP.NET provides a CompareValidator that will work in most cases when comparing two fields. However, since the out of the box Calendar component is being used, you cannot do this as the CompareValidator does not support this component. In this particular instance, I had to assign these dates to two text fields and validate against them instead. Another option would be to write validation logic in the Page_Load event routine itself. Since JSF has no standard validation component to compare two fields, I had to write validation logic by implementing the start time component's validate method:

   public void calendarStart_validate(FacesContext fc, UIComponent uic, Object o) {
      if (this.calendarStart.getSelectedDate().after(this.calendarEnd.getSelectedDate()) ) {
  	     throw new ValidatorException(new FacesMessage
           ("The start time must come before the end reservation time."));

You'll notice the reference to a FacesMessage component in the JSF validation logic above. In JSF, a message component is required to display output text related to a field. To do this, I added a Message component from the component palette and attached it to the correct form field by dragging it onto the field. Once this is complete, error messages will be displayed for the offending component when validation fails. In ASP.NET, you don't have to perform this step because the controls themselves display the error message.

Another important set of JSF components are known as 'converters'. These components are responsible for converting form data to and from the object model. There are a number of default converters available in JSF to perform common conversion like Strings to Integers and Strings to Dates. For more complex transformations that involve application objects, custom converters can be created. As you can see in the code above, the JSF calendar component has a method called getSelectedDate. This is because this component includes its own converter already. Though this JSF application will not require the use of converters, you should familiarize yourself with JSF converters as they can help reduce code and runtime conversion errors. ASP.NET does not provide a standard set of converters.

There is one additional validation that should probably be added to these forms. This validation would query the database to ensure there are no existing reservations that conflict with the time the user selects. To do something of this complexity, you would use a CustomValidator control in ASP.NET. Likewise, in JSF, this logic would be placed in the component's validate method or delegated to another class that handles validations.

Adding ASP.NET Validator Controls
Adding JSF Validator Components

Page 1 of 2

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date