Announcement Announcement Module
Collapse
No announcement yet.
Passing a list as a parameter to ModelAndView Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Passing a list as a parameter to ModelAndView

    I was wondering if anyone knows how to pass a list of model objects as a parameter to the ModelandView method in the controller. For example, if i want to get a list of all employees working in a company, then passing the company Id in the URL i should search for employees working in that company, put it in a list of employee object and then the return statement of the method being something like

    return new ModelAndView(XML_VIEW_NAME, "employees", emps); where emps is a List<Employee> and Employee is a POJO.

    It gives an error

    java.lang.OutOfMemoryError: Java heap space


    pointing to emps.add(emp) where emp is the Employee object at the controller code.

    I am deploying the app on Google App Engine. The actual code I am working on is getting a list of 'variables' in an 'application' in the form of an XML in stead of a list of employees in a company.

    Thanks

  • #2
    Can you post your code?

    About this
    It gives an error
    java.lang.OutOfMemoryError: Java heap space
    How are you filling the collection? from the DB?
    how many POJO would contain your collection?
    If is really high it could be a reason of the exception

    Comment


    • #3
      CONTROLLER
      ----------

      import java.io.IOException;
      import java.io.StringReader;
      import java.util.ArrayList;
      import java.util.List;

      import javax.xml.transform.Source;
      import javax.xml.transform.stream.StreamSource;

      import org.springframework.oxm.XmlMappingException;
      import org.springframework.oxm.castor.CastorMarshaller;
      //import org.springframework.oxm.jaxb.Jaxb2Marshaller;
      import org.springframework.stereotype.Controller;
      import org.springframework.web.bind.annotation.PathVariab le;
      import org.springframework.web.bind.annotation.RequestBod y;
      import org.springframework.web.bind.annotation.RequestMap ping;
      import org.springframework.web.bind.annotation.RequestMet hod;
      import org.springframework.web.servlet.ModelAndView;

      import com.google.appengine.api.datastore.DatastoreServic e;
      import com.google.appengine.api.datastore.DatastoreServic eFactory;
      import com.google.appengine.api.datastore.Entity;
      import com.google.appengine.api.datastore.EntityNotFoundE xception;
      import com.google.appengine.api.datastore.FetchOptions;
      import com.google.appengine.api.datastore.Key;
      import com.google.appengine.api.datastore.KeyFactory;
      import com.google.appengine.api.datastore.Query;
      import com.google.appengine.api.datastore.Query.FilterOpe rator;
      import com.example.bean.Application;
      import com.example.bean.Variable;


      import java.util.logging.Logger;

      @Controller
      public class DataController {

      private CastorMarshaller castorMarshaller;

      public void setCastorMarshaller(CastorMarshaller castorMashaller) {
      this.castorMarshaller = castorMashaller;
      }

      private static final String XML_VIEW_NAME = "rest";


      @RequestMapping(method=RequestMethod.GET, value="/application/{id}")
      public ModelAndView getVariables(@PathVariable String id) throws Exception {
      Query findAttsQuery = new Query("Variable");
      id = id.substring(0, id.lastIndexOf('.'));

      findAttsQuery.addFilter("Application", FilterOperator.EQUAL, id);

      DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
      List<Entity> results = datastore.prepare(findAttsQuery).asList(FetchOptio ns.Builder.withDefaults());
      int i =0;

      ArrayList<Variable> vars = new ArrayList<Variable>(results.size());
      ModelAndView mav = new ModelAndView();

      while(i<results.size()) {
      vars.add(new Variable(results.get(i).getKey().getName(),results .get(i).getProperty("Type").toString(),results.get (i).getProperty("Value").toString()));
      mav.addObject("variable", vars);

      }

      mav.setViewName(XML_VIEW_NAME);
      return mav;
      }


      Here goes the code. I have modified it a little using a modelandview object defined. This code retrieves data from hte google app engine datastore.

      The issue I have realized is in the vars.add() statement . When I just create 2 different Variable objects (as I know there are only 2 objects in teh database for a particular search query of GET.) and add those 2 objects to the array list vars , it works perfectly fine.

      I am pretty new to Spring and Java and am wondering in second thoughts if this is a Java error am doing here.

      Here is the bean:Variable
      ---------------------

      public class Variable {

      private String id;
      private String value;

      private String type;

      public Variable() {}

      public Variable(String id, String value, String type) {
      this.id = id;
      this.value = value;
      this.type = type;

      }
      public String getId() {
      return id;
      }

      public void setId(String id) {
      this.id = id;
      }
      public String getValue() {
      return value;
      }
      public void setValue(String value) {
      this.value = value;
      }

      public String getType() {
      return type;
      }

      public void setType(String type) {
      this.type = type;
      }


      }
      mapping .xml for marshaller

      <class name="com.example.bean.Variable" identity="id">
      <map-to xml="variable" />
      <field name="id" type="string">
      <bind-xml name="id" node="element" />
      </field>
      <field name="value" type="string">
      <bind-xml name="value" node="element" />
      </field>
      <field name="type" type="string">
      <bind-xml name="type" node="element" />
      </field>
      </class>


      Thank you!

      Comment


      • #4
        mav.addObject("variable", vars); should be outside the loop. I moved it out but still the error.

        Comment


        • #5
          Use code tags, is more readable for us

          About this

          Code:
          findAttsQuery.addFilter("Application", FilterOperator.EQUAL, id);
          
          DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
          List<Entity> results = datastore.prepare(findAttsQuery).asList(FetchOptio ns.Builder.withDefaults());
          int i =0;
          
          ArrayList<Variable> vars = new ArrayList<Variable>(results.size());
          ModelAndView mav = new ModelAndView();
          
          while(i<results.size()) {
          vars.add(new Variable(results.get(i).getKey().getName(),results .get(i).getProperty("Type").toString(),results.get (i).getProperty("Value").toString()));
          mav.addObject("variable", vars);
          How was filled the bold part? and how many elements are in your collection?

          Code:
          mav.addObject("variable", vars); 
          should be outside the loop. I moved it out but still the error.
          OK

          Comment


          • #6
            The bold statement populates the List using Google App Engine's low level API.
            Right now in my datastore there are 2 elements (or entities, in GAE objects are stored as entities having key value pairs). But it will not be limited to 2 elements and hence should iterate over the list.

            Thank you.

            Comment


            • #7
              Hi

              It works now! I am sorry for all the noise!


              Code:
              @RequestMapping(method=RequestMethod.GET, value="/application/{id}")
              	public ModelAndView getVariables(@PathVariable String id) throws Exception {
              		Query findAttsQuery = new Query("Variable");
              		id = id.substring(0, id.lastIndexOf('.'));
              		
              		findAttsQuery.addFilter("Application", FilterOperator.EQUAL, id);
              		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
              		List<Entity> results = datastore.prepare(findAttsQuery).asList(FetchOptions.Builder.withDefaults());
              
              		
              		ArrayList<Variable> vars = new ArrayList<Variable>(results.size());
              		ModelAndView mav = new ModelAndView();
              		Iterator<Entity> it = results.iterator ();
              		while (it.hasNext ()) {
              		    Entity ent =  it.next ();
              		
              		Variable var = new Variable();
              		
              		var.setId(ent.getKey().getName());
              		if (ent.getProperty("Type") == null)
              			var.setType("NULL");
              		else
              		var.setType(ent.getProperty("Type").toString());
              		
              		if (ent.getProperty("Value") == null)
              			var.setValue("NULL");
              		else
              		var.setValue(ent.getProperty("Value").toString()); 
              		vars.add(var);
              		}
              		
              		mav.addObject("variable", vars);	
              	mav.setViewName(XML_VIEW_NAME);
              		return mav;
              	}

              Comment


              • #8
                It works now! I am sorry for all the noise!
                Don't worry

                it seems the key line was

                Code:
                while(i<results.size()) {
                vars.add(new Variable(results.get(i).getKey().getName(),results .get(i).getProperty("Type").toString(),results.get (i).getProperty("Value").toString()));
                mav.addObject("variable", vars);
                }
                which was re edited to
                Code:
                while (it.hasNext ()) {
                		    Entity ent =  it.next ();
                		
                		Variable var = new Variable();
                		
                		var.setId(ent.getKey().getName());
                		if (ent.getProperty("Type") == null)
                			var.setType("NULL");
                		else
                		var.setType(ent.getProperty("Type").toString());
                		
                		if (ent.getProperty("Value") == null)
                			var.setValue("NULL");
                		else
                		var.setValue(ent.getProperty("Value").toString()); 
                		vars.add(var);
                		}

                Comment

                Working...
                X