SpringMVC的三种处理器适配器
SpringMVC具有三种处理器适配器,他们分别是BeanNameUrlHandlerMapping、SimpleControllerHandlerAdapter、ControllerClassNameHandlerMapping,可以分别使用这三种处理器适配器进行获取Controller。
程序结构如下:
首先,来介绍BeanNameUrlHandlerMapping映射,如下所示,这种映射使用Bean的name来访问控制器,它根据类名(Mycontroller)类名.do来访问,类名首字母小写。
(1)写UserController.java类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | package com.zk.UserController; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.Controller; public class UserController implements Controller{ public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { // TODO Auto-generated method stub // 接受请求,接受参数,验证参数 // 封装参数,调用业务方法 // 返回视图 ModelAndView mv= new ModelAndView(); //设置页面回显数据 mv.addObject( "hello" , "凤姐喜欢你" ); //指定跳转视图,返回物理视图 //mv.setViewName("/index.jsp"); //返回逻辑视图 mv.setViewName( "index" ); return mv; } } |
(2)写一个index.jsp界面
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | <%@ page language= "java" import = "java.util.*" pageEncoding= "utf-8" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+ "://" +request.getServerName()+ ":" +request.getServerPort()+path+ "/" ; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" > <html> <head> <base href= "<%=basePath%>" > <title>My JSP 'index.jsp' starting page</title> <meta http-equiv= "Content-Type" content= "text/html;charset=utf-8" /> <meta http-equiv= "pragma" content= "no-cache" > <meta http-equiv= "cache-control" content= "no-cache" > <meta http-equiv= "expires" content= "0" > <meta http-equiv= "keywords" content= "keyword1,keyword2,keyword3" > <meta http-equiv= "description" content= "This is my page" > <!-- <link rel= "stylesheet" type= "text/css" href= "styles.css" > --> </head> <body> ${hello } </body> </html> |
(3)写web.xml页面
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | <?xml version= "1.0" encoding= "UTF-8" ?> <web-app version= "2.5" xmlns= "http://java.sun.com/xml/ns/javaee" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http: //java.sun.com/xml/ns/javaee http: //java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <display-name>Spring_002</display-name> <servlet> <servlet-name>springmvc</servlet-name> <servlet- class >org.springframework.web.servlet.DispatcherServlet</servlet- class > <!-- Spring 默认加载SpringMVC的配置文件,这个需要满足一下规则: 命名规则:Servlet-name-servlet.xml========>SpringMVC-servlet.xml 路径规范SpringMVC-serlvet.xml必须放在WEB-INF下 --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/config/springmvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*. do </url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app> |
(4)SpringMVC.xml配置BeanNameUrlHandlerMapping
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <?xml version= "1.0" encoding= "UTF-8" ?> <beans xmlns= "http://www.springframework.org/schema/beans" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xmlns:aop= "http://www.springframework.org/schema/aop" xsi:schemaLocation="http: //www.springframework.org/schema/beans http: //www.springframework.org/schema/beans/spring-beans.xsd http: //www.springframework.org/schema/aop http: //www.springframework.org/schema/aop/spring-aop.xsd http: //www.springframework.org/schema/tx http: //www.springframework.org/schema/tx/spring-tx-3.2.xsd"> <bean id= "userController" name= "/hello.do" class = "com.zk.UserController.UserController" ></bean> <!-- 配置处理器映射器 --> <bean class = "org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" ></bean> <bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver" > <property name= "prefix" value= "/WEB-INF/jsp/" ></property> <property name= "suffix" value= ".jsp" ></property> </bean> </beans> |
运行结果如下所示:
(2)其次,来介绍SimpleControllerHandlerAdapter映射,寻找Controller 。根据浏览器url匹配简单url的key,key又Controller的id找到Controller。
更改SpringMVC.xml文件,配置一个SimpleControllerHandlerAdapter映射。
SpringMVC.xml配置文件如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | <?xml version= "1.0" encoding= "UTF-8" ?> <beans xmlns= "http://www.springframework.org/schema/beans" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xmlns:aop= "http://www.springframework.org/schema/aop" xsi:schemaLocation="http: //www.springframework.org/schema/beans http: //www.springframework.org/schema/beans/spring-beans.xsd http: //www.springframework.org/schema/aop http: //www.springframework.org/schema/aop/spring-aop.xsd http: //www.springframework.org/schema/tx http: //www.springframework.org/schema/tx/spring-tx-3.2.xsd"> <bean id= "userController" name= "/hello.do" class = "com.zk.UserController.UserController" ></bean> <!-- url进行集中配置 --> <bean class = "org.springframework.web.servlet.handler.SimpleUrlHandlerMapping" > <property name= "mappings" > <props> <prop key= "/min.do" >userController</prop> <prop key= "/abc.do" >userController</prop> <prop key= "/ss.do" >userController</prop> </props> </property> </bean> <!-- 配置处理器适配器:负责执行UserController,springMVC默认处理器适配器 --> <bean class = "org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" ></bean> <bean class = "com.zk.UserController.UserController" ></bean> <bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver" > <property name= "prefix" value= "/WEB-INF/jsp/" ></property> <property name= "suffix" value= ".jsp" ></property> </bean> </beans> |
运行结果:
(3)最后来介绍ControllerClassNameHandlerMapping,寻找Controller, 根据类名(MyController)类名.do来访问,类名首字母小写。更改SpringMVC.xml文件,配置一个ControllerClassNameHandlerMapping。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <?xml version= "1.0" encoding= "UTF-8" ?> <beans xmlns= "http://www.springframework.org/schema/beans" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xmlns:aop= "http://www.springframework.org/schema/aop" xsi:schemaLocation="http: //www.springframework.org/schema/beans http: //www.springframework.org/schema/beans/spring-beans.xsd http: //www.springframework.org/schema/aop http: //www.springframework.org/schema/aop/spring-aop.xsd http: //www.springframework.org/schema/tx http: //www.springframework.org/schema/tx/spring-tx-3.2.xsd"> <!-- 通过类名. do 来访问,类名首字母小写 --> <bean class = "org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" ></bean> <bean class = "com.zk.UserController.UserController" ></bean> <bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver" > <property name= "prefix" value= "/WEB-INF/jsp/" ></property> <property name= "suffix" value= ".jsp" ></property> </bean> </beans> |
运行结果:
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)