View Full Version : FormController
radone
Jan 24th, 2005, 03:38 AM
Can anyone explain me what this part of code do?
return new ModelAndView("countryList", Constants.COUNTRY_LIST,
manager.getObjects(Country.class));
I guess the result is that I can use variable "countryList" in my JSP file.
What should I do if I need to display on 1 page:
- list of Countries
- list of Towns
Together?
Thanks in advance.
[/code]
huijzer
Jan 24th, 2005, 04:29 AM
You are returning a new ModelAndView object with three parameters:
1. viewName - name of the View to render, to be resolved by the DispatcherServlet
2. modelName - name of the single entry in the model
3. modelObject - the single model object
This means that the view that maps to "countryList" has the list of countries available as Constants.COUNTRY_LIST
If you want to pass in both countries and towns, use a different constructor for ModelAndView:
Map myModel = new HashMap();
myModel.put(Constants.COUNTRY_LIST, manager.getObjects(Country.class));
myModel.put(Constants.TOWN_LIST, manager.getObjects(Town.class));
return new ModelAndView(getViewName(), myModel);
Hope this helps,
Arjan Huijzer
huijzer
Jan 24th, 2005, 04:30 AM
Sorry, that last line of code should read:
return new ModelAndView("countryList", myModel);
olivier
Jan 24th, 2005, 05:09 AM
Just one more precision, countryList, is your viewName, so it's not a variable that you will be able to use in your jsp.
To retrieve the country list, you will use the variable :
Constants.COUNTRY_LIST
radone
Jan 27th, 2005, 04:01 AM
If I need to display list even on non-submit action (viewing)- is it suitable to create
Map in formBackingObject too?
Somthing like this:
protected Object formBackingObject(HttpServletRequest request)
throws Exception {
if (log.isDebugEnabled()) {
log.debug("entering 'formBackingObject' method...");
}
String id = request.getParameter("id");
Town town = null;
if (!StringUtils.isEmpty(id)) {
town = townManager.getTown(id);
return town;
} else {
Country cc = new Country();
cc.setCode("CZ"); // Dafault opinion
town = new Town();
town.setCountry(cc);
}
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!!!!!!!!!!!!!!!!
// return town;
Map resultMap = new HashMap();
resultMap.put(Constants.COUNTRY_LIST, manager.getObjects(Country.class));
resultMap.put(Constants.TOWN_LIST, manager.getObjects(Town.class));
return resultMap;
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!!!!!!!!!!!!!!!!
}
Thanx in advance.
radone
Jan 27th, 2005, 04:33 AM
For anyone having the common problem - look at
protected Map referenceData(HttpServletRequest request) method
from SimpleFormController class
vBulletin® v3.7.3, Copyright ©2000-2008, Jelsoft Enterprises Ltd.