1.SimpleControllerHandlerAdapter(默认)
使用该处理器适配器需要自定义Controller实现Controller接口,否则会找不到对应的controller
默认的Controller适配器
<!-- 配置处理器适配器执行Controlelr ,springmvc默认的SimpleControllerHandlerAdapter:执行Controller-->
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean>
SimpleControllerHandlerAdapter源码
public class SimpleControllerHandlerAdapter implements HandlerAdapter {
public boolean supports(Object handler) {
return (handler instanceof Controller);
}
public ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
return ((Controller) handler).handleRequest(request, response);
}
public long getLastModified(HttpServletRequest request, Object handler) {
if (handler instanceof LastModified) {
return ((LastModified) handler).getLastModified(request);
}
return -1L;
}
}
通过查看源码可知,需要先判断自定义的controller是否实现的Controller接口才会执行后面的方法
2.HttpRequestHandlerAdapter
HttpRequestHandlerAdapter处理器适配器将http请求封装成HttpServletResquest 和HttpServletResponse对象,和servlet接口类似
1.配置xml文件
<!-- 配置HttpRequestHandlerAdapter适配器 用来处理实现了HttpRequestHandler后端控制类Controller-->
<bean class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter"></bean>
2.自定义Controller
public class HtppController implements HttpRequestHandler{
public void handleRequest(HttpServletRequest request, HttpServletResponse respone) throws ServletException, IOException {
request.setAttribute("hello", "sprinmvc,又见面了!");
request.getRequestDispatcher("/WEB-INF/jsps/index.jsp").forward(request, respone);
}
}
3.将自定义的Controller交给spring管理
<bean name="/htppController.do" class="com.study.controller.HtppController"></bean>
4.jsp页面显示结果
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>springmvc</title>
</head>
<body>
${hello }
</body>
</html>
注:两个处理器适配器是可以共存的.