i am trying to re-use my formbean which i used in my adding of a record and trying to 
use in editing my record.

i can't seem to show the data to the screen for editing.

what's wrong with my code? i am attaching my code in this
email. please help me.

many thanks in advance...

-Richard
========================================================================================================
package com.fltic.crm.form;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.util.MessageResources;

import java.util.Calendar;
import java.util.TimeZone;
import java.util.ArrayList;

import com.fltic.crm.UserActionMapping;

public class UserAccountForm extends ActionForm{
 protected int userid;
 protected String lastname;
 protected String name;
 protected String mi;
 protected String username;
 protected String password;
 protected String confirmpassword;
 protected String address;
 protected String provincecode;
 protected String citycode;
 protected String datecreated;
 protected String dateexpired;
 protected String dateexpiredmonth;
 protected String dateexpiredday;
 protected String dateexpiredyear;
 protected int createdby;

 //--
 public void setUserid(int userid){
  this.userid = userid;
 }

 public int getUserid(){
  return userid;
 }

 //--
 public void setLastname(String lastname){
  this.lastname = lastname;
 }

 public String getLastname(){
  return lastname;
 }

 //--
 public void setName(String name){
  this.name = name;
 }

 public String getName(){
  return name;
 }

 //--
 public void setMi(String mi){
  this.mi = mi;
 }

 public String getMi(){
  return mi;
 }

 //--
 public void setUsername(String username){
  this.username = username;
 }

 public String getUsername(){
  return username;
 }

 //--
 public void setPassword(String password){
  this.password = password;
 }

 public String getPassword(){
  return password;
 }

 //--
 public void setConfirmpassword(String confirmpassword){
  this.confirmpassword = confirmpassword;
 }

 public String getConfirmpassword(){
  return confirmpassword;
 }

 //--
 public void setAddress(String address){
  this.address = address;
 }

 public String getAddress(){
  return address;
 }

 //--
 public void setProvincecode(String provincecode){
  this.provincecode = provincecode;
 }

 public String getProvincecode(){
  return provincecode;
 }

 //--
 public void setCitycode(String citycode){
  this.citycode = citycode;
 }

 public String getCitycode(){
  return citycode;
 }

 //--
 public void setCreatedby(int createdby){
  this.createdby = createdby;
 }

 public int getCreatedby(){
  return createdby;
 }

 //--
 public void setDateexpired(String dateexpired){
  this.dateexpired = dateexpired;
 }

 //--
 public void setDateexpiredmonth(String dateexpired){
  this.dateexpiredmonth = dateexpired;
 }

 public String getDateexpiredmonth(){
  return dateexpiredmonth;
 }
 //--
 public void setDateexpiredday(String dateexpired){
  this.dateexpiredday = dateexpired;
 }

 public String getDateexpiredday(){
  return dateexpiredday;
 }

 //--
 public void setDateexpiredyear(String dateexpired){
  this.dateexpiredyear = dateexpired;
 }

 public String getDateexpiredyear(){
  return dateexpiredyear;
 }

 //--
 public String getDateexpired(){
  return dateexpiredyear+dateexpiredmonth+"-"+dateexpiredday;
 }

 public String getDatecreated(){
  return datecreated;
 }

  // This method is called with every request. It resets the Form
  // attribute prior to setting the values in the new request.
  public void reset(ActionMapping mapping, HttpServletRequest request) {
 System.err.println("---> entered UserAccountForm reset");
    this.username = "";
    this.password = "";
    this.confirmpassword = "";
    this.name = "";
    this.lastname = "";
    this.mi = "";
    this.address = "";
    HttpSession session = request.getSession();
    if( session.getAttribute("USERID") != null){
     this.createdby = Integer.parseInt((String)session.getAttribute("USERID"));
     }
    Calendar cal = Calendar.getInstance(TimeZone.getDefault());
  String DATE_FORMAT = "yyyy-MM-dd hh:mm:ss";
    java.text.SimpleDateFormat sdf =
          new java.text.SimpleDateFormat(DATE_FORMAT);
    sdf.setTimeZone(TimeZone.getDefault());
    this.datecreated = sdf.format(cal.getTime());
 }

  public ActionErrors validate(ActionMapping mapping,
    HttpServletRequest request) {

    System.err.println("---> entered UserAccountForm validate");

    ActionErrors errors = new ActionErrors();

    UserActionMapping userMapping =
      (UserActionMapping)mapping;

    // Does this action require the user to login
    if ( userMapping.isLoginRequired() ) {

      HttpSession session = request.getSession();
      if ( session.getAttribute("USER") == null ) {
        // return null to force action to handle login
        // error
        return null;
      }
    }

 if ( (username == null ) || (username.length() == 0) ) {
  errors.add("username", new ActionError("errors.userform.save.username.required"));
    }

 if ( (password == null ) || (password.length() == 0) ) {
  errors.add("password", new ActionError("errors.userform.save.password.required"));
    }

 if ( !password.equals(confirmpassword) ||
   ((confirmpassword == null) || (confirmpassword.length() ==0)) ) {
  errors.add("confirmpassword", new 
ActionError("errors.userform.save.password.failed"));
    }

 if ( (address == null ) || (address.length() == 0) ) {
  errors.add("address", new ActionError("errors.userform.save.address.required"));
    }

 if ( (name == null ) || (name.length() == 0) ) {
  errors.add("address", new ActionError("errors.userform.save.name.required"));
    }

 if ( (provincecode == null ) || (provincecode.length() == 0) ) {
  errors.add("provincecode", new 
ActionError("errors.userform.save.province.required"));
    }

 if ( (citycode == null ) || (citycode.length() == 0) ) {
  errors.add("citycode", new ActionError("errors.userform.save.city.required"));
    }

 if( (createdby <= 0) ) {
  errors.add("createdby", new ActionError("errors.cannot.save"));
 }
 
    return errors;
    }

}
==============================================================================================================
package com.fltic.crm.action.manager;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.ServletContext;
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.ActionErrors;
import org.apache.struts.action.ActionError;

import java.text.SimpleDateFormat;

import com.fltic.crm.UserActionMapping;
import com.fltic.crm.util.SQLDef;
import com.fltic.crm.util.Constants;
import com.fltic.crm.view.UserAccountInfo;
import com.fltic.crm.form.UserAccountForm;
import com.fltic.crm.service.manager.GetUserAccountService;

public class EditTmAccountAction extends Action {

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

 String tmUserId = null;
 String userId = null;
 String userType = null;

 SimpleDateFormat f = new SimpleDateFormat("MMM-dd-yyyy");

 System.err.println("\n----> Entering EditTmAccountAction\n");

 // Default target to success
    String target = new String(Constants.SUCCESS_KEY);

 System.err.println("---> Test mapping: " + mapping.getAttribute());

    UserActionMapping userMapping =
      (UserActionMapping)mapping;

 HttpSession session = request.getSession();
 tmUserId = (String) request.getParameter("tmuserid");
 userId = (String) session.getAttribute("USERID");
 userType = (String) session.getAttribute("USERTYPE");

    // Does this action require the user to login
    if ( (userMapping.isLoginRequired()) || (userId == null) ) {
      //Check if valid user
      System.err.println("<--- Checking userId " + userId);
      if ( userId == null ) {
        // The user is not logged in
        target = new String(Constants.LOGIN_KEY);
  ActionErrors errors = new ActionErrors();
        errors.add(ActionErrors.GLOBAL_ERROR,
   new ActionError("errors.login.required"));
        // Report any errors we have discovered back to
        // the original form
        if (!errors.empty()) {
          saveErrors(request, errors);
        }
      }
      //Check if authorized user
      System.err.println("<--- Comparing userType " + userType + " to " + 
Constants.MANAGER);
      if ( (userType != null) && !(userType.equals(Constants.MANAGER )) ){
       // Not authorized user
       target = new String(Constants.LOGIN_KEY);
       ActionErrors  errors = new ActionErrors();
       errors.add(ActionErrors.GLOBAL_ERROR,
   new ActionError("errors.login.notauthorized"));
        // Report any errors we have discovered back to
        // the original form
        if (!errors.empty()) {
          saveErrors(request, errors);
        }
      }
    }

    System.err.println("<--- EditTmAccountAction - Loading TM Account: " + tmUserId);
    GetUserAccountService service = null;
    UserAccountForm userAccountForm = null;
    if(tmUserId != null){
     service = new GetUserAccountService();
     userAccountForm = new UserAccountForm();
     service.setServlet(servlet);
     userAccountForm = 
(UserAccountForm)service.getUserAccount(Integer.parseInt(tmUserId));
     //form = (ActionForm)userAccountForm;
     form = service.getUserAccount(Integer.parseInt(tmUserId));
     if( form == null){
   System.err.println("---> userAccountForm is Null!");
   }
     System.err.println("---> Test form.getUserid: " + userAccountForm.getUserid());
     System.err.println("---> Test form.getUsername: " + 
userAccountForm.getUsername());
     System.err.println("---> Test mapping: " + mapping.getAttribute());
     //Save form in session for editing
     try{
      System.err.println("---> Mapping scope: " + mapping.getScope());
      System.err.println("---> Mapping attribute: " + mapping.getAttribute());
      session.setAttribute(mapping.getAttribute(), form);
  }catch (Exception e) {
        System.err.println("ERROR:"+e.getMessage());
        e.printStackTrace(System.err);
   }
 }
   // Forward to the appropriate View
   return (mapping.findForward(target));
 }
}
===================================================================================================================
package com.fltic.crm.service.manager;

import org.apache.struts.action.ActionForm;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.text.SimpleDateFormat;

import com.fltic.crm.util.SQLDef;
import com.fltic.crm.util.Constants;
import com.fltic.crm.form.UserAccountForm;
import com.fltic.crm.service.CrmService;

public class GetUserAccountService extends CrmService{


 public ActionForm getUserAccount(int usertype){

 UserAccountForm userAccount = null;
 Connection conn = null;
 PreparedStatement pstmt = null;
 ResultSet rs = null;
 String fullname = null;    //for debugging

 SimpleDateFormat f = new SimpleDateFormat("MMM-dd-yyyy");

 try {

  //Get a DataSource from CrmService
  conn = super.getDataSource().getConnection();
  pstmt = conn.prepareStatement(SQLDef.USERACCOUNTS_GETRECORD);
  pstmt.setInt(1,usertype);
  rs = pstmt.executeQuery();

  System.err.println("<--- Executing SQL : --->");
  System.err.println(SQLDef.USERACCOUNTS_GETRECORD);
  System.err.println("<---  SQL STATEMENT ENDS HERE --->");
  System.err.println("<--- GetUserAccountService getUserAccount Start of Result(s) 
---->");
  if ( rs.next() ) {
   userAccount = new UserAccountForm();
   userAccount.setUserid(rs.getInt("userid"));
   fullname = rs.getString("lastname") + ", " + rs.getString("name")
   + " " + rs.getString("mi");
   userAccount.setName(rs.getString("name"));
   userAccount.setPassword(rs.getString("name"));
   userAccount.setLastname(rs.getString("lastname"));
   userAccount.setMi(rs.getString("mi"));
   userAccount.setUsername(rs.getString("username"));
   userAccount.setProvincecode(rs.getString("provincecode"));
   userAccount.setCitycode(rs.getString("citycode"));
   userAccount.setDateexpired(f.format(rs.getDate("dateexpired")));
   System.err.println("userid: " + rs.getInt("userid")
    + " fullname: " + fullname + " DateCreated: " + rs.getString("datecreated"));
  }//end of while
  System.err.println("<--- GetUserAccountService getUserAccount End of Result(s) 
---->");
 }
 catch (SQLException e) {
  System.err.println(e.getMessage());
 }
 finally {
  if (rs != null) {
   try {
    rs.close();
   }
   catch (SQLException sqle) {
    System.err.println(sqle.getMessage());
   }
  rs = null;
  }
  if (pstmt != null) {
   try {
    pstmt.close();
   }
   catch (SQLException sqle) {
    System.err.println(sqle.getMessage());
   }
  pstmt = null;
  }
  if (conn != null) {
   try {
    conn.close();
   }
   catch (SQLException sqle) {
    System.err.println(sqle.getMessage());
   }
  conn = null;
  }
 }
 return userAccount;
 }
}
==============================================================================================================
<%@ 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" %>
<script src="/js/address.js" type="text/javascript" language="javascript"></script>
<script src="/js/date.js" type="text/javascript" language="javascript"></script>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
 <td width="30">&nbsp;</td>
 <td>
 <table width="100%" border="0" cellspacing="2" cellpadding="2" >
  <tr>
   <td colspan="2" class="normalblack">
    <h2> Edit TM Account </h2>
   </td>
  </tr>
  <!-- Error(s) here -->
  <html:errors />
  <!-- End of Error(s) here -->
  <html:form action="/SaveTmAccount"
   name="userAccountForm"
   type="com.fltic.crm.form.UserAccountForm"
   focus="username"
   method="post"
   >
  <tr class="normal">
   <td width="20%"> Username :</td>
   <td><html:text property="username" maxlength="20" styleClass="normalblack" /></td>
  </tr>
  <tr class="normal">
   <td width="20%"> Password :</td>
   <td><html:password property="password" maxlength="20" 
styleClass="normalblack"/></td>
  </tr>
  <tr class="normal">
   <td width="20%"> Confirm Password :</td>
   <td><html:password property="confirmpassword" maxlength="20" 
styleClass="normalblack"/></td>
  </tr>
  <tr class="normal">
   <td width="20%"> Name :</td>
   <td> <nobr>
    <html:text property="name" maxlength="35" styleClass="normalblack"/>
    <html:text property="lastname" maxlength="35" styleClass="normalblack"/>
    <html:text property="mi" style="width: 20" maxlength="1" styleClass="normalblack"/>
    </nobr>
    Firstname/Lastname/Mi
   </td>
  </tr>
  <tr class="normal">
   <td width="20%"> Address :</td>
   <td>
    <nobr>
     <html:text property="address" maxlength="35" styleClass="normalblack"/>
     Province:
     <html:select property="provincecode" styleClass="normalblack" 
onchange="province(form,'')">
                   <SCRIPT LANGUAGE="JavaScript">
           writeprovince('<bean:write name="userAccountForm" 
property="provincecode"/>');
         </script>
     </html:select>
     City:
     <html:select property="citycode" styleClass="normalblack">
                   <SCRIPT LANGUAGE="JavaScript">
           writecity('<bean:write name="userAccountForm" property="citycode"/>');
         </script>
         <option value="">All</option>
     </html:select>
    </nobr>
   </td>
  </tr>
  <tr class="normal">
   <td width="20%"> Date Expired :</td>
   <td> <nobr>
     <html:select property="dateexpiredmonth" styleClass="normalblack">
                   <SCRIPT LANGUAGE="JavaScript">
           writeMonthDefault('<bean:write name="userAccountForm" 
property="dateexpiredmonth"/>');
         </script>
     </html:select>
     <html:select property="dateexpiredday" styleClass="normalblack">
                   <SCRIPT LANGUAGE="JavaScript">
           writeDayDefault('<bean:write name="userAccountForm" 
property="dateexpiredday"/>');
         </script>
     </html:select>
     <html:select property="dateexpiredyear" styleClass="normalblack">
                   <SCRIPT LANGUAGE="JavaScript">
           writeYearDefault('<bean:write name="userAccountForm" 
property="dateexpiredyear"/>');
         </script>
     </html:select>
    </nobr>
   </td>
  </tr>
  <tr>
   <td colspan="2">
    &nbsp;
   </td>
  </tr>
  <tr>
   <td colspan="2">
    <html:hidden property="userid"/>
    <html:submit />
    <html:cancel />
    <html:reset />
   </td>
  </tr>
  </html:form>
 </table>
 </td>
</tr>
</table>
</body>
======================================================================================================

Reply via email to