Hi,

I am Norman.

I have a simple problem for which I don't know the answer.

I have a single form that I want to use for both add and view user. I was able to 
successfully add a user. After adding the user I want the user details to be displayed.
Can somebody throw some lights on what I need to do. I am pasting my code with this.

Partial struts-config.xml
-------------------------
  <global-forwards>

    <forward name="login" path="/login.jsp"/>
    <forward name="logout" path="/logout.jsp"/>
    <forward name="changePassword" path="/password.jsp"/>
    <forward name="mainMenu" path="/index.jsp"/>
    <forward name="searchUser" path="/admin/search/searchuser.jsp"/>
    <forward name="userList" path="/admin/search/userlist.jsp"/>
    <forward name="user" path="/admin/user.jsp"/>

  </global-forwards>

  <form-bean name="userForm" type="strutsapp1.user.UserForm"/>

    <action path="/user" type="strutsapp1.user.UserAction"
      name="userForm" scope="request" input="/admin/user.jsp">
    </action>


user.jsp
---------
<%@ page language="java" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>

<jsp:useBean id="action" scope="page" class="java.lang.String">
</jsp:useBean>
<%
 action = request.getParameter( "Action" );
 //action = ( String ) request.getAttribute( "Action" );
  action = ( action == null ? "" : action );
%>

<!doctype html public "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
 <head>
  <link href="../style.css" rel="stylesheet" type="text/css">
 </head>

 <body>
 <html:form action="/user" method="post" enctype="application/x-www-form-urlencoded">
   <input type="hidden" name="txtAction" value="<%= action %>">
  <table border="1" cellpadding="0" cellspacing="0" width="100%" align="center">
   <tr width="100%">
    <td class="ErrorMessage" colspan="20" align="center">
         <table border="0" cellpadding="0" cellspacing="0" width="100%" align="center">
      <tr width="100%">
       <td class="FrameTitle" colspan="20" align="center">
               <%
                if( action.equals( "AddUser" ) )
                {
               %>
         <bean:message key="screen.adduser.title"/>
                <%
                }
                else
                {
                %>
         <bean:message key="screen.viewuser.title"/>
                <%
                }
                %>
       </td>
      </tr>
      <tr width="100%">
       <td class="" colspan="20" align="center">
            &nbsp;
       </td>
      </tr>
      <tr width="100%">
       <td class="ErrorMessage" colspan="20" align="center">
            <html:errors/>
       </td>
      </tr>
      <tr width="100%" align="center">
       <td colspan="20" align="center">
        <table border="0" cellpadding="0" cellspacing="0" width="100%" align="center">
         <tr width="100%">
          <td class="LabelDisplay" colspan="4" width="20%">
           User ID:
           &nbsp;
          </td>
          <td class="" colspan="4" width="20%">
           &nbsp;
           <html:text property="userID" size="8" maxlength="8" />
          </td>
          <td class="" colspan="4" width="20%">
           &nbsp;
          </td>
          <td class="" colspan="4" width="20%">
           &nbsp;
          </td>
          <td class="" colspan="4" width="20%">
           &nbsp;
          </td>
         </tr>
         <tr width="100%">
          <td class="LabelDisplay" colspan="4" width="20%">
           First Name:
           &nbsp;
          </td>
          <td class="" colspan="4" width="20%">
           &nbsp;
           <html:text property="firstName" size="14" maxlength="14" />
          </td>
          <td class="" colspan="4" width="20%">
           &nbsp;
          </td>
          <td class="LabelDisplay" colspan="4" width="20%">
           Last Name:
           &nbsp;
          </td>
          <td class="" colspan="4" width="20%">
           &nbsp;
           <html:text property="lastName" size="20" maxlength="20" />
          </td>
         </tr>
         <tr width="100%">
          <td class="LabelDisplay" colspan="4" width="20%">
           Phone Number:
           &nbsp;
          </td>
          <td class="" colspan="4" width="20%">
           &nbsp;
           <html:text property="phoneNumber" size="16" maxlength="16" />
          </td>
          <td class="" colspan="4" width="20%">
           &nbsp;
          </td>
          <td class="LabelDisplay" colspan="4" width="20%">
           Fax Number:
           &nbsp;
          </td>
          <td class="" colspan="4" width="20%">
           &nbsp;
           <html:text property="faxNumber" size="16" maxlength="16" />
          </td>
         </tr>
         <tr width="100%">
          <td colspan="20" align="center">
           &nbsp;
          </td>
         </tr>
         <tr width="100%">
          <td class="Heading1" colspan="20" align="center">
           Change Password
          </td>
         </tr>
         <tr width="100%">
          <td class="LabelDisplay" colspan="20" align="center">
                     <center>
           Please enter the new password and then verify the new password by 
re-entering.
                      <br>
                      Please remember that passwords are case sensitive.
                      </center>
          </td>
         </tr>
         <tr width="100%">
          <td class="LabelDisplay" colspan="10" align="center">
                     New Password:
          </td>
          <td colspan="10" align="left">
                     &nbsp;
           <html:password property="newPassword" size="16" maxlength="16" />
          </td>
         </tr>
         <tr width="100%">
          <td class="LabelDisplay" colspan="10" align="center">
                     Verify New Password:
          </td>
          <td colspan="10" align="left">
                     &nbsp;
           <html:password property="verifyNewPassword" size="16" maxlength="16" />
          </td>
         </tr>
         <tr width="100%">
          <td colspan="20" align="center">
           &nbsp;
          </td>
         </tr>
         <tr width="100%">
          <td colspan="20" align="center">
                  <%
                   if( action.equals( "AddUser" ) )
                   {
                  %>
            <input type="submit" name="cmdAddUser" value="Add" onClick="return( 
validateAdd() );">
                   <%
                   }
                   else
                   {
                   %>
            <input type="submit" name="cmdUpdateUser" value="Update" onClick="return( 
validateUpdate() );">
                       &nbsp;&nbsp;&nbsp;
            <input type="submit" name="cmdDeleteUser" value="Delete" onClick="return( 
validateDelete() );">
                   <%
                   }
                   %>
          </td>
         </tr>
        </table>
       </td>
      </tr>
     </table>
        </td>
      </tr>
    </table>
 </html:form>
 </body>
</html>

UserAction.java
---------------------

package strutsapp1.user;

import java.io.PrintWriter;
import java.io.IOException;
import java.sql.Connection;
import sun.jdbc.rowset.CachedRowSet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionServlet;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;

import strutsapp1.db.Database;
import strutsapp1.db.SystemUser;
import strutsapp1.util.Error;

public final class UserAction extends Action
{
 private String objectName = "SearchUserAction";

 public ActionForward perform( ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response )
      throws IOException, ServletException
  {
   boolean status = false;

    String methodName = "public ActionForward perform( ActionMapping mapping, 
ActionForm form, HttpServletRequest request, HttpServletResponse response ) throws 
IOException, ServletException";
    
   String userID = "";
    String firstName = "";
    String lastName = "";
    String phoneNumber = "";
    String faxNumber = "";
    String addedBy = "";
    String changedBy = "";

    PrintWriter out = null;

    Connection connection = null;
    CachedRowSet searchResult = null;
    HttpSession session = null;

    ActionServlet actionServlet = null;
    ActionForward actionForward = null;
    ActionErrors actionErrors = null;

    Database database = null;
    SystemUser systemUser = null;

  out = response.getWriter();
    session = request.getSession();
    actionErrors = new ActionErrors();

  //Retrieve the form parameter values from the bean.
  userID = ( ( UserForm ) form ).getUserID();
   firstName = ( ( UserForm ) form ).getFirstName();
   lastName = ( ( UserForm ) form ).getLastName();
    phoneNumber = ( ( UserForm ) form ).getPhoneNumber();
    faxNumber = ( ( UserForm ) form ).getFaxNumber();
    addedBy = ( String ) session.getAttribute( "currentUserID" );
    changedBy = addedBy;

    /*
    System.out.println( "User ID = [" + userID + "] <br>" );
    System.out.println( "First Name = [" + firstName + "] <br>" );
    System.out.println( "Last Name = [" + lastName + "] <br>" );
    System.out.println( "Phone Number = [" + phoneNumber + "] <br>" );
    System.out.println( "Fax Number = [" + faxNumber + "] <br>" );
    System.out.println( "Added By = [" + addedBy + "] <br>" );
    System.out.println( "Changed By = [" + changedBy + "] <br>" );
    */

    //Get the servlet.
    actionServlet = this.getServlet();

    database = new Database();
    status = database.openConnection( this );

    //If we cannot open the database connection then don't proceed.
    if( status )
    {
     //Proceed when there is a valid connection.
     connection = database.getConnection();

      //Check to see that the user id and password is valid. 
      systemUser = new SystemUser();

      //If add user button was clicked.
      if( request.getParameter( "cmdAddUser" ) != null )
      {
       status = systemUser.isValidUser( connection, userID );

        if( ! status )
        {
     systemUser.setUserID( userID );
     systemUser.setFirstName( firstName );
     systemUser.setLastName( lastName );
     systemUser.setPhoneNumber( phoneNumber );
     systemUser.setFaxNumber( faxNumber );
     systemUser.setAddedBy( addedBy );
     systemUser.setChangedBy( changedBy );

        status = systemUser.insert( connection );
          if( ! status )
          {
           actionErrors.add( ActionErrors.GLOBAL_ERROR, new ActionError( 
"error.user.addfailed" ) );
            database.rollback();
          }
          else
          {
            database.commit();
            systemUser.retrieve( connection, userID );
          }
        }
        else
        {
         actionErrors.add( ActionErrors.GLOBAL_ERROR, new ActionError( 
"error.user.userid.exists" ) );
        }
      }

      //If the update button was clicked.
      if( request.getParameter( "cmdUpdateUser" ) != null )
      {
       status = systemUser.isValidUser( connection, userID );

        if( status )
        {
     systemUser.setFirstName( firstName );
     systemUser.setLastName( lastName );
     systemUser.setPhoneNumber( phoneNumber );
     systemUser.setFaxNumber( faxNumber );
     systemUser.setChangedBy( changedBy );

        status = systemUser.update( connection, userID );
          if( ! status )
          {
           actionErrors.add( ActionErrors.GLOBAL_ERROR, new ActionError( 
"error.user.updatefailed" ) );
            database.rollback();
          }
          else
          {
            database.commit();
            systemUser.retrieve( connection, userID );
          }
        }
        else
        {
         actionErrors.add( ActionErrors.GLOBAL_ERROR, new ActionError( 
"error.user.userid.notexists" ) );
        }
      }

      //If the delete button was clicked.
      if( request.getParameter( "cmdDeleteUser" ) != null )
      {
       status = systemUser.isValidUser( connection, userID );

        if( status )
        {
        status = systemUser.delete( connection, userID );
          if( ! status )
          {
           actionErrors.add( ActionErrors.GLOBAL_ERROR, new ActionError( 
"error.user.deletefailed" ) );
            database.rollback();
          }
          else
          {
            database.commit();
          }
        }
        else
        {
         actionErrors.add( ActionErrors.GLOBAL_ERROR, new ActionError( 
"error.user.userid.notexists" ) );
        }
      }
    }
    else
    {
     actionErrors.add( ActionErrors.GLOBAL_ERROR, new ActionError( 
"error.database.unknown" ) );
    }

    database.closeConnection();

    if( status )
    {
     if( request.getParameter( "cmdAddUser" ) != null )
      {
        actionForward = new ActionForward( "/user.do?Action=ViewUser&UserID=" + 
userID, true );
      }
      else if( request.getParameter( "cmdUpdateUser" ) != null )
      {
        actionForward = new ActionForward( "/user.do?Action=ViewUser&UserID=" + 
userID, true );
      }
      else if( request.getParameter( "cmdDeleteUser" ) != null )
      {
      actionForward = mapping.findForward( "searchUser" );
      }
    }
    else
    {
     actionForward = mapping.findForward( "searchUser" );
    }

    saveErrors( request, actionErrors );
    return( actionForward );
  }

  public static void main( String arguments[] )
  {
  }
}

UserForm.java
---------------
package strutsapp1.user;

import javax.servlet.http.HttpServletRequest;

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

import strutsapp1.util.General;

public class UserForm extends ActionForm
{
 private String userID = "";
  private String password = "";
  private String newPassword = "";
  private String verifyNewPassword = "";
  private String firstName = "";
  private String lastName = "";
  private String phoneNumber = "";
  private String faxNumber = "";

  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 String getNewPassword()
  {
   return( ( newPassword == null ? "" : newPassword ) );
  }
  public void setNewPassword( String newPassword )
  {
   this.newPassword = ( newPassword == null ? "" : newPassword );
  }

  public String getVerifyNewPassword()
  {
   return( ( verifyNewPassword == null ? "" : verifyNewPassword ) );
  }
  public void setVerifyNewPassword( String verifyNewPassword )
  {
   this.verifyNewPassword = ( verifyNewPassword == null ? "" : verifyNewPassword );
  }

  public String getFirstName()
  {
   return( ( firstName == null ? "" : firstName ) );
  }
  public void setFirstName( String firstName )
  {
   this.firstName = ( firstName == null ? "" : firstName );
  }

  public String getLastName()
  {
   return( ( lastName == null ? "" : lastName ) );
  }
  public void setLastName( String lastName )
  {
   this.lastName = ( lastName == null ? "" : lastName );
  }

  public String getPhoneNumber()
  {
   return( ( phoneNumber == null ? "" : phoneNumber ) );
  }
  public void setPhoneNumber( String phoneNumber )
  {
   this.phoneNumber = ( phoneNumber == null ? "" : phoneNumber );
  }

  public String getFaxNumber()
  {
   return( ( faxNumber == null ? "" : faxNumber ) );
  }
  public void setFaxNumber( String faxNumber )
  {
   this.faxNumber = ( faxNumber == null ? "" : faxNumber );
  }

  public ActionErrors validate( ActionMapping mapping, HttpServletRequest request )
  {
   ActionErrors actionErrors = new ActionErrors();

  if( this.getUserID().equals( "" ) )
   {
   actionErrors.add( "userID", new ActionError( "error.user.userid.blank" ) );
    }

    if( request.getParameter( "cmdAddUser" ) != null ||
      request.getParameter( "cmdUpdateUser" ) != null )
    {
     //If the mandatory entries are blank, then don't proceed.
     if( this.getFirstName().equals( "" ) )
     {
    actionErrors.add( "userID", new ActionError( "error.user.firstname.blank" ) );
     }

     if( ! this.getNewPassword().equals( this.getVerifyNewPassword() ) )
     {
    actionErrors.add( "userID", new ActionError( "error.user.password.notsame" ) );
     }
    }

    return( actionErrors );
  }
}


Reply via email to