The DispatcherServlet configuration needs to inclu

2020-08-10 08:32发布

问题:

I wanted to use both annotation mapping and xml mapping in Spring MVC. My application-context.xml as follows:

<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="personal/account/history">accountHistoryController</prop>
            </props>
        </property>
    </bean>

    <bean id="accountHistoryController" class="com.fg.banking.ib.controller.AccountHistoryController" />

    <bean
        class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean>
    <context:annotation-config />
    <mvc:annotation-driven />
    <context:component-scan base-package="com.fg.banking.ib.controller, com.fg.banking.ib.helper, com.fg.banking.corporate.controller" />

I am getting the following error when I try to access the url. I have configured the SimpleControllerHandlerAdapter as above.

javax.servlet.ServletException: No adapter for handler 
[com.fg.banking.ib.controller.AccountHistoryController@218531e6]: The DispatcherServlet configuration needs to include a HandlerAdapter that supports this handler
org.springframework.web.servlet.DispatcherServlet.getHandlerAdapter(DispatcherServlet.java:1128)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:903)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:827)

What to do?

回答1:

I resolved the issue. I forgot to add the @Controller annotation in controller class. There are fore we can use the both methods(annotation mapping & XML mapping) together in an application.



回答2:

Make sure you have implemented Controller in your controller classes and overrided handleRequest method.



回答3:

Try adding the following as a handler mapper(Worked for me):

<bean id="HandlerMapping" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>


回答4:

Here our controller class should extends

import org.springframework.web.servlet.mvc.AbstractController;

public class AppController extends AbstractController{ }

Here we need to implement the abstract method as :

protected modelandview handleRequestInternal(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception { return null; }