0

I have the following class that doesn't seem to be returning a List from the getter.

package springmvc.service;

import java.math.BigDecimal;
import java.util.LinkedList;
import java.util.List;

import springmvc.model.Brand;
import springmvc.model.Car;

public class CarManager {

    private static List<Car> carList;

    static {
        Brand brand1 = new Brand();
        brand1.setId((long) 1);
        brand1.setName("Mercedes");
        brand1.setCountry("Germany");

        Brand brand2 = new Brand();
        brand2.setId((long) 2);
        brand2.setName("Peugeot");
        brand2.setCountry("France");

        Car car1 = new Car();
        car1.setId((long) 1);
        car1.setBrand(brand1);
        car1.setModel("SL 500");
        car1.setPrice(new BigDecimal(40000));

        Car car2 = new Car();
        car2.setId((long) 2);
        car2.setBrand(brand2);
        car2.setModel("607");
        car2.setPrice(new BigDecimal(35000));

        carList = new LinkedList<Car>();
        carList.add(car1);
        carList.add(car2);
    }

    public List<Car> getCarList() {
        return carList;
    }
}

For the record, here is the Spring controller class where I'm calling the above class. All the statics are copied from the tutorial I got this from.

package springmvc.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import springmvc.service.CarManager;

public class CarListController implements Controller {
    public ModelAndView handleRequest(HttpServletRequest arg0,
            HttpServletResponse arg1) throws Exception {

        CarManager carManager = new CarManager();

        ModelAndView modelAndView = new ModelAndView("carList");
        modelAndView.addObject("carList", carManager.getCarList());

        return modelAndView;
    }
}

Here is what I have for spring-servlet.xml.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    <bean name="/carList.jsp" class="springmvc.web.CarListController"/>
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="suffix" value=".jsp" />
    </bean>
</beans>
7
  • Can you show the controller config? Have you set a breakpoint in the controller class to verify that it is being called?
    – beny23
    Aug 22, 2011 at 22:39
  • 2
    Why all the statics in your first class? And why the non-static method amongst the statics? Aug 22, 2011 at 22:40
  • 2
    What do you mean by "doesn't seem to be returning a List"? What does it return? null? an empty List<Car>? Aug 22, 2011 at 22:53
  • I have added my spring-servlet.xml above. The statics were there in the tutorial I copied this from.
    – user636859
    Aug 22, 2011 at 22:53
  • 4
    Time to do some debugging to see what exactly is being returned. You really don't know at this point and so it's premature to ask a question here. Aug 22, 2011 at 23:15

2 Answers 2

0

Check your servlet container log for any exceptions being thrown. It could be throwing an exception in the static initialiser before it creates carList.

0

"Doesn't seem" doesn't mean anything...

At least you could have printed the content of the list to show us:

public class CarListController implements Controller {
    public ModelAndView handleRequest(HttpServletRequest arg0,
            HttpServletResponse arg1) throws Exception {

        CarManager carManager = new CarManager();
        System.out.println(carManager.getCarList());

        ModelAndView modelAndView = new ModelAndView("carList");
        modelAndView.addObject("carList", carManager.getCarList());

        return modelAndView;
    }
}

If the list if really null, then you should perhaps check for an exception in the static initializer

But my point of view is that you consider the list being empty while you just see nothing in your view/browser... there is probably a problem in your view iterating over the list (bad JSTL use?) because if you do not have any runtime exception and/or error 500 this probably means that the list is initialized...

Or perhaps you creates and set a new empty list or null ref to that carList attribute, but the code is not provided...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.