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界面
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
My JSP 'index.jsp' starting page${hello }
(3)写web.xml页面
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">
Spring_002
springmvc
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
/WEB-INF/config/springmvc.xml
springmvc
*.do
index.jsp
(4)SpringMVC.xml配置BeanNameUrlHandlerMapping
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">
运行结果如下所示:
(2)其次,来介绍SimpleControllerHandlerAdapter映射,寻找Controller 。根据浏览器url匹配简单url的key,key又Controller的id找到Controller。
更改SpringMVC.xml文件,配置一个SimpleControllerHandlerAdapter映射。
SpringMVC.xml配置文件如下:
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">
userController
userController
userController
运行结果:
(3)最后来介绍ControllerClassNameHandlerMapping,寻找Controller, 根据类名(MyController)类名.do来访问,类名首字母小写。更改SpringMVC.xml文件,配置一个ControllerClassNameHandlerMapping。
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">
运行结果: