getter不返回Java List

时间:2011-08-22 22:35:29

标签: java list

我有以下类似乎没有从getter返回List。

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

对于记录,这里是Spring控制器类,我在这里调用上面的类。所有静态都是从我从中得到的教程中复制的。

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

以下是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>

2 个答案:

答案 0 :(得分:0)

检查servlet容器日志中是否有任何异常抛出。它可能会在创建carList之前在静态初始化器中抛出异常。

答案 1 :(得分:0)

“看似不”并不代表什么......

至少你可以打印清单的内容给我们看:

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

如果列表确实为null,那么您应该检查静态初始值设定项中的异常

但我的观点是你认为列表是空的,而你在视图/浏览器中什么也看不见...你的视图中可能存在一个问题,即迭代列表(使用错误的JSTL?),因为如果你没有任何运行时异常和/或错误500这可能意味着列表已初始化...

或许你创建并设置一个新的空列表或null引用该carList属性,但是没有提供代码......