I am new in spring,creating one application in spring3.0
I am using annotation spring.
i want to return on list from my controller method to jsp. where that list will be shown in selection box. following is the code of controller and jsp part.
Please help me to understand this.
public String showUserForm(ModelMap model)
{
User user = new User();
model.addAttribute(user);
List<String> lookingfor=service.getOptions();
//want to send this "lookingfor" to the jsp
return "welcome";
}
and JSP
<td>
<form:select path="lookingfor">
<form:option value="0" label="Select" />
<form:options items="${lookingfor}" itemValue="lookingfor" itemLabel="lookingfor" /> </form:select>
</td>
While adding user in Model, its working fine, but when i am adding lookingfor attribute.
model.addAttribute("lookingfor", lookingfor);
its giving following error.
SEVERE: Servlet.service() for servlet jsp threw exception
org.springframework.beans.NotReadablePropertyException: Invalid
property 'lookin gfor' of bean class [java.lang.String]: Bean property
'lookingfor' is not readab le or has an invalid getter method: Does
the return type of the getter match the parameter type of the setter?
You have to add the lookingFor list to the ModelMap object as an attribute:
model.addAttribute("lookingfor", lookingfor);
Also verify the usage of the attributes "itemValue" and "itemLabel", because both itemValue and itemLabel refer to properties of the items of lookingFor list. You're using the same value on both and that's kind of wrong.
You can use like this also
@RequestMapping("get_vendor_tests.htm")
public ModelAndView getVendorTests(@RequestParam int vendorId,Map<String, Object> map){
try {
map.put("vendor", vendorService.getVendor(vendorId));
return new ModelAndView("vendor_tests","message",map);
} catch (Exception e) {
// TODO Auto-generated catch block
//e.printStackTrace();
logger.error("In vendor tests for vendor Id:"+vendorId+"",e);
return new ModelAndView("error","message", e);
}
}
Pass the list back using the model
public String showUserForm(ModelMap model) {
User user = new User();
model.addAttribute(user);
//want to send this "lookingfor" to the jsp
List<String> lookingfor=service.getOptions();
model.addAttribute("lookingFor", lookingFor);
return "welcome";
}
In your jsp display the options like:
<form:select path="lookingFor">
<form:option value="-" label="--Please Select"/>
<form:options items="${lookingFor"}"/>
</form:select>
You can also return ModelAndView
e.g.
public ModelAndView showUserForm()
{
mv= new ModelAndView("welcome");
User user = new User();
mv.getModel.put("user",user);
List<String> lookingfor=service.getOptions();
//want to send this "lookingfor" to the jsp
mv.getModel().put("lookingfor",lookingfor);
return mv;
}
edit: in response to comment below look at the itemValue
This should be one of the properties of your lookingfor object see this country list example
<form:options items="${countryList}" itemValue="code" itemLabel="name"/>
where list is a list of country objects and code and name are properties of the country object
edit2
So just in jsp do
<c:forEach var="item" items="${lookingfor}">
<form:option value="${item}"/>
</c:forEach>