Java Reference
In-Depth Information
Building the update Facelet was remarkably easy; however, getting the RR to work with the DBMS was not,
and the RR does not handle the “employee number not found” condition very well. SDOs are very powerful but, as
mentioned, they often “do not play well” with non-SDO technologies. So in the next tutorial we will address some of
these shortcomings.
Tutorial: Fixing the Application
There are several ways to fix the problems. The harder, but prettier way, is to change the pagecode classes associated
with UpdateEmp (to trim off the spaces) and GetEmp (to check that the employee does exist in the DBMS). In
addition, better navigation rules will be needed.
To check that an employee exists modify the GetEmp's doButton1Action to check to see if there is an
employee record for the employee number specified. This would require importing DBAccess and Employee from
TutorialsWeb/c11 into TutorialsJSF/JavaResources/src/pagecode. c12. (On the paste, RAD will change their package
statements from c11 to pagecode.c12. Wasn't that nice of RAD!)
In doButton1Action, create an Employee object and then use the scripting variable to retrieve the record from the
database. Remember, the Employee class does not throw an exception if no record is found so doButton1Action must
check to see if employee information exists in the employee object. If there is an employee name, then a “success”
outcome will be returned.
If there is no employee name, then employee number should be blanked out of the inputText field and an
“employee doesn't exist” error message displayed on GetEmp.xhtml. To do this add a JSF Output field (which
generates an outputText field called text2) to the page. In addition, create a sessionScope scripting variable (named
msg) and bind it to text2. Then in doButton1Action, blank out the value in the scripting variable empNum. Because
the scripting variable is bound to the text field, the text field will be blanked out also. Place the error message text in
the scripting variable msg. Once again, because of binding the message will appear in the output field. Finally, we
want to return an outcome of “failure”.
The navigation buttons still need to be changed so that only when success is returned is UpdateEmp.xhtml displayed.
On failure, GetEmp.xhtml will be redisplayed with the error message and the invalid employee number erased.
The following is the new doButton1Action method:
public String doButton1Action() {
String Num = (String) getSessionScope().get("empNum");
Employee emp = new Employee();
emp.getEmpInfo(Num);
getSessionScope().put("msg", "");
if (emp.getEmpName().length() > 0) {
return "success";
} else {
getSessionScope().put("msg", "Employee number " +
getSessionScope().get("empNum") +
" doesn't exist, please re-enter.");
getSessionScope().put("empNum", "");
return "failure";
}
}
As mentioned, the old navigation rule must be changed so that it is not the default. This rule (displaying
UpdateEmp.xhtml) should only be executed when the outcome is “success” (see Figure 12-44 ). Create a new
navigation rule for the “failure” outcome that redisplays getEmp (see Figure 12-45 ).
 
Search WWH ::




Custom Search