views:

329

answers:

1

Hi,

I'm new to spring controllers using annotated controllers.

Here is my configuration

Bean definition

<bean
 class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />

Controller

package learn.web.controller.annotation;

import javax.servlet.http.HttpServletRequest;

import learn.web.controller.BaseController;

import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class FirstController extends BaseController {

    @RequestMapping("/annotation/first.ftl")
    public ModelAndView first(HttpServletRequest request) {

        if(messageSource  instanceof ReloadableResourceBundleMessageSource){
            ReloadableResourceBundleMessageSource m = (ReloadableResourceBundleMessageSource) messageSource;
            m.clearCache();
        }

        messageSource.getMessage("learn.message.first", new Object[] {},
                localResolver.resolveLocale(request));

        return new ModelAndView("/annotation/first");
    }

}

When tried to access the given URL Spring is throwing a warning org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/Learn/annotation/first.ftl] in DispatcherServlet with name 'springapp'

+3  A: 

I think what you are missing is the component scan

<context:component-scan base-package="learn.web.controller" />

Add this to your configuration and try.

This will load all annotated components from the specified package

Your configuration may look like this

<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-2.5.xsd
       http://www.springframework.org/schema/context 
       http://www.springframework.org/schema/context/spring-context-2.5.xsd"&gt;

    <context:component-scan base-package="learn.web.controller" />
    <bean
 class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />

</beans>
Arun P Johny