wood burning stoves 2.0*
The moose likes Struts and the fly likes Dynamic iterate checkbox Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Dynamic iterate checkbox" Watch "Dynamic iterate checkbox" New topic
Author

Dynamic iterate checkbox

Albert Mora
Greenhorn

Joined: Feb 14, 2007
Posts: 2
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

Joined: Feb 15, 2005
Posts: 4864
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:


Merrill
Consultant, Sima Solutions
Dave Hayward
Greenhorn

Joined: Feb 28, 2007
Posts: 14
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!


The worst thing with having everything is finding a place in the garage to keep it.
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
@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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Dynamic iterate checkbox
 
Similar Threads
how to obtain list object from an actionform
Struts, Iterate/Index/Checkboxes
Iterator and multibox
A issue in Struts1.x
Problem with IndexId attribute