我用scala编写的简单spring mvc控制器有问题:
@Controller
class HelloWorldController {
implicit def sessionFactory2Session(sf: SessionFactory) = sf.getCurrentSes
@Autowired
var sessionFactory: SessionFactory = null
@Transactional
@RequestMapping(value=Array("/hello.html"),method = Array(RequestMethod.GET,RequestMethod.POST))
def showHello = {
val document = new CustomDocument("name","custom")
sessionFactory.save(document)
sessionFactory.flush
"helloPage"
}
}
当我尝试访问/hello.html时,我收到了异常:
javax.servlet.ServletException: No adapter for handler: Does your handler implement a supported interface like Controller?
at org.springframework.web.servlet.DispatcherServlet.getHandlerAdapter(DispatcherServlet.java:951)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:758)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:717)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
但是当我删除@Transactional注释时 - 一切正常!! Spring无法找到带有两个注释的请求映射?我的applicationContext.xml片段:
<bean id="openSessionInViewInterceptor"
class="org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="interceptors">
<list><ref bean="openSessionInViewInterceptor"/></list>
</property>
<property name="alwaysUseFullPath" value="true" />
</bean>
<context:component-scan base-package="scala.hibernate"/>
<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- for transaction -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManaager">
<property name="sessionFactory" ref="sessionFactory"/>
<property name="dataSource" ref="dataSource"/>
</bean>
<tx:annotation-driven/>
修改
使用动态代理应用事务方面,它阻止Spring MVC访问目标类上的@RequestMapping注释 解决方案:
<tx:annotation-driven proxy-target-class="true"/>
答案 0 :(得分:0)
尝试将<context:annotation-config />
添加到applicationContext.xml
,看看是否能解决问题。
答案 1 :(得分:-1)
声明以下bean。这应该解决问题
<bean id="simpleHandler" class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
一旦找到根本原因,会更新你的信息。