Back Forum Reply New

problem with controller annotation : No adapter for handler

Hi,

I try to use the  @Controller and @RequestMapping annotation.
I have my controller class like this :Code:
@Controller
@RequestMapping(quot;/home.doquot;)
public class HomeController
{
...
}
Here my servlet config file.

Code:
lt;?xml version=quot;1.0quot; encoding=quot;UTF-8quot;?gt;
lt;beans xmlns=quot;schema/beansquot;      xmlns:xsi=quot;2001/XMLSchema-instancequot;      xmlns:context=quot;schema/contextquot;      xmlns:p=quot;schema/pquot;      xmlns:aop=quot;schema/aopquot;      xmlns:tx=quot;schema/txquot;      xsi:schemaLocation=quot;      schema/beans schema/beans/spring-beans-2.5.xsd      schema/context  schema/context/spring-context-2.5.xsd      schema/tx schema/tx/spring-tx-2.5.xsd      schema/aop schema/aop/spring-aop-2.1.xsdquot;gt;      lt;!--
- The controllers are autodetected POJOs labeled with the @Controller annotation.
--gt;
lt;context:component-scan base-package=quot;web.controllerquot;/gt;      lt;!--
- This bean processes annotated handler methods, applying PetClinic-specific PropertyEditors
- for request parameter binding. It overrides the default AnnotationMethodHandlerAdapter.
--gt;
lt;bean class=quot;org..web.servlet.mvc.annotation.AnnotationMethodHandlerAdapterquot; /gt;

lt;!--
- This bean resolves specific types of exceptions to corresponding logical
- view names for error views. The default behaviour of DispatcherServlet
- is to propagate all exceptions to the servlet container: this will happen
- here with all other types of exceptions.
--gt;
lt;bean class=quot;org..web.servlet.handler.SimpleMappingExceptionResolverquot;gt;
lt;property name=quot;exceptionMappingsquot;gt;
lt;propsgt;
lt;prop key=quot;org..dao.DataAccessExceptionquot;gt;dataAccessFailurelt;/propgt;
lt;prop key=quot;org..transaction.TransactionExceptionquot;gt;dataAccessFailurelt;/propgt;
lt;/propsgt;
lt;/propertygt;
lt;/beangt;

lt;!--
- This bean configures the 'prefix' and 'suffix' properties of
- InternalResourceViewResolver, which resolves logical view names
- returned by Controllers. For example, a logical view name of quot;vetsquot;
- will be mapped to quot;/WEB-INF/jsp/vets.jspquot;.
--gt;
lt;bean class=quot;org..web.servlet.view.InternalResourceViewResolverquot; p:prefix=quot;/view/quot; p:suffix=quot;.jspquot;/gt;

lt;!--
- Message source for this context, loaded from localized quot;messages_xxquot; files.
- Could also reside in the root application context, as it is generic,
- but is currently just used within UnikityPortal's web tier.
--gt;
lt;bean id=quot;messageSourcequot; class=quot;org..context.support.ResourceBundleMessageSourcequot;
p:basename=quot;messagesquot;/gt;
   lt;!-- this bean with the well known name generates view names for us --gt;   lt;bean id=quot;viewNameTranslatorquot; class=quot;org..web.servlet.view.DefaultRequestToViewNameTranslatorquot;/gt;   

lt;/beansgt;
When I try to go to : home.do I have this error :

javax.servlet.ServletException: No adapter for handler [web.controller.HomeController@17834de]: Does your handler implement a supported interface like Controller?

Thank you for your help.

Do you have one of the methods in your controller annotated with @RequestMapping(method=RequestMethod.GET)?

Not (yet).

Dude! I have the same f... error

Hi Drim,

Can I make one suggestion, you don't need to define a AnnotationMethodHandlerAdapter bean as this is given to you by default by the DispatcherServlet.

Have a look at the DispatcherServlet api docs, it tells you more than ref docs ever will.

Josh

I fixed my problem using the DefaultAnnotationHandlerMappingCode:   lt;context:component-scan base-package=quot;mx.itesm.ccm.dia.tps.webquot; /gt;      lt;bean class=quot;org..web.servlet.mvc.annotation.DefaultAnnotationHandlerMappingquot; prder=quot;0quot; /gt;
   lt;!-- lt;bean class=quot;org..web.servlet.mvc.annotation.AnnotationMethodHandlerAdapterquot;gt;       lt;property name=quot;webBindingInitializerquot;gt;lt;bean class=quot;mx.itesm.ccm.dia.tps.binding.TGBindingInitializerquot; /gt;       lt;/propertygt;   lt;/beangt; --gt;      lt;bean id=quot;web.siteMappingquot; class=quot;org..web.servlet.handler.SimpleuclHandlerMappingquot;gt;       lt;property name=quot;mappingsquot;gt;lt;propsgt;               lt;prop key=quot;/generarExamen.htmquot;gt;testGeneratorControllerlt;/propgt;lt;/propsgt;       lt;/propertygt;   lt;/beangt;      lt;bean id=quot;testGeneratorControllerquot; class=quot;mx.itesm.ccm.dia.tps.web.TestGeneratorControllerquot;gt;       lt;property name=quot;pagesquot;gt;lt;listgt;    lt;valuegt;tg_temalt;/valuegt;    lt;valuegt;tg_subtemalt;/valuegt;lt;/listgt;       lt;/propertygt;   lt;/beangt;
The only problem is that i have to create an initBinder method in all my annotated controllersCode:   @InitBinder   public void initBinder(WebDataBinder binder) {       binder.registerCustomEditor(Materia.class, new MateriaEditor(tGService));       binder.registerCustomEditor(Profesor.class, new ProfesorEditor(tGService));   }
And in the old fashion controllers override it.Code:   @Override   protected void initBinder(fromServletRequest request, ServletRequestDataBinder binder) throws Exception {       binder.registerCustomEditor(Tema.class, new TemaEditor(tGService));       binder.registerCustomEditor(SubTema.class, new SubTemaEditor(tGService));   }Hi lalbertme,

Your config is a bit strange as it seems you are defining beans which are given to you for free by the DispatcherServlet.

The DefaultAnnotationHandlerMapping bean is one of the beans given to you for free so there is no need to define it.

Also, why have you commented out the following code:

Code:
lt;!-- lt;bean class=quot;org..web.servlet.mvc.annotation.AnnotationMethodHandlerAdapterquot;gt;       lt;property name=quot;webBindingInitializerquot;gt;lt;bean class=quot;mx.itesm.ccm.dia.tps.binding.TGBindingInitializerquot; /gt;       lt;/propertygt;   lt;/beangt; --gt;
If you define a webBindingInitializer then you wont have to have an initBinder in each of your annotated controllers.

But remember, and this is all in the ref docs, once you define a particular type of bean (eg. mapper or handler) then you override all the ones given to you by default (ones that deal with simple controllers).

Have a good read of the api docs and ref docs for further info.

Hopes this helps

Josh

Yes you are right, I read the DispatcherServlet API.

Default is BeanNameuclHandlerMapping, as well as a DefaultAnnotationHandlerMapping  when running on Java 5+

But when I defined AnnotationMethodHandlerAdapter, in order to put the webBindingInitializer my old fashion controllers didn't work, just the annotated ones works, I got errors likeCode:
javax.servlet.ServletException: No adapter for handler [mx.itesm.ccm.dia.tps.web.TestGeneratorController@17834de]: Does your handler implement a supported interface like Controller?
And TestGeneratorController extends of AbstractWizardFormController.

I'm using Tomcat 6.0.16 and Spring Framework 2.5.3, I guess I will read more, but for now that configuration just work!

Hi lalbertme,

I know the saying goes 'If it ain't broke, don't fix it', but sometimes it might not be broke but just very verbose.

If you define a AnnotationMethodHandlerAdapter then you are overriding all the ones given to you by default, including the SimpleControllerHandlerAdapter.

As long as you define HandlerAdapters which cater for all controllers you use you should be away laughing.

Josh

add the following code into your dispatch-servlet.xml

lt;bean class=quot;org..web.servlet.mvc.SimpleC  ontrollerHandlerAdapterquot;/gt;

Because you explicitly register one handlerAdapter, it overwrites all the default handlerAdapters, including the above one. The SimpleControllerHandlerAdapter handles all triditioinal controller beans, which implement quot;Controllerquot; interface.

I can understand why you register the pre-defined quot;AnnotationMethodHandlerAdapterquot;, as you need to define a quot;globalquot; customer propertiesEditor for all annotated controllers.
¥
Back Forum Reply New