spring: web学习笔记1--异常处理:No adapter for handler

在配置spring web框架的时候,遇到No adapter for handler的异常

前置过程:

自己想实现两种方式的bean的解析,一种是通过手动配置的

即要通过写 <bean id="xxxx" class="xx.xxx.ccc"/>

第二种是通过注解方式实现的

即在public class xxx上面加个注释@Controller,然后又不想在这个地方写上@RequestMapping("/xxx.htm"),即不想把这个请求的url写死,想根据controller的name来自动解析

然后就基本卡壳在这个解析上面了,两种方式都不能很好的兼容。

 

出现错误的时候的xml是

 

 

 

xxx-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- - DispatcherServlet application context for PetClinic's web tier. -->
<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"
xmlns:oxm
="http://www.springframework.org/schema/oxm"
xsi:schemaLocation
="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.0.xsd"
>

<context:component-scan base-package="com.molly"></context:component-scan>

<bean id="homePageController" class="com.molly.controller.HomePageController">
<property name="rantService" ref="rantService"></property>
</bean>
<bean id="rantService" class="com.molly.service.RantService"></bean>
<bean id="sampleController" class="com.molly.controller.SampleController"></bean>

<bean id="rantsForVehicle" class="com.molly.controller.RantsForVehicleController">
<property name="rantService" ref="rantService"></property>
</bean>


<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
<bean
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
<!-- this bean with the well known name generates view names for us -->
<bean id="viewNameTranslator"
class
="org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator" />
<!-- maps request URLs to Controller names -->
<bean
class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" />

  <!-- - 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.
-->
<bean
class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings">
<props>
<prop key="org.springframework.web.servlet.PageNotFound">pageNotFound</prop>
<prop key="org.springframework.dao.DataAccessException">dataAccessFailure</prop>
<prop key="org.springframework.transaction.TransactionException">dataAccessFailure</prop>
</props>
</property>
</bean>


<bean id="velocityConfigurer"
class
="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
<property name="configLocation">
<value>/WEB-INF/velocity/velocity.properties</value>
</property>
<property name="resourceLoaderPath">
<value>/WEB-INF/templates/</value>
</property>
<property name="velocityProperties">
<props>
<prop key="directive.foreach.counter.name">
loopCoounter
</prop>
<prop key="directive.foreach.counter.initial.value">
0
</prop>
<prop key="input.encoding">UTF-8</prop>
<prop key="output.encoding">UTF-8</prop>
<prop key="contentType">text/html;charset=UTF-8</prop>
</props>
</property>
</bean>


<bean id="viewResolver"
class
="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
<property name="suffix">
<value>.vm</value>
</property>
<property name="exposeRequestAttributes">
<value>true</value>
</property>
<property name="exposeSessionAttributes">
<value>true</value>
</property>
<property name="contentType">
<value>text/html;charset=UTF-8</value>
</property>
<!-- <property name="toolboxConfigLocation"> <value>/WEB-INF/velocity/toolbox.xml</value>
</property>
-->
</bean>

</beans>

出错的地方就是,通过指定bean id的那几个,都不能被访问到,会抛错误出来(这几个是没有@Controller这个注解的,直接实现的controller)

但是写了@controller和@RequestMapping的,都可以正常访问

 

然后加了两个adapter,就可以了

 

<bean
class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />
<bean class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter" />

加上这两个bean的处理,就可以用了

 

参考自google groups:http://groups.google.com/group/gwt-sl/browse_thread/thread/f563b200aa0af307

 

一点一点搞spring,原理暂时也讲不出个123来,只是暂时解决遇到的问题

posted @ 2010-08-31 16:08  foward.molly.宝儿  阅读(10546)  评论(0编辑  收藏  举报