Announcement Announcement Module
Collapse
No announcement yet.
Use of @Controller and @RequestMapping Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Use of @Controller and @RequestMapping

    Hello all

    Im trying to build my Controller using the Annotation @Controller and @RequestMapping but i can only make it work implementing the interface Controller.

    If i change the signature of my class to

    Code:
    public class InventoryController  implements Controller
    It will work flawless.

    I would appreciate any help

    Here is my Controller code:

    Code:
    @Controller
    @RequestMapping("/hello.htm")
    public class InventoryController  {
    
    	protected final Log logger = LogFactory.getLog(getClass());
    
    	@Autowired
    	private ProductManager productManager;
    
    	public ModelAndView handleRequest(HttpServletRequest request,
    			HttpServletResponse response) throws ServletException, IOException {
    
    		String now = (new Date()).toString();
    		Map<String, Object> myModel = new HashMap<String, Object>();
    		myModel.put("now", now);
    		myModel.put("products", productManager.getProducts());
    
    		return new ModelAndView("hello", "model", myModel);
    
    	}
    
    	public void setProductManager(ProductManager productManager) {
    		this.productManager = productManager;
    	}
    }
    My XML:

    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:aop="http://www.springframework.org/schema/aop"
    	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-2.5.xsd
            http://www.springframework.org/schema/aop
            http://www.springframework.org/schema/aop/spring-aop-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="springapp.domain.product" />
    	<context:component-scan base-package="springapp.web" />
    	<context:annotation-config />
    	<aop:aspectj-autoproxy />
    
    	<bean id="viewResolver"
    		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    		<property name="viewClass"
    			value="org.springframework.web.servlet.view.JstlView" />
    		<property name="prefix" value="/WEB-INF/jsp/" />
    		<property name="suffix" value=".jsp" />
    	</bean>
    
    	<bean id="productManager" class="springapp.service.SimpleProductManager">
    	</bean>
    
    	<bean id="messageSource"
    		class="org.springframework.context.support.ResourceBundleMessageSource">
    		<property name="basename" value="messages" />
    	</bean>
    </beans>
    The error:

    Code:
    javax.servlet.ServletException: No adapter for handler [springapp.web.InventoryController@1afb0c7]: Does your handler implement a supported interface like Controller?
    	org.springframework.web.servlet.DispatcherServlet.getHandlerAdapter(DispatcherServlet.java:1100)
    	org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:874)
    	org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:807)
    	org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:571)
    	org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:501)
    	javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
    	javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

  • #2
    Change your method signature and see what happens. The body doesn't use neither parameters.

    Comment


    • #3
      To put it another way, when you use annotations instead of inheritance, you have to tell it what methods are called on particular requests. It doesn't know to call "handleRequest()". Read the section in the Spring doc titled "Mapping requests with @RequestMapping" for examples of this.

      Comment


      • #4
        Originally posted by dkarr View Post
        To put it another way, when you use annotations instead of inheritance, you have to tell it what methods are called on particular requests. It doesn't know to call "handleRequest()". Read the section in the Spring doc titled "Mapping requests with @RequestMapping" for examples of this.
        Thank you for you help!

        Here is my working code

        Code:
        @Controller
        @RequestMapping("/hello.htm")
        @SessionAttributes("model")
        public class InventoryController {
        
        	protected final Log logger = LogFactory.getLog(getClass());
        
        	@Autowired
        	private ProductManager productManager;
        
        	@RequestMapping(method = RequestMethod.GET)
        	public ModelAndView handleRequest(HttpServletRequest request,
        			HttpServletResponse response) throws ServletException, IOException {
        
        		String now = (new Date()).toString();
        		Map<String, Object> myModel = new HashMap<String, Object>();
        		myModel.put("now", now);
        		myModel.put("products", productManager.getProducts());
        
        		return new ModelAndView("hello", "model", myModel);
        
        	}
        
        	public void setProductManager(ProductManager productManager) {
        		this.productManager = productManager;
        	}
        }

        Comment


        • #5
          Also note that "handleRequest()" doesn't need any of those function parameters or throws clauses anymore.

          Comment


          • #6
            Originally posted by dkarr View Post
            Also note that "handleRequest()" doesn't need any of those function parameters or throws clauses anymore.
            Sure, a better and clean code

            thank you again.

            Here my new clean code

            Code:
            @Controller
            @RequestMapping("/hello.htm")
            @SessionAttributes("model")
            public class InventoryController {
            
            	protected final Log logger = LogFactory.getLog(getClass());
            
            	@Autowired
            	private ProductManager productManager;
            
            	@RequestMapping(method = RequestMethod.GET)
            	public ModelAndView handleRequest() {
            
            		String now = (new Date()).toString();
            		Map<String, Object> myModel = new HashMap<String, Object>();
            		myModel.put("now", now);
            		myModel.put("products", productManager.getProducts());
            
            		return new ModelAndView("hello", "model", myModel);
            
            	}
            
            	public void setProductManager(ProductManager productManager) {
            		this.productManager = productManager;
            	}
            
            }

            Comment

            Working...
            X