valeur SELECTED dans un select
Bonjour,
J'ai un formulaire de type AbstractWizardFormController qui permet la selection d'une option dans une liste d'objets. Tout fonctionne si ce n'est que lorsque l'option est déjà connue, la valeur SELECTED ne se met pas par défaut..
La liste est fournie via le controller comme ceci:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
@Override
protected Map<Object,Object> referenceData(HttpServletRequest request, Object command,
Errors errors, int page) throws Exception {
Map<Object, Object> dataMap = new HashMap<Object, Object>();
tlocationManager.loadClass(TypeLieu.class);
dataMap.put("typeLocList",this.tlocationManager.getList());
tcommManager.loadClass(TypeCommunication.class);
dataMap.put("typeCommList",this.tcommManager.getList());
initiativManager.loadClass(Initiative.class);
dataMap.put("initiList",this.initiativManager.getList());
return dataMap;
} |
J'affiche la liste dans le formulaire de ma jsp :
Code:
1 2 3 4 5 6 7 8 9 10
|
<form:form method="POST" commandName="rapport">
Initiative :
<form:select path="initiative">
<form:option value="-1" label="Veuillez choisir..."/>
<form:options items="${initiList}" itemValue="initiativId" itemLabel="initiativNom" />
</form:select><font color="red"><form:errors path="initiative"/></font>
<br/>
</form:form> |
L'objet Rapport contient un objet Initiative, pour faire le lien entre l'identifié retourné par le formulaire et mon objet Initiative, j'utilise ceci :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
@Override
protected void initBinder(HttpServletRequest request,
ServletRequestDataBinder binder) throws Exception {
df.setLenient(true);
binder.registerCustomEditor(Date.class, new CustomDateEditor(df, false));
/*
CommonTypePropertyEditor<Initiative> initTpe = new CommonTypePropertyEditor<Initiative>(initiativManager,Initiative.class,"getInitiativId()");
binder.registerCustomEditor(Initiative.class,initTpe);
*/
InitiativeTypePropertyEditor initTpe = new InitiativeTypePropertyEditor();
initTpe.setInitiativManager(initiativManager);
binder.registerCustomEditor(Initiative.class,initTpe);
CommonTypePropertyEditor<TypeCommunication> tCommTpe = new CommonTypePropertyEditor<TypeCommunication>(tcommManager,TypeCommunication.class,"tcommId");
binder.registerCustomEditor(TypeCommunication.class,tCommTpe);
CommonTypePropertyEditor<TypeLieu> tLocTpe = new CommonTypePropertyEditor<TypeLieu>(tlocationManager,TypeLieu.class,"tlieuId");
binder.registerCustomEditor(TypeLieu.class,tLocTpe);
} |
Le propertyEditor en détail :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
public class CommonTypePropertyEditor<R> extends PropertyEditorSupport{
private CommonManager<R> commonManager;
private Class<R> classBean;
private String idFieldName;
public CommonTypePropertyEditor(CommonManager<R> commonManager,Class<R> classBean,String idFieldName)
{
this.classBean=classBean;
this.commonManager = commonManager;
this.idFieldName = idFieldName;
}
@Override
public void setAsText(String arg0) throws IllegalArgumentException {
commonManager.loadClass(classBean);
R myInst = commonManager.getObjById(Long.parseLong(arg0));
this.setValue(myInst);
}
} |
Jusque là tout va bien, je récupère bien la bonne valeur, mon rapport est bien constitué de mon objet Initiative retourné par le formulaire..
Le problème est que, lorsque je reviens sur la page jsp où se trouve la liste de choix (select), l'option de mon select, qui est déjà connue, n'est pas "SELECTED" ...
Je suppose parce qu'il attend un string contenant l'identifiant et qu'il trouve un objet de type Initiative..
Comment puis-je résoudre ceci ?