SpringMVC-简略学习一下

SpringMVC

    一、简介下

            SpringMVC是一个优秀的表现层框架,他最主要的作用就是接受页面请求,并将处理结果返回给前端。其核心是一个控制器org.springframework.web.servlet.DispatcherServlet是一个servlet。这个控制器负责SpringMVC所有的流程控制。

            web.xml中的配置

<servlet>
  	<servlet-name>springmvc</servlet-name>
  	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  	<!-- tomcat启动的时候加载这个servlet -->
  	<load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
  	<servlet-name>springmvc</servlet-name>
  	<url-pattern>*.action</url-pattern>
  </servlet-mapping>
	tomcat会拦截所有的以action结尾的请求

        如果不写配置文件,他会自动的找/WEB_INF/springmvc-servlet.xml这个配置文件,配置文件的名称和前端控制器的名称一致。

    二、加载Springmvc配置文件

<!-- SpringMVC前端控制器 -->
  <servlet>
  	<servlet-name>springmvc</servlet-name>
  	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  	<!-- 指定mvc核心配置文件 -->
  	<init-param>
  		<param-name>contextConfigLocation</param-name>
  		<param-value>classpath:SpringMVC.xml</param-value>
  	</init-param>
  	<!-- tomcat启动的时候加载这个servlet -->
  	<load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
  	<servlet-name>springmvc</servlet-name>
  	<url-pattern>*.action</url-pattern>
  </servlet-mapping>

三、简单的例子

    SpringMVC配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
	
	<!-- 配置@Controller注解扫描 -->
	<context:component-scan base-package="org.lier.controller"></context:component-scan>
</beans>
控制层写法:
	@Controller(控制层注解)
	public class ItemsController {
	
	@RequestMapping("/list")
	public ModelAndView listItems() throws Exception{
		List<Items>itemList = new ArrayList<>();
		……
	//创建modelandView对象
		ModelAndView modelAndView = new ModelAndView();
		//添加model:model中存放了返回给页面的数据
		modelAndView.addObject("itemList", itemList);
		//添加视图:view指定了返回的页面位置
		modelAndView.setViewName("/WEB-INF/jsp/itemList.jsp");
//		modelAndView.setViewName("itemsList");	
		return modelAndView;
}

四、架构原理


  1. 用户发送请求至前端控制器DispatcherServlet

  2. DispatcherServlet收到请求调用HandlerMapping处理器映射器。

  3. 处理器映射器根据请求url找到具体的处理器,生成处理器对象及处理器拦截器(如果有则生成)一并返回给DispatcherServlet

  4. DispatcherServlet通过HandlerAdapter处理器适配器调用处理器

  5. 执行处理器(Controller,也叫后端控制器)

  6. Controller执行完成返回ModelAndView

  7. HandlerAdaptercontroller执行结果ModelAndView返回给DispatcherServlet

  8. DispatcherServletModelAndView传给ViewReslover视图解析器

  9. ViewReslover解析后返回具体View

  10. DispatcherServletView进行渲染视图(即将模型数据填充至视图中)。

    DispatcherServlet响应用户

五、组件

    DispatcherServlet:前端控制器

        用户请求到达前端控制器,它就相当于mvc模式中的cdispatcherServlet是整个流程控制的中心,由它调用其它组件处理用户的请求,dispatcherServlet的存在降低了组件之间的耦合性。

    HandlerMapping:处理器映射器

    HandlerMapping负责根据用户请求找到Handler即处理器,springmvc提供了不同的映射器实现不同的映射方式,例如:配置文件方式,实现接口方式,注解方式等。

    Handler:处理器

    Handler是继DispatcherServlet前端控制器的后端控制器,在DispatcherServlet的控制下Handler对具体的用户请求进行处理。

由于Handler涉及到具体的用户业务请求,所以一般情况需要程序员根据业务需求开发Handler

    HandlAdapter:处理器适配器

    通过HandlerAdapter对处理器进行执行,这是适配器模式的应用,通过扩展适配器可以对更多类型的处理器进行执行。

    ViewResolver:视图解析器

    ViewResolver负责将处理结果生成View视图,ViewResolver首先根据逻辑视图名解析成物理视图名即具体的页面地址,再生成View视图对象,最后对View进行渲染将处理结果通过页面展示给用户。

    View:视图

    springmvc框架提供了很多的View视图类型的支持,包括:jstlViewfreemarkerViewpdfView等。我们最常用的视图就是jsp

    一般情况下需要通过页面标签或页面模版技术将模型数据通过页面展示给用户,需要由程序员根据业务需求开发具体的页面。

说明:在springmvc的各个组件中,处理器映射器、处理器适配器、视图解析器称为springmvc的三大组件。

需要用户开放的组件有handlerview

    

    1、注解扫描(扫描@Controller

        扫描多个包的时候使用逗号进行分割

<context:component-scan base-package="cn.lier.controller"></context:component-scan>

    2、RequestMappingHandlerMapping

        注解式处理器映射器,对类中标记@ResquestMapping的方法进行映射,根据ResquestMapping定义的url匹配ResquestMapping标记的方法,匹配成功返回HandlerMethod对象给前端控制器,HandlerMethod对象中封装url对应的方法Method

  spring3.1版本开始,废除了DefaultAnnotationHandlerMapping的使用,推荐使用RequestMappingHandlerMapping完成注解式处理器映射。

配置如下:

<!-- 注解形式的处理器映射器	 →
	
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean>

    3、RequestMappingHandlerAdapter

        注解式处理器适配器,对标记@ResquestMapping的方法进行适配。

       从spring3.1版本开始,废除了AnnotationMethodHandlerAdapter的使用,推荐使用RequestMappingHandlerAdapter完成注解式处理器适配。

配置最新版的处理器适配器
	 <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean>

    4、Springmvc推荐使用注解驱动自动匹配最新版的处理器映射器和处理器适配器

<mvc:annotation-driven></mvc:annotation-driven>

    5、视图解析器

<!-- 配置视图解析器
	 	  目的就是解决前缀后后缀的问题,不用写页面的完整路径了,省事 -->
	 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
	 <!-- 真正的页面路径:前缀+去掉后缀名的页面名称+后缀 -->
	 	<!-- 前缀 -->
	 	<property name="prefix" value="/WEB-INF/jsp/"></property>
	 	<!-- 后缀 -->
	 	<property name="suffix" value=".jsp"></property>
	 </bean>
     InternalResourceViewResolver:支持JSP视图解析

    viewClassJstlView表示JSP模板页面需要使用JSTL标签库,所以classpath中必须包含jstl的相关jar包。此属性可以不设置,默认为JstlView

    prefixsuffix:查找视图页面的前缀和后缀,最终视图的址为:前缀+逻辑视图名+后缀,逻辑视图名需要在controller中返回ModelAndView指定,比如逻辑视图名为hello,则最终返回的jsp视图地址“WEB-INF/jsp/hello.jsp”

    6、SpringMVC的配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" 
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" 
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" 
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
	http://www.springframework.org/schema/context 
	http://www.springframework.org/schema/context/spring-context-4.0.xsd
	http://www.springframework.org/schema/aop 
	http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 
	http://www.springframework.org/schema/tx 
	http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
	http://www.springframework.org/schema/util 
	http://www.springframework.org/schema/util/spring-util-4.0.xsd
	http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
	<!-- 配置@Controller注解扫描 -->
	<context:component-scan base-package="org.lier.controller"></context:component-scan>
	<!-- 如果没有显示的配置处理器映射器和处理器适配器,springmvc回去默认的DispatcherServlet.properties中进行查找对应的处理器映射器和处理器适配器
	使用,这样每个请求都会都要扫描一次默认的配置文件,效率非常低,降低访问速度所以要显示的配置处理器映射器和处理器shipeiqi
	 -->
	 <!-- 注解形式的处理器映射器	 -->
	 <!-- <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"></bean>
	 注解形式的处理器适配器
	 <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"></bean>
	  -->
	 <!-- 配置最新版的处理器映射器 -->
	<!--  <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean>
	 配置最新版的处理器适配器
	 <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean>
	  -->
	  
	  <!-- 注解驱动自动的帮我们找到最新的处理器映射器和处理器适配器 -->
	 <mvc:annotation-driven></mvc:annotation-driven>
	 <!-- 配置视图解析器
	 	  目的就是解决前缀后后缀的问题,不用写页面的完整路径了,省事 -->
	 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
	 <!-- 真正的页面路径:前缀+去掉后缀名的页面名称+后缀 -->
	 	<!-- 前缀 -->
	 	<property name="prefix" value="/WEB-INF/jsp/"></property>
	 	<!-- 后缀 -->
	 	<property name="suffix" value=".jsp"></property>
	 </bean>
</beans>
文件头的检验好像是所有的都给列了啊!


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值