• Spring MVC新手教程(二)


    第一篇文章宏观讲了Spring MVC概念,以及分享了一个高速入门的样例


    这篇文章主要来谈谈Spring MVC的配置文件。


    首先来谈谈web.xml: web项目启动时自己主动载入到内存中的信息,比方server配置參数,<listener>监听器。<filter>过滤器,<servlet>等。再如,假设在项目中使用了spring框架。则必须定义ContextLoaderListener。那么在启动Web容器时。会自己主动装配Spring applicationContext.xml的配置信息。这里贴出一个典型的web.xml文件,凝视写的非常具体:

    <span style="font-size:14px;"><?xml version="1.0" encoding="UTF-8"?

    > <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>Spring MVC</display-name> <!-- 这里是指定 Spring配置文件:contextConfigLocation的位置--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/configs/spring/applicationContext*.xml</param-value> </context-param> <!-- 启动Spring容器须要的类文件 --> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <!-- DispatcherServlet, Spring MVC的核心 --> <servlet> <servlet-name>mvc-dispatcher</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- DispatcherServlet相应的上下文配置。 默觉得/WEB-INF/$servlet-name$-servlet.xml --> <init-param> <param-name>contextConfigLocation</param-name> <!-- 指定Spring MVC的配置文件的详细位置,这一參数能够不指定。那就是默认设置。上面有说 --> <param-value>/WEB-INF/configs/spring/mvc-dispatcher-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>mvc-dispatcher</servlet-name> <!-- "/"写法,mvc-dispatcher拦截全部的请求--> <url-pattern>/</url-pattern> </servlet-mapping> </web-app></span>




    再来说下Spring MVC配置文件mvc-dispatcher-servlet.xml:web项目启动时。在启动对应的servlet时会配置Spring MVC。一个典型的Spring MVC的配置文件例如以下:

    <pre name="code" class="html"><span style="font-size:14px;"><?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: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.xsd
            http://www.springframework.org/schema/context 
            http://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    
    	<!-- DispatcherServlet使用, 提供其相关的Spring MVC配置 -->
    
    	<!-- 启用Spring基于annotation的DI-->
    	<context:annotation-config />
    
    	<!-- DispatcherServlet上下文, 仅仅管理@Controller类型的bean, 忽略其它型的bean, 如@Service -->
    	<context:component-scan base-package="com.xidian.mvcdemo">
    		<context:include-filter type="annotation"
    			expression="org.springframework.stereotype.Controller" />
    	</context:component-scan>
    
    	<!-- HandlerMapping, 无需配置, Spring MVC能够默认启动。 DefaultAnnotationHandlerMapping 
    		annotation-driven HandlerMapping -->
    
    	<!-- 扩充了注解功能,能够将请求參数绑定到控制器參数,比方能够在类中的某个方法上加上。进一步指定url,极大提高开发效率 -->
    	<mvc:annotation-driven />
    
    	<!-- 静态资源处理, css, js。 imgs等。为web项目映射详细的资源路径-->
    	<mvc:resources mapping="/resources/**" location="/resources/" />
    
    
    	<!-- 配置ViewResolver。视图解析器,还能够有多个,仅仅要附上对应的order就可以 ,实际执行时会次序载入-->
    	<bean
    		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    		<property name="viewClass"
    			value="org.springframework.web.servlet.view.JstlView" />
    		<property name="prefix" value="/WEB-INF/jsps/" />
    		<property name="suffix" value=".jsp" />
    	</bean>
    
    	<!-- Spring mvc 提供为了实现文件的上下传 -->
    	<!--200*1024*1024即200M resolveLazily属性启用是为了推迟文件解析。以便捕获文件大小异常 -->
    	<bean id="multipartResolver"
    		class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    		<property name="maxUploadSize" value="209715200" />
    		<property name="defaultEncoding" value="UTF-8" />
    		<property name="resolveLazily" value="true" />
    	</bean>
    
    </beans></span>


    
    最后来说下Spring容器的配置文件applicationContext.xml:这里没太多涉及,详细的配置细节能够看《Spring新手教程(一)》。在后面的Mybatis系列文章中会结合来做一个简单的demo,Spring
     MVC入门系列就是三篇文章。另一篇文章会详细说些开发中经常使用的标签的注意事项和一些Spring MVC开发思想。
    

    <span style="font-size:14px;"><?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:context="http://www.springframewor                          k.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.xsd
            http://www.springframework.org/schema/context 
            http://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc.xsd">
         
        <!-- 启用Spring基于annotation的DI-->
    	<context:annotation-config />
    	
    	<context:component-scan base-package="com.xidian.mvcdemo">
    	    <!-- 提示Spring不须要管理com.xidian.mvc.demo下Controller属性修饰的对象 -->   
    		<context:exclude-filter type="annotation"
    			expression="org.springframework.stereotype.Controller" /> 
    	</context:component-scan>
    	
    </beans></span>





    你看会了吗?欢迎讨论http://blog.csdn.net/code_7




  • 相关阅读:
    Flask 请求源码分析
    Flask 上下文管理
    flask-基础知识
    分布式爬虫
    数据结构
    webpack安装和入门
    vue相关
    vue入门
    修改npm全局安装模式的路径
    Vue2+VueRouter2+Webpack+Axios
  • 原文地址:https://www.cnblogs.com/zsychanpin/p/7211273.html
Copyright © 2020-2023  润新知