Another Example

In this Login page mock-up, the Page object switches its behavior based on the parameters passed to it.

package Project::Login;
use strict;
use warnings;
our @ISA = qw( Org::Bgw::Pas::SessionPage );

sub execute
{
  my($self) = @_;
  return $self->handleUserLoggingIn if $self->isAction('loggingIn');
  return $self->handleLogUserOut    if $self->isAction('loggingOut');
  return $self->forward_request('login.psp');
}

sub isAction
{
  my($self,$action) = @_;
  return undef unless defined $self->query->param('action');
  return $action eq $self->query->param('action');
}

sub handleUserLoggingIn
{
  my($self) = @_;

  my $profile = Project::DB::UserProfile->loadByUserName(
    $self->query->param('userName')
  );

  unless( $profile ) {
    return $self->loginError("Error, invalid username or password.");
  }

  unless( $self->query->param('password') eq $profile->password ) {
    return $self->loginError("Error, invalid username or password.");
  }

  $profile->isLoggedIn(1);
  $self->session->param('project.userProfile',$profile);
  return $self->response->set_redirect('/pas/project/UserProfile');
}

sub loginError
{
  my($self,@msg) = @_;
  $self->query->param('login.error',join('',@msg));
  return $self->forward_request('login.psp');
}

sub handleUserLoggingIn
{
  my($self) = @_;
  $self->session->param('project.userProfile', Project::DB::UserProfile->new );
  return $self->response->set_redirect('login.psp');
}

1;

    

The PSP page first looks for an error message, and displays it if found. Then it proceeds to display the login form. This PSP uses the query object to produce the form elements, so that they will retain their previous values. In the event of an unsuccessful login, the username and password fields will remain populated with the value the user filled in.

<HTML>
<HEAD><TITLE>Login</TITLE></HEAD>
<BODY BGCOLOR="#FFFFFF">
<H1>Login</H1>
<% if( $self->query->param('login.error') ) { %>
<FONT COLOR="RED">
  <%= $self->query->param('login.error') %>
</FONT>
<% } %>
<FORM ACTION="/pas/project/Login" METHOD="POST">
<INPUT TYPE="HIDDEN" NAME="action" VALUE="loggingIn">
<TABLE BORDER=0 ALIGN=CENTER>
  <TR>
    <TD> User Name: </TD>
    <TD> <%= $self->query->textfield('userName') > </TD>
  </TR>
  <TR>
    <TD> Password: </TD>
    <TD> <%= $self->query->password_field('password') > </TD>
  </TR>
</TABLE>
<INPUT TYPE="Submit" NAME="Submit" VALUE="Submit">
</FORM>
</BODY>
</HTML>