brave_heart
brave_heart

Reputation: 1

The form bean takes the updated value, but in JSP it keeps the old data in Struts 1

I have an action in Struts 1 that forwards to an JSP with input data and a select option of the object. I want, when I change the the select option, the date in the same JSP changes also. I follow the debug mode, the form bean takes the updated value, but in JSP it keeps the old data.

The JSP:

    <table width="90%" border="0" cellspacing="0" cellpadding="3"
            align="center">
            <tr>
                <td width="22%" class="TableInfo"><bean:message
                        bundle="drive" key="etiquette.form.support" /></td>
                <td width="40%" class="TableInfo"><span class="TabData">
                              <html:select name="colisageForm" property="code"  styleId="mySelect" onchange="submitGetSupport('${root}/getSupportAction.do?methode=getSupport');" >
                              <html:option value="0">--Select Support--</html:option>
                              <html:optionsCollection name="colisageForm"
                                property="listSupport" label="libelle" value="code"  />
                             </html:select>
                </span></td>
           </tr>
            
            <!-- debut ce code cause un probleme quon on clique sur le button retour / CODE A ENLEVER  
            <tr>
                <td width="22%" class="TableInfo"><bean:message
                        bundle="drive" key="etiquette.form.support" /></td>
                <td width="40%" class="TableInfo"><span class="TabData">
                              <html:select name="colisageForm" property="listSupport" styleId="mySelect1" onchange="charge1();">
                              <logic:iterate  id ="supp" name="colisageForm" property="listSupport" indexId="numIndex">
                                 <option value="${numIndex}"><bean:write name="supp" property="libelle"  /></option>
                              </logic:iterate>
                     </html:select>
                </span></td>
            </tr>     -->
            
             <!-- fin -->
    
 
 <nested:present  name="colisageForm" property="supportCommande"  > 
<!--    <bean:define  id="supportCommande" name="colisageForm" property="listSupport[0]" ></bean:define> -->
    ${supportCommande.code }
    <tbody id="dimSupport"  >
     
          <tr>
             <td width="40%" class="ChampTexteLibelle"><bean:message key="etiquette.form.quantite" bundle="drive"/> </td>
              <td width="20%" class="TabData"><nested:text  name="supportCommande" property="quantite" styleClass="ChampTexte" size="1" maxlength="4"   /></td>
            <td width="40%" class="ChampTexteLibelle"><bean:message key="etiquette.form.longueur" bundle="drive"/> </td>
              <td width="20%" class="TabData"><nested:text  name="supportCommande" property="longueur" styleClass="ChampTexte" size="1" maxlength="4"   /></td>
            
           </tr>
            <tr>
             <td width="40%" class="ChampTexteLibelle"><bean:message key="etiquette.form.largeur" bundle="drive"/> :</td>
              <td width="20%" class="TabData"><nested:text  name="supportCommande" property="largeur" styleClass="ChampTexte" size="1" maxlength="4"   /></td>
            <td width="40%" class="ChampTexteLibelle"><bean:message key="etiquette.form.hauteur" bundle="drive"/> :</td>
              <td width="20%" class="TabData"><nested:text  name="supportCommande" property="hauteur" styleClass="ChampTexte" size="1" maxlength="4"   /></td>
            
           </tr>

           
            <tr>
             <td width="40%" class="ChampTexteLibelle"><bean:message key="etiquette.form.poids" bundle="drive"/> :</td>
              <td width="20%" class="TabData"><nested:text  name="supportCommande" property="poids" styleClass="ChampTexte" size="1" maxlength="4"   /></td>
           
             
           </tr>
           
    </tbody> 
  
  
 
 </nested:present>         
</table>

The Action:

public ActionForward getSupport(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        ColisageForm cf = (ColisageForm) form;
        ActionForward forward = mapping.getInputForward();
        // forward.setRedirect(true);
        String code = cf.getCode();
        SupportCommande supportCommande = new SupportCommande();
        ArrayList<SupportCommande> list = cf.getListSupport();
        for (int i = 0; i < list.size(); i++) {

            if (code.equals(list.get(i).getCode())) {
                supportCommande = list.get(i);

            }

        }
        // request.setAttribute("sc", supportCommande);
        cf.setSupportCommande(supportCommande);
        // return mapping.findForward("resterSupportCommande");

        // return mapping.findForward("resterSupportCommande");
        return forward;

    }

The Form Bean:

package com.brico.depot.drive.form;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

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

import com.brico.depot.bapi.DriveBapi;
import com.brico.depot.drive.CommandeDrive;
import com.brico.depot.drive.DriveEcranNumero;
import com.brico.depot.drive.StatutCommande;
import com.brico.depot.drive.SupportCommande;
import com.brico.depot.logistique.Rayon;
import com.brico.depot.session.UserBean;

public class ColisageForm extends ActionForm {

    private static final long serialVersionUID = 1L;
    private UserBean user;
    private DriveBapi driveBapi;
    private String numCommande;
    private String statut;
    private String code;
    private String quantite;

    private String rayon;
    private String dateDebut;
    private String dateFin;
    private String buttLbl;
    private ArrayList<StatutCommande> statutList = null;
    private ArrayList<Rayon> listRayons;
    private ArrayList<CommandeDrive> listCommandes;
    private ArrayList<SupportCommande> listSupport = null;
    private SupportCommande supportCommande = null;

    public String getButtLbl() {
        return buttLbl;
    }

    public void setButtLbl(String buttLbl) {
        this.buttLbl = buttLbl;
    }

    /**
     * Retourne la liste de statut de l'ecran Preparer
     *
     * @return Liste des statut pour l'ecran Preparer
     */
    public ArrayList<StatutCommande> getStatutList() {
        driveBapi = new DriveBapi(user);
        if (this.statutList == null) {
            statutList = (ArrayList<StatutCommande>) driveBapi.getListStatuts(DriveEcranNumero.ECRAN_COLISAGE);

            if (statutList != null && statutList.size() > 0) {
                Set<StatutCommande> statutListDedoublonnee = new HashSet<StatutCommande>(statutList);
                statutList = new ArrayList<StatutCommande>(statutListDedoublonnee);
            }
        }
        return statutList;
    }

    public void setStatutList(ArrayList<StatutCommande> statutList) {
        this.statutList = statutList;
    }

    public ArrayList<Rayon> getListRayons() {
        return listRayons;
    }

    public void setListRayons(ArrayList<Rayon> listRayons) {
        this.listRayons = listRayons;
    }

    public String getNumCommande() {
        return numCommande;
    }

    public void setNumCommande(String numCommande) {
        this.numCommande = numCommande;
    }

    public String getStatut() {
        return statut;
    }

    public void setStatut(String statut) {
        this.statut = statut;
    }

    public String getRayon() {
        return rayon;
    }

    public void setRayon(String rayon) {
        this.rayon = rayon;
    }

    public String getDateDebut() {
        return dateDebut;
    }

    public void setDateDebut(String dateDebut) {
        this.dateDebut = dateDebut;
    }

    public String getDateFin() {
        return dateFin;
    }

    public void setDateFin(String dateFin) {
        this.dateFin = dateFin;
    }

    public ArrayList<CommandeDrive> getListCommandes() {
        return listCommandes;
    }

    public void setListCommandes(ArrayList<CommandeDrive> listCommandes) {
        this.listCommandes = listCommandes;
    }

    public ArrayList<SupportCommande> getListSupport() {

        return listSupport;
    }

    public void setListSupport(ArrayList<SupportCommande> listSupport) {
        this.listSupport = listSupport;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getQuantite() {
        return quantite;
    }

    public void setQuantite(String quantite) {
        this.quantite = quantite;
    }

    public SupportCommande getSupportCommande() {
        return supportCommande;
    }

    public void setSupportCommande(SupportCommande supportCommande) {
        this.supportCommande = supportCommande;
    }

    /**
     * Recupere le User de la session
     * 
     * @param mapping
     *            ActionMapping
     * @param request
     *            parametre de la request
     */
    public void reset(ActionMapping mapping, HttpServletRequest request) {
        user = (UserBean) request.getSession(true).getAttribute("USERBEAN");
        this.listCommandes = null;

        SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
        this.dateDebut = formatter.format(new Date(System.currentTimeMillis()));
        this.dateFin = "31/12/9999";
        this.supportCommande = null;
    }

}

Upvotes: 0

Views: 503

Answers (1)

Roman C
Roman C

Reputation: 1

The reset() method is invoked for any action that reference a form bean.

You can see in the struts-config.xml if action is configured to use a form or not if it has a name attribute.

If it has a name attribute but doesn't have scope attribute, the default scope is a session scope.

Each time you invoke an action that pick up the form bean from the session scope and initializes it with reset() method. Then it calls execute() where you can modify the value.

If you didn't modify the form with a value after it resettled then it will keep the old value. If you want the new value to be set always use empty value to the property in the reset() method.

Upvotes: 1

Related Questions