1,映射器
1.1 ControllerBean +BeanNameUrlHandlerMapping
其中ControllerBean 需要指明name(url)通过url找到对应的Controller
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd"><!-- 配置Handle handle:分发请求 --><bean name="/hello" class="com.xiaohui.controller.HelloController"/><!-- 处理映射器将bean的name作为url进行查找,需要在配置Handle是指定name(即url) --><bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" /><!-- SimpleControllerHandlerAdapter 是一个处理器适配器,所有的适配都要实现HandlerAdapter --><bean class="org.springframework.web.portlet.mvc.SimpleControllerHandlerAdapter" /><!-- 配置视图渲染器 --><bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"/></beans>
1.2 ControllerBean + SimpleUrlHandlerMapping
通过给bean定义id,在SimpleUrlHandlerMapping 中配置键值对进行关联url与对应的Controller。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd"><!-- 配置Handle handle:分发请求 --><bean id="hellobeanid" class="com.xiaohui.controller.HelloController"/><bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping" ><property name="mappings"><props><prop key="/hello1">hellobeanid</prop></props></property></bean><!-- SimpleControllerHandlerAdapter 是一个处理器适配器,所有的适配都要实现HandlerAdapter --><bean class="org.springframework.web.portlet.mvc.SimpleControllerHandlerAdapter" /><!-- 配置视图渲染器 --><bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"/></beans>
java读取jar包下的配置文件。 由以上两个映射器可以看出适配器在spring中主要用来解析url。
2,适配器
2.1 SimpleControllerHandlerAdapter
<bean class="org.springframework.web.portlet.mvc.SimpleControllerHandlerAdapter" />
该适配器所能掉起的Controller必须实现org.springframework.web.servlet.mvc.Controller 接口。
<!-- SimpleControllerHandlerAdapter 是一个处理器适配器,所有的适配都要实现HandlerAdapter,
该适配器对应的Controller 必须实现org.springframework.web.servlet.mvc.Controller 接口 -->
bean class="org.springframework.web.portlet.mvc.SimpleControllerHandlerAdapter" />
SimpleControllerHandlerAdapter源码如下:
public class SimpleControllerHandlerAdapter implements HandlerAdapter, PortletContextAware {private PortletContext portletContext;@Overridepublic void setPortletContext(PortletContext portletContext) {this.portletContext = portletContext;}@Overridepublic boolean supports(Object handler) {return (handler instanceof Controller);}//省略下面代码....
}
handler instanceof Controller);}//省略下面代码....
}
该适配器对应的Controller如下:
package com.xiaohui.controller;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 HelloController implements Controller{@Overridepublic ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {ModelAndView modelAndView = new ModelAndView();//添加数据模型modelAndView.addObject("msg", "hello springmvc");//设置逻辑试图名,视图解析器会根据改名字解析到具体的视图页面modelAndView.setViewName("/WEB-INF/jsp/hello.jsp");return modelAndView;}}
按键映射器哪个好用、2.2 HttpRequestHandlerAdapter
该适配器源码如下:
public class HttpRequestHandlerAdapter implements HandlerAdapter {@Overridepublic boolean supports(Object handler) {return (handler instanceof HttpRequestHandler);}@Overridepublic ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler)throws Exception {((HttpRequestHandler) handler).handleRequest(request, response);return null;}@Overridepublic long getLastModified(HttpServletRequest request, Object handler) {if (handler instanceof LastModified) {return ((LastModified) handler).getLastModified(request);}return -1L;}
}
handler instanceof HttpRequestHandler);}@Overridepublic ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler)throws Exception {((HttpRequestHandler) handler).handleRequest(request, response);return null;}@Overridepublic long getLastModified(HttpServletRequest request, Object handler) {if (handler instanceof LastModified) {return ((LastModified) handler).getLastModified(request);}return -1L;}
}
通过源码既可以看出该适配器所能掉起的Controller必须实现 HttpRequestHandler接口
controller代码如下:
public class HelloController2 implements HttpRequestHandler{@Overridepublic void handleRequest(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//添加数据模型req.setAttribute("msg", "hello springmvc22222");//设置逻辑试图名,视图解析器会根据改名字解析到具体的视图页面req.getRequestDispatcher("/WEB-INF/jsp/hello.jsp").forward(req, resp);}
}
此类控制器可以通过response设置相应内容比如返回json数据等。
vs时间映射器。 而通过上面两例适配器则可以看出SpringMvc根据适配器规则去调用对应规则的Controller;
所以在SpringMvc 中 当用户通过一个Url请求后,在springmvc中则通过sprignmvc解析器 及 springMvc解析器在确定执行哪一个Controller;
如上例子使用的为非注解映射器和适配器,其中Controller通过实现接口,所以内部只有一个实现方法。后面通过注解形式实现则可以有多个方法,供多个请求访问调用。
版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。
工作时间:8:00-18:00
客服电话
电子邮件
admin@qq.com
扫码二维码
获取最新动态