Announcement Announcement Module
Collapse
No announcement yet.
Following old Spring Tutorial... Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Following old Spring Tutorial...

    I am working on a tutorial that I suppose is based on Spring 2.0 I found on the internet. It has been very good so far. However, it instructed me to use the SimpleFormController class, which is obviously deprecated. I found an article by Willie Wheeler explaining how to migrate 2.0 to 2.5 using the annotations... and tried making some of those changes to my files, but am having problems I do not understand.

    BTW I am taking the Spring Core training in two weeks and am doing all of this just to get my feet wet with Spring.

    My controller class, with the annotations, looks like this:
    Code:
    package org.aero.tcs.add.spring2;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServletRequest;
    
    import org.aero.tcs.add.spring2.service.PriceIncrease;
    import org.aero.tcs.add.spring2.service.ProductManager;
    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.servlet.ModelAndView;
    import org.springframework.web.servlet.view.RedirectView;
    
    @Controller
    @RequestMapping("/hello.htm")
    public class PriceIncreaseFormController {
    	/** Logger for this class and subclasses */
    	protected final Log logger = LogFactory.getLog(getClass());
    	private ProductManager productManager;
    	
    	public ModelAndView onSubmit(Object command) throws ServletException {
    		int increase = ((PriceIncrease)command).getPercentage();
    		logger.info("Increasing prices by " + increase + "%.");
    		
    		productManager.increasePrice(increase);
    		
    		logger.info("returning from PriceIncreaseForm view to " + getSuccessView());
    		
    		return new ModelAndView(new RedirectView(getSuccessView()));
    	}
    	
    	protected Object formBackingObject(HttpServletRequest request) throws ServletException {
    		PriceIncrease priceIncrease = new PriceIncrease();
    		priceIncrease.setPercentage(20);
    		return priceIncrease;
    	}
    	
    	public void setProductManager(ProductManager productManager) {
    		this.productManager = productManager;
    	}
    	
    	public ProductManager getProductManager() {
    		return productManager;
    	}
    }
    Using STS, I am seeing now errors indicating getSuccessView() is now undefined for the controller class.

    What other changes do I have to make to the class and to the bean definition file to make this work?

    If I have an old Spring app in production now that uses the SimpleFormController, will it have to be rewritten to continue to work or will Spring maintain backward compatibility in future releases? I've read that deprecated classes are usually removed after a release or two with Spring...

    Thanks!

    Ginni

  • #2
    This is my bean def file...

    I added in the schema refs for context, and the context declaration. Now, I have errors reported in the bean def for PriceIncreaseFormController saying that there's no "setter found for property" for sessionForm, etc. I assume those were previously handled by the SimpleFormController super class...

    I am just starting to understand Java, so please excuse my naivete here.

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-2.5.xsd">
    
    	<context:component-scan base-package="org.aero.tcs.add.spring2"/>
    	
    	<bean id="productManager" class="org.aero.tcs.add.spring2.service.SimpleProductManager">
    		<property name="products">
    			<list>
    				<ref bean="product1"/>
    				<ref bean="product2"/>
    				<ref bean="product3"/>
    			</list>
    		</property>
    	</bean>
    	
    	<bean id="product1" class="org.aero.tcs.add.spring2.domain.Product">
    		<property name="description" value="Lamp"/>
    		<property name="price" value="5.75"/>
    	</bean>
    	
    	<bean id="product2" class="org.aero.tcs.add.spring2.domain.Product">
    		<property name="description" value="Table"/>
    		<property name="price" value="25.50"/>
    	</bean>
    	
    	<bean id="product3" class="org.aero.tcs.add.spring2.domain.Product">
    		<property name="description" value="Chair"/>
    		<property name="price" value="18.50"/>
    	</bean>
    	
    	<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
    		<property name="basename" value="messages"/>
    	</bean>
    	
    	<bean name="/hello.htm" class="org.aero.tcs.add.spring2.InventoryController">
    		<property name="productManager" ref="productManager"/>
    	</bean>
    	
    	
        <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
    
        <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
    	
    	
    	<bean name="/priceincrease.htm" class="org.aero.tcs.add.spring2.PriceIncreaseFormController">
    		<property name="sessionForm" value="true"/>
    		<property name="commandName" value="priceIncrease"/>
    		<property name="commandClass" value="org.aero.tcs.add.spring2.service.PriceIncrease"/>
    		<property name="validator">
    			<bean class="org.aero.tcs.add.spring2.service.PriceIncreaseValidator"/>
    		</property>
    		<property name="formView" value="priceincrease"/>
    		<property name="successView" value="hello.htm"/>
    		<property name="productManager" ref="productManager"/>
    	</bean>
    	
    	<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    		<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
    		<property name="prefix" value="/WEB-INF/jsp/"></property>
    		<property name="suffix" value=".jsp"></property>
    	</bean>
    </beans>

    Comment


    • #3
      Hi!

      When not subclassing any of the Spring controllers you can use the @SessionAttributes annotation to tell Spring which objects to store in the session, see

      http://static.springsource.org/sprin...-sessionattrib


      -Kaj

      Comment


      • #4
        Still confused...

        I am still lost on these annotations.

        My controller class:
        Code:
        package org.aero.tcs.add.spring2;
        
        import org.aero.tcs.add.spring2.service.ProductManager;
        import java.util.Date;
        import java.util.HashMap;
        import java.util.Map;
        
        import org.apache.commons.logging.Log;
        import org.apache.commons.logging.LogFactory;
        import org.springframework.stereotype.Controller;
        import org.springframework.web.bind.annotation.RequestMapping;
        
        @Controller
        public class InventoryController {
        	
        	protected final Log logger = LogFactory.getLog(getClass());
        	private ProductManager productManager;
        	
        	@RequestMapping("/jsp/hello.jsp")
        	public void handleHello() {
        		String now = (new Date()).toString();
        		logger.info("returning hello view " + now);
        		Map<String, Object> myModel = new HashMap<String, Object>();
        		myModel.put("now", now);
        		myModel.put("products", this.productManager.getProducts());
        	}
        	
        	public void setProductManager(ProductManager productManager) {
        		this.productManager = productManager;
        	}
        	
        }
        My dispatcher:
        Code:
        <?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:context="http://www.springframework.org/schema/context"
        xmlns:p="http://www.springframework.org/schema/p"
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-2.5.xsd">
        
        	<context:component-scan base-package="org.aero.tcs.add.spring2.service"/>
        
        
        	<bean id="productManager" class="org.aero.tcs.add.spring2.service.SimpleProductManager">
        		<property name="products">
        			<list>
        				<ref bean="product1"/>
        				<ref bean="product2"/>
        				<ref bean="product3"/>
        			</list>
        		</property>
        	</bean>
        	
        	<bean id="product1" class="org.aero.tcs.add.spring2.domain.Product">
        		<property name="description" value="Lamp"/>
        		<property name="price" value="5.75"/>
        	</bean>
        	
        	<bean id="product2" class="org.aero.tcs.add.spring2.domain.Product">
        		<property name="description" value="Table"/>
        		<property name="price" value="25.50"/>
        	</bean>
        	
        	<bean id="product3" class="org.aero.tcs.add.spring2.domain.Product">
        		<property name="description" value="Chair"/>
        		<property name="price" value="18.50"/>
        	</bean>
        	
        	<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
        		<property name="basename" value="messages"/>
        	</bean>
        	
        	<bean name="/hello.htm" class="org.aero.tcs.add.spring2.InventoryController">
        		<property name="productManager" ref="productManager"/>
        	</bean>
         
        		
            <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
        
            <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
        	
        	<bean id="configurationLoader" class="org.springmodules.validation.bean.conf.loader.annotation.AnnotationBeanValidationConfigurationLoader"/>
        	<bean id="validator" class="org.springmodules.validation.bean.BeanValidator" 
        	p:configurationLoader-ref="configurationLoader"/>
        	
        	<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        		<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
        		<property name="prefix" value="/WEB-INF/jsp/"></property>
        		<property name="suffix" value=".jsp"></property>
        	</bean>
        	
        </beans>
        I try to run index.jsp, which has a redirect:
        Code:
        <c:redirect url="/hello.htm" />
        My WEB-INF/jsp/hello.jsp should come up, no? When I test it, all I get is "resource () is unavailable". I assume this means I have some mapping wrong.

        Any ideas?

        Thanks,
        Ginni

        Comment

        Working...
        X