配置了DispatcherServlet的detectAllHandlerMappings属性为true;
spring-mvc.xml也有声明:
**访问:http://localhost:8080/demo/index 一直报错:** javax.servlet.ServletException: No adapter for handler [com.wch.controller.UserController@20142abf]: The DispatcherServlet configuration needs to include a HandlerAdapter that supports this handler org.springframework.web.servlet.DispatcherServlet.getHandlerAdapter(DispatcherServlet.java:1136)
下面是Controller部分:
package com.wch.controller;
import com.wch.entity.User;
import com.wch.service.IUserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
@Controller
public class UserController {
@Resource private IUserService userService;
@RequestMapping(value = "/index", method = RequestMethod.GET) public String toIndex(HttpServletRequest request, Model model) { String name = (String)request.getParameter("name"); User user = this.userService.getUser(name); model.addAttribute("user",user); return "index"; }
}
这样配置应该是没问题的
<!-- 默认的注解映射的支持 -->
<mvc:annotation-driven />
<!-- 避免IE在ajax请求时,返回json出现下载 -->
<bean id="jacksonMessageConverter"
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
知道原因了,原来我的controller里面使用了 @RequestMapping(value = "/index", method = RequestMethod.GET)这个注解,然后我的spring-mvc配置文件也注册了一个handlerAdapter,去掉这段就好了
<!--<bean--> <!--class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">--> <!--<property name="messageConverters">--> <!--<list>--> <!--<ref bean="mappingJacksonHttpMessageConverter" /> <!– JSON转换器 –>--> <!--</list>--> <!--</property>--> <!--</bean> 具体原因说不上,还在研究