Home

 

Completing the logon action

The skeleton code for the LogonAction class was created from the Web Diagram and is shown in Example | 5-5.

Example 15-5 Skeleton action class (abbreviated)

public class LogonAction extends Action {

	public ActionForward execute(ActionMapping mapping, ActionForm form,
						HttpServletRequest request, HttpServletResponse response)
            throws Exception {
		ActionMessages errors = new ActionMessages();
		ActionForward forward = new ActionForward(); // return value
		try {
            // do something here
		} catch (Exception e) {
            // Report the error using the appropriate name and ID.
            errors.add("name", new ActionMessage("id"));
		}
        // If a message is required, save the specified key(s)
        // into the request for use by the <struts:errors> tag.
		.........
}

We have to complete the execute method with our logic. The final code is shown in Example | 5-6. Select Source Æ Organize Imports to resolve the imports.

Example 15-6 Completed execute method of the logon action class

public ActionForward execute(ActionMapping mapping, ActionForm form,
					HttpServletRequest request, HttpServletResponse response)
				 throws Exception {

	ActionMessages errors = new ActionMessages();
	ActionForward forward = new ActionForward(); // return value

	LogonForm logonForm = (LogonForm)form;
	String ssn = logonForm.getSsn(); 

	try {
		// Create a bank object with pre-generated in-memory data
		Bank bank = ITSOBank.getBank();
		Customer customerBean = bank.searchCustomerBySsn(ssn);
		// Add the customer to the request object for the JSP pages to use it
		request.setAttribute("customer", customerBean);
		// Add the bank and ssn objects to the session for further use
		request.getSession().setAttribute("ssn", logonForm.getSsn());
		request.getSession().setAttribute("ITSOBank", bank);
	} catch (InvalidCustomerException e){
		errors.add("ssn", new ActionError("error.ssn"));
	} catch (Exception e) {
		errors.add("error", new ActionError("errors.systemError"));
	}
	// If a message is required, save the specified key(s)
	// into the request for use by the <struts:errors> tag.
	if (!errors.isEmpty()) {
		saveErrors(request, errors);
		// Forward control to the appropriate 'failure' URI (........)
		forward = mapping.findForward("failure");
	} else {
		// Forward control to the appropriate 'success' URI (.......)
		forward = mapping.findForward("success");
	}
	// Finish with
	return (forward);
}

Let us now understand some of the action class coding:

The ActionForm parameter is cast to the correct LogonForm class and the ssn value is extracted.

The customer and accounts are retrieved from the in-memory data autogenerated on invocation to the ITSOBank.getBank() method.

The ssn and bank objects are stored as session data and the customer information is stored in the request block. The session data is available to all actions and JSPs.

If exceptions are thrown by the data access, an ActionMessage is added to the Struts errors list. The error text ("error.ssn" or "errors.systemError") comes from the ApplicationResources file (see Example | 5-1).

An ActionForward of either "failure" or "success" is returned. Struts will find the appropriate resulting action or JSP in the configuration file.
ibm.com/redbooks