java中什么是处理器适配器_SpringMVC的三种处理器适配器

SpringMVC具有三种处理器适配器,他们分别是BeanNameUrlHandlerMapping、SimpleControllerHandlerAdapter、ControllerClassNameHandlerMapping,可以分别使用这三种处理器适配器进行获取Controller。

程序结构如下:

1bd293dadd181ce68ef5a6e7252e2287.png

首先,来介绍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">

运行结果如下所示:

11af22debd2a397cd3acdeb801573823.png

(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

运行结果:

0de49b4474d72859ba5c601783013499.png

(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">

运行结果:

389abcbd2b039ecc40c2f63f20e010b3.png

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值