Hi All,

I am Ryan Norman

I am trying to put together a simple sample application. 
Right now I have a login page upon submitting the user name and password I want it to 
be transferred
to another page.

For some reason, it is not working.

Can somebody help me with this?

I am pasting my code and struts-config.xml file contents.

Thanks in advance

struts-config.xml
============
<?xml version="1.0" encoding="ISO-8859-1" ?>

<!DOCTYPE struts-config PUBLIC
  "-//Apache Software Foundation//DTD Struts Configuration 1.0//EN"
    "http://jakarta.apache.org/struts/dtds/struts-config_1_0.dtd";>

<!--
     This is a blank Struts configuration file based on the example application,
     with commented examples of some items.

     NOTE: If you have a generator tool to create the corresponding Java classes
     for you, you could include the details in the "form-bean" declarations.
     Otherwise, you would only define the "form-bean" element itself, with the
     corresponding "name" and "type" attributes, as shown here.
-->

<struts-config>
  <!-- ========== Data Source Configuration =============================== -->
  <data-sources>
    <data-source autoCommit="false" description="Struts Test1 DB"
      driverClass="sun.jdbc.odbc.JdbcOdbcDriver" maxCount="4" minCount="2"
    password="TIGER" url="jdbc:odbc:DEVELOPMENT"
        user="SCOTT" />
  </data-sources>

  <!-- ========== Form Bean Definitions =================================== -->
  <form-beans>
    <!-- Example logon form bean -->
   <form-bean name="loginForm" type="strutstest1.LoginForm"/>
  </form-beans>

  <!-- ========== Global Forward Definitions ============================== -->
  <global-forwards>
   <!-- Example logon forward -->
   <forward name="login" path="/login.jsp"/>
    <forward name="failure" path="/login.jsp"/>
    <forward name="success" path="/mainmenu.jsp"/>
  </global-forwards>

  <!-- ========== Action Mapping Definitions ============================== -->
  <action-mappings>
    <!-- Example logon action -->
    <action path="/login" type="strutstest1.LoginAction"
      name="loginForm" scope="request" input="/login.jsp">
    </action>

    <!-- The standard administrative actions available with Struts -->
    <!-- These would be either omitted or protected by security -->
    <!-- in a real application deployment -->
    <action path="/admin/addFormBean"
    type="org.apache.struts.actions.AddFormBeanAction"/>
    <action path="/admin/addForward"
    type="org.apache.struts.actions.AddForwardAction"/>
    <action path="/admin/addMapping"
    type="org.apache.struts.actions.AddMappingAction"/>
    <action path="/admin/reload"
    type="org.apache.struts.actions.ReloadAction"/>
    <action path="/admin/removeFormBean"
    type="org.apache.struts.actions.RemoveFormBeanAction"/>
    <action path="/admin/removeForward"
    type="org.apache.struts.actions.RemoveForwardAction"/>
    <action path="/admin/removeMapping"
    type="org.apache.struts.actions.RemoveMappingAction"/>

  </action-mappings>
</struts-config>

LoginAction.java
============
package strutstest1;

import java.io.PrintWriter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionServlet;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;

public class LoginAction extends Action
{
 public ActionForward perform( ActionServlet servlet,
   ActionMapping mapping,
      ActionForm form,
      javax.servlet.http.HttpServletRequest request,
      javax.servlet.http.HttpServletResponse response )
  {
  String userID = "";
    String password = "";

    PrintWriter out = null;

   ActionForward actionForward = null;

    try
    {
     userID = ( ( LoginForm ) form ).getUserID();
     password = ( ( LoginForm ) form ).getPassword();

      out = response.getWriter();

      out.println( "User ID  = [" + userID + "] <br>" );
      out.println( "Password = [" + password + "] <br>" );
    }
    catch( Exception error )
    {
     System.out.println( "Error Occurred: " + error.toString() );
    }

    actionForward = mapping.findForward( "success" );

    return( actionForward );
  }
}


LoginForm.java
===========
package strutstest1;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;

public class LoginForm extends ActionForm
{
 private String userID = "";
  private String password = "";

  public String getUserID()
  {
   return( ( userID == null ? "" : userID ) );
  }
  public void setUserID( String userID )
  {
   this.userID = ( userID == null ? "" : userID );
  }

  public String getPassword()
  {
   return( ( password == null ? "" : password ) );
  }
  public void setPassword( String password )
  {
   this.password = ( password == null ? "" : password );
  }

  public ActionErrors validate( ActionMapping mapping, 
javax.servlet.http.HttpServletRequest request )
  {
   ActionErrors actionErrors = new ActionErrors();

    if( this.getUserID().equals( "" ) )
    {
   actionErrors.add( "userID", new ActionError( "error.login.blankuserid" ) );
    }

    if( this.getPassword().equals( "" ) )
    {
   actionErrors.add( "password", new ActionError( "error.login.blankpassword" ) );
    }

    return( actionErrors );
  }
}


Reply via email to