How to pass data from controller to front end in spring

Go To StackoverFlow.com

0

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?

2012-04-04 16:54
by Pedantic
I solved my problem ,thks al - Pedantic 2012-04-04 17:45


1

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.

2012-04-04 17:03
by Carlos Gavidia
Hi thanks for the suggestion, But when I am adding this line.I am getting following error Apr 5, 2012 1:07:05 AM org.apache.catalina.core.ApplicationDispatcher invoke 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 - Pedantic 2012-04-04 17:07


0

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>
2012-04-04 17:07
by Shaun Hare
Thanks But i am getting Exception 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 - Pedantic 2012-04-04 17:12
What does lookinfor look like - Shaun Hare 2012-04-04 17:18
Hi, this is a List of some values, that needs to populate on jsp. I want this list to travel from controller to jsp - Pedantic 2012-04-04 17:24


0

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);
        }

    }
2012-04-04 17:10
by Ramesh Kotha


0

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>
2012-04-04 17:11
by Kevin Bowersox
Ads