大家好,又见面了,我是你们的朋友全栈君。
大家知道springmvc 是一个非常优良的框架,配置灵活实现简单,只需我们更多的关注我们的业务逻辑。今天我们就通过一个简单的例子模拟适配生成过程。
处理器适配器HandlerAdapter
1、SimpleControllerHandlerAdapter
表示所有实现了org.springframework.web.servlet.mvc.Controller接口的Bean 可以作为Spring Web MVC 中的处理器。
如果需要其他类型的处理器可以通过实现HandlerAdapter 来解决。
案例分析:
(1)模拟一个handlerAdapter
- //定义一个Adapter接口
- public interface HandlerAdapter {
- public boolean supports(Object handler);
- public void handle(Object handler);
- }
//定义一个Adapter接口
public interface HandlerAdapter {
public boolean supports(Object handler);
public void handle(Object handler);
}
(2)模拟一个Controller
- //以下是三种Controller实现
- public interface Controller {
- }
- public class HttpController implements Controller{
- public void doHttpHandler(){
- System.out.println(“http…”);
- }
- }
- public class SimpleController implements Controller{
- public void doSimplerHandler(){
- System.out.println(“simple…”);
- }
- }
- public class AnnotationController implements Controller{
- public void doAnnotationHandler(){
- System.out.println(“annotation…”);
- }
- }
//以下是三种Controller实现
public interface Controller {
}
public class HttpController implements Controller{
public void doHttpHandler(){
System.out.println("http...");
}
}
public class SimpleController implements Controller{
public void doSimplerHandler(){
System.out.println("simple...");
}
}
public class AnnotationController implements Controller{
public void doAnnotationHandler(){
System.out.println("annotation...");
}
}
(3)适配器
- //下面编写适配器类
- public class SimpleHandlerAdapter implements HandlerAdapter {
- public void handle(Object handler) {
- ((SimpleController)handler).doSimplerHandler();
- }
- public boolean supports(Object handler) {
- return (handler instanceof Controller);
- }
- }
- public class HttpHandlerAdapter implements HandlerAdapter {
- public void handle(Object handler) {
- ((HttpController)handler).doHttpHandler();
- }
- public boolean supports(Object handler) {
- return (handler instanceof Controller);
- }
- }
- public class AnnotationHandlerAdapter implements HandlerAdapter {
- public void handle(Object handler) {
- ((AnnotationController)handler).doAnnotationHandler();
- }
- public boolean supports(Object handler) {
- return (handler instanceof Controller);
- }
- }
//下面编写适配器类
public class SimpleHandlerAdapter implements HandlerAdapter {
public void handle(Object handler) {
((SimpleController)handler).doSimplerHandler();
}
public boolean supports(Object handler) {
return (handler instanceof Controller);
}
}
public class HttpHandlerAdapter implements HandlerAdapter {
public void handle(Object handler) {
((HttpController)handler).doHttpHandler();
}
public boolean supports(Object handler) {
return (handler instanceof Controller);
}
}
public class AnnotationHandlerAdapter implements HandlerAdapter {
public void handle(Object handler) {
((AnnotationController)handler).doAnnotationHandler();
}
public boolean supports(Object handler) {
return (handler instanceof Controller);
}
}
(4)DispatcherServlet
- public class Dispatcher {
- public static List<HandlerAdapter> handlerAdapters = new ArrayList<HandlerAdapter>();
- public DispatchServlet(){
- handlerAdapters.add(new AnnotationHandlerAdapter());
- handlerAdapters.add(new HttpHandlerAdapter());
- handlerAdapters.add(new SimpleHandlerAdapter());
- }
- public void doDispatch(){
- //此处模拟SpringMVC从request取handler的对象,仅仅new出,可以出,
- //不论实现何种Controller,适配器总能经过适配以后得到想要的结果
- // HttpController controller = new HttpController();
- // AnnotationController controller = new AnnotationController();
- SimpleController controller = new SimpleController();
- //得到对应适配器
- HandlerAdapter adapter = getHandler(controller);
- //通过适配器执行对应的controller对应方法
- adapter.handle(controller);
- }
- public HandlerAdapter getHandler(Controller controller){
- for(HandlerAdapter adapter: this.handlerAdapters){
- if(adapter.supports(controller)){
- return adapter;
- }
- }
- return null;
- }
- public static void main(String[] args){
- new DispatchServlet().doDispatch();
- }
public class Dispatcher {
public static List<HandlerAdapter> handlerAdapters = new ArrayList<HandlerAdapter>();
public DispatchServlet(){
handlerAdapters.add(new AnnotationHandlerAdapter());
handlerAdapters.add(new HttpHandlerAdapter());
handlerAdapters.add(new SimpleHandlerAdapter());
}
public void doDispatch(){
//此处模拟SpringMVC从request取handler的对象,仅仅new出,可以出,
//不论实现何种Controller,适配器总能经过适配以后得到想要的结果
// HttpController controller = new HttpController();
// AnnotationController controller = new AnnotationController();
SimpleController controller = new SimpleController();
//得到对应适配器
HandlerAdapter adapter = getHandler(controller);
//通过适配器执行对应的controller对应方法
adapter.handle(controller);
}
public HandlerAdapter getHandler(Controller controller){
for(HandlerAdapter adapter: this.handlerAdapters){
if(adapter.supports(controller)){
return adapter;
}
}
return null;
}
public static void main(String[] args){
new DispatchServlet().doDispatch();
}
功能:执行Handler(Controller)
默认走该适配器:
- <!– 配置处理器适配器执行Controller ,springmvc默认的SimpleControllerHandlerAdapter:执行Controller–>
- <bean class=“org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter”></bean>
<!-- 配置处理器适配器执行Controller ,springmvc默认的SimpleControllerHandlerAdapter:执行Controller-->
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean>
适配器源码:
结合Controller分析
2、HttpRequestHandlerAdapter
HTTP请求处理器适配器
HTTP请求处理器适配器将http请求封装成HttpServletResquest 和HttpServletResponse对象,和servlet接口类似
第一步:配置HttpRequestHandlerAdapter适配器
- <!– 配置HttpRequestHandlerAdapter适配器 用来处理实现了HttpRequestHandler后端控制类Controller–>
- <bean class=“org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter”></bean>
<!-- 配置HttpRequestHandlerAdapter适配器 用来处理实现了HttpRequestHandler后端控制类Controller-->
<bean class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter"></bean>
第二步:编写Controller
- public class HttpRequestController implements HttpRequestHandler{
- @Override
- public void handleRequest(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- request.setAttribute(“message”, “我是中国人”);
- request.getRequestDispatcher(“/WEB-INF/jsps/mess.jsp”).forward(request, response);
- }
- }
public class HttpRequestController implements HttpRequestHandler{
@Override
public void handleRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setAttribute("message", "我是中国人");
request.getRequestDispatcher("/WEB-INF/jsps/mess.jsp").forward(request, response);
}
}
第三步:让spring来创建对象Controller,即配置自定义Controller:
- <!– 配置自定义的controller –>
- <bean name=“/hello.do” class=“controller.HttpRequestController”></bean>
<!-- 配置自定义的controller -->
<bean name="/hello.do" class="controller.HttpRequestController"></bean>
第四步:准备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=UTF-8”>
- <title>Insert title here</title>
- </head>
- <body>
- <h1>这是我的第二个适配器</h1>
- <h1>${message }</h1>
- </body>
- </html>
<%@ 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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>这是我的第二个适配器</h1>
<h1>${message }</h1>
</body>
</html>
第四步:调试运行
总结:适配器可以共存,只是控制器实现不同的接口使用不同的适配器而已。
注意:SimpleControllerHandlerAdapter是默认的适配器。如果使用后面这个适配器必须手动配置。否则将会访问失败。
实现了HttpRequestHandler接口:配置文件里面没有配置这个适配器报如下错误:
No adapter forhandler [springMVC_01.HttpRequestController@12d527f]: Does your handlerimplement a supported interface like Controller?
通过这个错误,我们发现:实现Controller接口是默认的,既是说明:SimpleControllerHandlerAdapter是默认的适配器
还必须注意:实现HttpRequestHandler接口的控制器不支持ControllerClassNameHandlerMapping这个处理器映射器。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/145032.html原文链接:https://javaforall.cn