Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Dynamic iterate checkbox

 
Albert Mora
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, i've a problem with the tag html:checkbox. With other tags (html:text) i try this and it's ok. I put one checkbox in a logic:iterate, and its prints me the data from the BBDD, but the problem is when i unckecked some checkbox and submit() the form, in the Form in Action(ActuMedioambiental.saveTareas()) not appears the new value... but if i check and unchecked checkbox the value in the Action is the correct.

My code:

tareas.jsp:

<logic:iterate indexId="i" name="actuMedioambientalForm" id="tarea" property="tareas">
<html:checkbox name="tarea" property="fechaPrevistaInd" value="true"></html:checkbox>
</logic:iterate>

ActuMedioambiental.java:

private ActionForward loadTareas(...) {
...
actuMedioambientalFrom.setTareas(ActuAmbientalesDAO.getTareasActualizacion(id));
...
}

private ActionForward saveTareas(...) {
ActuMedioambientalForm actuMedioambientalFrom = (ActuMedioambientalForm) form;
...
}


ActuMedioambientalForm.java:

public void setTareas(List tareas) {
this.tareas = tareas;
}

public List getTareas() {
return tareas;
}

public TareaBean getTarea(int index)
{
return (TareaBean)tareas.get(index);
}

public void setTarea(int index, TareaBean tarea) {
((TareaBean)(this.tareas.get(index))).setFechaIndFin(tarea.getFechaIndFin());
((TareaBean)(this.tareas.get(index))).setFechaIndInicio(tarea.getFechaIndInicio());
((TareaBean)(this.tareas.get(index))).setFechaPrevistaInd(tarea.getFechaPrevistaInd());
((TareaBean)(this.tareas.get(index))).setFechaRealInd(tarea.getFechaRealInd());
((TareaBean)(this.tareas.get(index))).setOrden(tarea.getOrden());
}

struts-config.xml:

<action path="/actuMedioambiental" type="es.indra.tram.control.action.actuMedioambiental.ActuMedioambiental" name="actuMedioambientalForm" scope="session" validate="false">
<forward name="tareas" path="/paginas/actuMedioambiental/tareas.jsp"/>
</action>

TareaBean.java:

private boolean fechaPrevistaInd = false;
private boolean fechaRealInd = false;
public void setFechaPrevistaInd(boolean fechaPrevistaInd) {
this.fechaPrevistaInd = fechaPrevistaInd;
}
public boolean getFechaPrevistaInd() {
return this.fechaPrevistaInd;
}
....


Thanks.
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This problem is due to an odd behavior of the HTML for checkboxes. If a checkbox is unchecked, no parameter is sent to the server, and hence, no setter is called in the Struts ActionForm. To get around this problem, you must override the reset method in your ActionForm, and in that method, set the value of each property associated with a checkbox to its unchecked value. Since struts calls the reset method before it calls the setters, any checkboxes that are checked will cause the setters to override the values set in the reset method. Here's an example:
 
Dave Hayward
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Merrill - a quick non-related question.

I have seen you use the @Override before re-defining the public void reset() method. Is that necessary? I don't have that when I override the reset() method. Thanks!
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Override is a java 1.5 annotation, so it only works if your development environment and your App Server supports Java SE 1.5. It's entirely optional, but I like it and have started using it because it gives me an added level of error checking. If I get one of the parameters wrong on the method, the compiler will tell me "Hey, you said you were overriding a method, but there's no method in any of your superclasses that matches that signature."

It also makes for good documentation. When looking at a class, it's very helpful to know which methods are overriding superclass methods.
[ March 01, 2007: Message edited by: Merrill Higginson ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic