我在 Struts 1 中有一个操作,它使用输入数据和对象的选择选项转发到 JSP。我希望,当我更改选择选项时,同一 JSP 中的日期也会更改。我遵循调试模式,表单bean采用更新后的值,但在JSP中它保留旧数据。
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>
行动:
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;
}
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;
}
}
任何引用表单 bean 的操作都会调用
reset()
方法。
如果操作具有
struts-config.xml
属性,您可以在 name
中查看操作是否配置为使用表单。
如果它有 name 属性但没有
scope
属性,则默认范围是会话范围。
每次调用一个操作时,都会从
session
范围中获取表单 bean 并使用 reset()
方法对其进行初始化。然后它会调用 execute()
,您可以在其中修改该值。
如果您在重置后没有修改表单的值,那么它将保留旧值。如果您希望设置新值,请始终在
reset()
方法中对属性使用空值。