The dispatcherservlet configuration needs to include a handleradapter that supports this handler

To configure the DispatcherServlet properly, you need to include a HandlerAdapter that supports the handler you are using. The HandlerAdapter is responsible for executing the appropriate methods of the handler based on the incoming request.

Here is an example of configuring the DispatcherServlet in an XML-based Spring MVC application context file with the required HandlerAdapter:

    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:mvc="http://www.springframework.org/schema/mvc"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/mvc
           http://www.springframework.org/schema/mvc/spring-mvc.xsd">
           
      <mvc:annotation-driven/>
      
      <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"/>
        <property name="suffix" value=".jsp"/>
      </bean>
      
      <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>
      
      <bean class="your.package.YourController"/>
      
      <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"/>
        <property name="suffix" value=".jsp"/>
      </bean>
      
      <bean id="dispatcherServlet" class="org.springframework.web.servlet.DispatcherServlet">
        <property name="contextConfigLocation" value="/WEB-INF/application-context.xml"/>
      </bean>
      
      <servlet>
        <servlet-name>dispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
      </servlet>
      
      <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
      </servlet-mapping>
      
    </beans>
  

In this example, we are using the RequestMappingHandlerAdapter as the HandlerAdapter. This adapter is responsible for handling annotated controller methods and supports the use of annotations such as @RequestMapping, @PathVariable, @RequestParam, etc. You can customize or use a different HandlerAdapter depending on your specific requirements.

Similar post

Leave a comment