Conserver les valeurs d'éléments Set dans un POJO
Bonjour,
J'ai une classe POJO qui a des éléments Set :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| public class Device implements Serializable {
/**
*
*/
private static final long serialVersionUID = 3522966146250618503L;
/** identifier field */
private Integer id;
/** nullable persistent field */
private String userAgent;
/** nullable persistent field */
private String manufacturerName;
/** nullable persistent field */
private String model;
/** persistent field */
private Set j2meToDevices;
/** persistent field */
private Set styleToDevices;
/** full constructor */
public Device(Integer id, String userAgent, String manufacturerName, String model, Set j2meToDevices, Set styleToDevices) {
this.id = id;
this.userAgent = userAgent;
this.manufacturerName = manufacturerName;
this.model = model;
this.j2meToDevices = j2meToDevices;
this.styleToDevices = styleToDevices;
}
/** default constructor */
public Device() {
}
public void init() {
this.id = new Integer(0);
this.userAgent = "";
this.manufacturerName = "";
this.model = "";
this.j2meToDevices = d.getJ2meToDevices();
this.styleToDevices = d.getStyleToDevices();
}
// getter-setter ...
} |
L'id correspond à un champs auto_increment dans ma BDD
J'ai un formulaire dont les champs correspondent à cette classe.
Pour les Set il s'agit de multiple select.
Si je fais une erreur dans mon formulaire(par ex : oublier un champs obligatoire), le validator me retourne le formulaire avec les données des champs du device(userAgent, manufactuerName) mais tous les éléments multiple select qui correspondent aux Set sont vidés.
Ma classe controller :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
| public class DeviceAddFormController extends SimpleFormController {
private MetaDao metaDao;
private DeviceDao deviceDao;
private Set j2meSet = new HashSet();
private Set styleSet = new HashSet();
public Set getJ2meSet() {
return j2meSet;
}
public void setJ2meSet(Set j2meSet) {
this.j2meSet = j2meSet;
}
public MetaDao getMetaDao() {
return metaDao;
}
public void setMetaDao(MetaDao metaDao) {
this.metaDao = metaDao;
}
public DeviceDao getDeviceDao() {
return deviceDao;
}
public void setDeviceDao(DeviceDao deviceDao) {
this.deviceDao = deviceDao;
}
protected Map referenceData(HttpServletRequest request) throws Exception {
Map map = new HashMap();
map.put("manufacturers", this.metaDao.getManufacturers());
map.put("j2mes", this.metaDao.getJ2mes());
map.put("styles", this.metaDao.getStyles());
return map;
}
protected void initBinder(HttpServletRequest request,
ServletRequestDataBinder binder) throws Exception {
binder.registerCustomEditor(Integer.class, new IntegerCustomEditor());
}
protected ModelAndView onSubmit(HttpServletRequest request,
HttpServletResponse response, Object command,
org.springframework.validation.BindException be) throws Exception {
DevicesForm devicesForm;
devicesForm = (DevicesForm) command;
Integer deviceId = devicesForm.getDevice().getId();
String ua = devicesForm.getDevice().getUserAgent();
// Verify if the device exist ...
if (deviceDao.isDevicePresent(ua)) {
Map map = new HashMap();
map.put("warning", "global.device.exist");
return this.showForm(request, response, be, map);
}
// ... else get "Set" device informations
else {
// J2me list
String[] stJ2me = request.getParameterValues("j2me");
if (stJ2me != null && stJ2me != null && stJ2me.length != 0) {
j2meSet.clear();
for (int v = 0; v < stJ2me.length; v++) {
J2meToDevice jtd = new J2meToDevice(new J2meToDevicePK(
deviceId, stJ2me[v]));
j2meSet.add(jtd);
if (log.isDebugEnabled())
log.debug("jtd" + jtd.getComp_id().getJ2meId()
+ "************************** stJ2me[" + v
+ "] = " + stJ2me[v]);
}
}
// Style list
String[] stStyle = request.getParameterValues("style");
if (stStyle != null && stStyle.length != 0) {
styleSet.clear();
for (int v = 0; v < stStyle.length; v++) {
StyleToDevice styletd = new StyleToDevice(
new StyleToDevicePK(deviceId, stStyle[v]));
styleSet.add(styletd);
if (log.isDebugEnabled())
log.debug("stStyle[" + v + "] = "
+ styletd.getComp_id().getStyleId());
}
}
devicesForm.getDevice().setJ2meToDevices(j2meSet);
devicesForm.getDevice().setStyleToDevices(styleSet);
// Insert Device informations
deviceId = this.deviceDao.store(devicesForm.getDevice());
}
// get the property successView from devicemanager-servlet.xml
String action = getSuccessView();
return new ModelAndView(new RedirectView(action + "?id=" + deviceId));
}
} |
Comment faire pour qu'il me garde les valeurs sélectionnées dans mes multiple select si le formulaire est réaffiché pour des erreurs(ex: champs obligatoire incomplet)