SpringMVC具有三种处理器适配器,他们分别是BeanNameUrlHandlerMapping、SimpleControllerHandlerAdapter、ControllerClassNameHandlerMapping,可以分别使用这三种处理器适配器进行获取Controller。
程序结构如下:
首先,来介绍BeanNameUrlHandlerMapping映射,如下所示,这种映射使用Bean的name来访问控制器,它根据类名(Mycontroller)类名.do来访问,类名首字母小写。
(1)写UserController.java类
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界面
<%@ 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页面
<?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
<?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配置文件如下:
<?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。
<?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>
运行结果:
原文地址:https://www.cnblogs.com/longlyseul/p/11380570.html
时间: 2024-11-10 01:38:46