FileDocCategorySizeDatePackage
ValidatingLoginAction.javaAPI DocExample2150Thu Aug 19 12:19:56 BST 2004com.oreilly.strutsckbk.ch09

ValidatingLoginAction

public class ValidatingLoginAction extends BaseAction

Fields Summary
Constructors Summary
Methods Summary
public org.apache.struts.action.ActionForwardexecute(org.apache.struts.action.ActionMapping mapping, org.apache.struts.action.ActionForm form, javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)


        ActionMessages appWarnings = form.validate(mapping, request);
        saveAppWarnings(request, appWarnings);
        
        String username = (String) PropertyUtils.getSimpleProperty(form, "username");
        String password = (String) PropertyUtils.getSimpleProperty(form, "password");
        
        ActionMessages appErrors = new ActionMessages();
                
        try {
            SecurityService service = new SecurityService();
            service.authenticate( username, password);            
        }
        catch (PasswordMatchException e) {
            appErrors.add("password", new ActionMessage("error.password.match"));
        }

        saveAppErrors(request, appErrors);
        
        if (!appWarnings.isEmpty() || !appErrors.isEmpty()) {
            return mapping.getInputForward();
        }
        
        User user = new User();
        user.setUsername(username);
        request.getSession().setAttribute("user", user);
        
        ActionMessages messages = new ActionMessages();
        ActionMessage message = new ActionMessage("message.login.success", username);

        messages.add(ActionMessages.GLOBAL_MESSAGE, message);

        if (!messages.isEmpty()) {
            saveMessages( request.getSession(true), messages );
        }

        return mapping.findForward("success");