PDA

View Full Version : How to show a checkbox list


cacho
Aug 26th, 2005, 03:42 AM
Hi All

I´m new to Spring.

I´ve a controller that will retrieve some data from a DB and need to show it as a list of checkboxes in a jsp.

Then, the user will select some checks and submit them again to my app.

I´ve been looking in documentation about how to do this. I guess I understand how to make the controller but I really can´t see clear how to make a bind to show checkboxes list and how to recover them after submit.

I´ve been looking specific related examples on the net but I couldn´t found.

Could somebody please clarify me ?

Thanks in advance

C

Colin Yates
Aug 26th, 2005, 07:22 AM
Ah, the age old problem ;)

The way I deal with this is have the YourObject[] setSelectedItems on the command, and then in referenceData create a map which contains all possible options and a true/false depending on whether the object is selected.

You will want to investigate propertyEditors for converting from the string id to retrieving the appropriate object and registering custom binders on initBinder ;)

Keep posting if you need more help ;)

cacho
Aug 26th, 2005, 06:04 PM
Ah, the age old problem ;)

The way I deal with this is have the YourObject[] setSelectedItems on the command, and then in referenceData create a map which contains all possible options and a true/false depending on whether the object is selected.

You will want to investigate propertyEditors for converting from the string id to retrieving the appropriate object and registering custom binders on initBinder ;)

Keep posting if you need more help ;)

Thanks for your reply.

Yes, I really need more help. I'm totally new to Spring and there a lot of settings that drive me crazy and confused.

May be, if you have a sample code to show me, it could be easy to understand.

Thanks in advance

J

Colin Yates
Aug 27th, 2005, 10:51 AM
Try http://forum.springframework.org/showthread.php?t=17646 and read the spring MVC step by step guide ;)

cacho
Aug 30th, 2005, 05:56 AM
Try http://forum.springframework.org/showthread.php?t=17646 and read the spring MVC step by step guide ;)

Thanks for your reply.

I' ve read the MVC step by step guide, but it´s not totally useful for me in this matter.

I´ve found your example usefull but, nevertheless, I´m a little bit confused yet.

Let me show you what I did:

I´ve defined a class that represents a logged aplication with its url and sessionid


// LoggedApp.java
//
public class LoggedApp {
private String url;
//private String sessionId;

public String getUrl() {
return this.url;
}

public void setUrl(String url) {
this.url = url;
}

public String getSessionId() {
return this.sessionId;
}

public void setSessionId(String sessionId) {
this.sessionId = sessionId;
}

}


Then I did a backingObject:


class MyBackingObject {
private LoggedApp[] myLoggedApp;
private int x = 0;

public LoggedApp getObject(int x) {
return myLoggedApp[x];
}

public void setObject(LoggedApp mylogout) {
this.myLoggedApp[x] = myLoggedApp;
x++;
}
}


And then I tried to make a SimpleFormController


public final class MyLogoutController extends SimpleFormController {

/**
* The log instance.
*/
private final Log log = LogFactory.getLog(MyLogoutController.class);



public MyLogoutController() {
setCommandClass(LoggedApp.class);
setCommandName("loggedApp");
setFormView("myLogoutView");
setSuccessView("myLogoutViewSuccess");
}


public Map referenceData(HttpServletRequest request, Object command, Errors errors) throws Exception {
MyBackingObject form = (MyBackingObject) command;
Set selectedMyClasses = new HashSet().addAll(Arrays.asList(form.getMyClasses() ));

Map allMyClasses = new HashMap();
for (Iterator i = myDAO.getAllMyClasses(); i.hasNext(); ) {
MyClass myClass = (MyClass) i.next();
allMyClasses.put(myClass, new Boolean(selectedMyClasses.contains(myClass));
}

Map model = new HashMap();
model.put("allMyClasses", allMyClasses);
return model;
}
public void afterPropertiesSet() throws Exception {
}
}




Now I´ve some problems:

1 - Am I right with what I did above ?

2 - How could I bind all data with my form ? Should I do an onBind method ?

3 - How do I process received data from the form ?


I don't want others making my job but I need some help to clarify myself.


Thanks in advance

C

[/code]