[spring 3.0.5] MVC
I have on class like that:
public class Address {
private String street;
private String city;
public String getStreet() {
return this.street;
}
public void setStreet(String street) {
this.street = street;
}
public String getCity() {
return this.city;
}
public void setCity(String city) {
this.city = city;
}
}
My function is to rely on the possibility of adding an infinite number of addresses on the site (JSP with spring and form tag). Can somebody give a skeleton of such functionality in the controller?
Your question is a little unclear. The answer is twofold:
Displaying a list of addresses
In your jsp page, you can display the list of addresses using e.g., the
corejstl lib, here noted using the prefixc.Submitting addresses
If you’re looking to submit an undefined number of addresses, that’s a little more tricky. What you could do is create a javasript form template which upon a button press or so creates two additional form fields. Ensure each form field will get a unique name, e.g., :
Then you can iterate the fields using
request.getParameterMap(), where you construct your address options based on matching names (e.g., street_1 should be associated with city_1 and so forth).[EDIT] To give you an idea of what the javascript could look like, I give you this from the top of my head (note I haven’t tested this code, it’s there to give you an idea of what it might look like).
Then call the javascript when pressing a button:
If you decided to use e.g., jQuery this would be even easier. E.g.,