robh,
sorry to take so long to respond, I have been away.
my configuration is as follows
Tomcat 4.1 on Win2k
my problem is that the List "products" never gets instantiated or populated and so I get a null pointer exception when trying to access it in the handleRequest method of the controller.
When I put code in to instantiate and populate products, the problem goes away, but I get the impression this is supposed to be done automatically somehow and this is what I do not understand.
The Controller:
Code:
public class SpringTestController implements Controller {
protected final Log logger = LogFactory.getLog(getClass());
private ProductManager prodMan= new ProductManager();
public ProductManager getProdMan() {
return prodMan;
}
public void setProdMan(ProductManager prodMan) {
this.prodMan = prodMan;
}
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String now = (new java.util.Date()).toString();
Map myModel = new HashMap();
myModel.put("now",now);
// the nullPointer exception happens on this line:
myModel.put("products",this.getProdMan().getProducts());
return new ModelAndView("hello","model",myModel);
}
}
The Product:
Code:
public class Product implements Serializable {
private String description;
private Double price;
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public void setDescription(String s){
description = s;
}
public String getDescription(){
return description;
}
}
Here is productManager:
Code:
public class ProductManager implements Serializable{
private ArrayList products;
public ArrayList getProducts() {
return products;
}
public void setProducts(ArrayList products) {
this.products = products;
}
}
here is the SpringTest-servlet.xml
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<!--
- Application context definition for "springapp" DispatcherServlet.
-->
<beans>
<bean id="SpringTestController" class="SpringTestController"/>
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/hello.htm">SpringTestController</prop>
</props>
</property>
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass">
<value>org.springframework.web.servlet.view.JstlView</value>
</property>
<property name="prefix"><value>/WEB-INF/jsp/</value></property>
<property name="suffix"><value>.jsp</value></property>
</bean>
<bean id="prodMan" class="ProductManager">
<property name="products">
<list>
<ref bean="product1"/>
<ref bean="product2"/>
<ref bean="product3"/>
</list>
</property>
</bean>
<bean id="product1" class="Product">
<property name="description">
<value>Lamp</value>
</property>
</bean>
<bean id="product2" class="Product">
<property name="description">
<value>Battery</value>
</property>
</bean>
<bean id="product3" class="Product">
<property name="description">
<value>Valve</value>
</property>
</bean>
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename">
<value>messages</value>
</property>
</bean>
</beans>
and, just in case, web.xml:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC '-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN' 'http://java.sun.com/dtd/web-app_2_3.dtd'>
<web-app>
<servlet>
<servlet-name>SpringTest</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringTest</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>
index.jsp
</welcome-file>
</welcome-file-list>
</web-app>