Sorry if this has been asked somewhere else, but I have looked all around, found some answers, but not a complete example and I am still in doubts on this one.
So, I am adding an Autopopulating list from my Spring controller to my jsp and I would like to add items on the list inside my javascript/jquery function. Is it possible?
I tried the code below to test the functionality but it did not work (the list elements did not show up at all at the generated html). So Im unsure if Im messing up with the javascrit/spring/jsp syntax or if it is just not possible.
Here is the code:
Controller code:
@RequestMapping(value="/create_custobject.html",method = RequestMethod.GET)
public ModelAndView showCreateCustObjectPage() {
Map<String, Object> model = new HashMap<String, Object>();
CreateObjectForm form = new CreateObjectForm();
model.put("createObjectform", form);
return new ModelAndView("create_custobject", model) ;
}
Form code:
public class CreateObjectForm {
private AutoPopulatingList<Criteria> ruleArray = new AutoPopulatingList<Criteria>(Criteria.class);
public AutoPopulatingList<Criteria> getRuleArray() {
return ruleArray;
}
public void setRuleArray(AutoPopulatingList<Criteria> ruleArray) {
this.ruleArray = ruleArray;
}
public CreateObjectForm() {}
}
Criteria code:
public class Criteria{
String attribute;
String operator;
//... constructor + getters and setters
}
javascript/jquery code (on the same page as the jsp one):
<script type="text/javascript">
$(document).ready(function(){
//startup functionality
var i = 0;
document.getElementById("addCriteria").onclick = function() {
$("#msgid").html("${ruleArray[i].attribute}");
${ruleArray[i].attribute} = $('#attributeValue').val();
${ruleArray[i].operator} = $('#operatorValue').val();
i++;
}
}
for existing items in your form use jstl as
this will render form like this
then you simply add with javascript new form “lines” with coresponding indexes
for example
I think that if you are using spring 3 + you don’t need to use AutopopulatingList , Any collection should be enough.