zoukankan      html  css  js  c++  java
  • Spring MVC 配置文件dispatcher-servlet.xml 文件详解

    <?xml version="1.0" encoding="UTF-8"?>  
    <beans xmlns="http://www.springframework.org/schema/beans"  
           xmlns:mvc="http://www.springframework.org/schema/mvc"  
           xmlns:context="http://www.springframework.org/schema/context"  
           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-3.2.xsd  
                http://www.springframework.org/schema/context  
                http://www.springframework.org/schema/context/spring-context-3.2.xsd  
                http://www.springframework.org/schema/mvc  
                http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">  
        <!-- 使用spring提供的PropertyPlaceholderConfigurer读取数据库配置信息.properties  
         1、这里的classpath可以认为是项目中的src-  
         2、属性名是 locations,使用子标签<list></list>可以指定多个数据库的配置文件,这里指定了一个  
         其中order属性代表其加载顺序,而ignoreUnresolvablePlaceholders为是否忽略不可解析的 Placeholder,  
         如配置了多个PropertyPlaceholderConfigurer,则需设置为true  
         <bean id="propertyConfigurerForProject2" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
        <property name="order" value="2" />  
        <property name="ignoreUnresolvablePlaceholders" value="true" />  
        <property name="locations">  
          <list>  
            <value>classpath:/spring/include/jdbc-parms.properties</value>  
            <value>classpath:/spring/include/base-config.properties</value>  
          </list>  
        </property>  
        </bean>-->  
        <bean id="propertyConfigurer"  
              class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
            <property name="ignoreUnresolvablePlaceholders" value="true"/>  
            <property name="location" value="classpath:/application.properties"/>  
        </bean>  
        <!--注解探测器,在xml配置了这个标签后,spring可以自动去扫描base-pack下面或者子包下面的java文件,  
        如果扫描到有@Component @Controller@Service等这些注解的类,则把这些类注册为bean  
        注意:如果配置了<context:component-scan>那么<context:annotation-config/>标签就可以不用再xml中配置了,因为前者包含了后者。  
        另外<context:annotation-config/>还提供了两个子标签  
        1. <context:include-filter> 2.<context:exclude-filter>  
        <context:component-scan>有一个use-default-filters属性,改属性默认为true,这就意味着会扫描指定包下的全部的标有@Component的类,  
        并注册成bean.也就是@Component的子注解@Service,@Reposity等。所以如果仅仅是在配置文件中这么写  
        <context:component-scan base-package="com.test.myapp.web"/>  
         Use-default-filter此时为true,那么会对base-package包或者子包下的jun所有的进行java类进行扫描,并把匹配的java类注册成bean。  
        可以发现这种扫描的粒度有点太大,如果你只想扫描指定包下面的Controller,该怎么办?此时子标签<context:incluce-filter>就起到了勇武之地。如下所示  
        <context:component-scan base-package="com.test.myapp.web.Controller">  
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>  
        </context:component-scan>  
        这样就会只扫描base-package指定下的有@Controller下的java类,并注册成bean.  
        但是因为use-dafault-filter在上面并没有指定,默认就为true,所以当把上面的配置改成如下所示的时候,就会产生与你期望相悖的结果(注意base-package包值得变化)  
        <context:component-scan base-package="com.test.myapp.web ">  
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>  
        </context:component-scan>  
        此时,spring不仅扫描了@Controller,还扫描了指定包所在的子包service包下注解@Service的java类  
        此时指定的include-filter没有起到作用,只要把use-default-filter设置成false就可以了。这样就可以避免在base-packeage配置多个包名这种不是很优雅的方法来解决这个问题了。  
        另外在我参与的项目中可以发现在base-package指定的包中有的子包是不含有注解了,所以不用扫描,此时可以指定<context:exclude-filter>来进行过滤,说明此包不需要被扫描。综合以上说明  
        Use-dafault-filters=”false”的情况下:<context:exclude-filter>指定的不扫描,<context:include-filter>指定的扫描-->  
        <context:component-scan base-package="com.test.myapp">  
            <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>  
        </context:component-scan>  
      
        <!-- 视图解析器,根据视图的名称new ModelAndView(name),在配置文件查找对应的bean配置  
         这个视图解析器跟XmlViewResolver有点类似,也是通过把返回的逻辑视图名称去匹配定义好的视图bean对象。  
         不同点有二,一是BeanNameViewResolver要求视图bean对象都定义在Spring的application context中,  
         而XmlViewResolver是在指定的配置文件中寻找视图bean对象,二是BeanNameViewResolver不会进行视图缓存。  
         如果没有设置viewResolver,spring使用InternalResourceViewResolver进行解析。  
         Spring实现ViewResolver的非抽象类且我们经常使用的viewResolver有以下四种:  
         1、InternalResourceViewResolver  将逻辑视图名字解析为一个路径  
         2、BeanNameViewResolver  将逻辑视图名字解析为bean的Name属性,从而根据name属性,找定义View的bean  
         3、ResourceBundleResolver   和BeanNameViewResolver一样,只不过定义的view-bean都在一个properties文件中,用这个类进行加载这个properties文件  
         4、XmlViewResolver  和ResourceBundleResolver一样,只不过定义的view-bean在一个xml文件中,用这个类来加载xml文件  
         DispatcherServlet会加载所有的viewResolver到一个list中,并按照优先级进行解析。  
         我们不想只使用一种视图解析器的话,可以在[spring-dispatcher-name]-servlet.xml定义多个viewResolver:  
         注意order中的值越小,优先级越高。而id为viewResolver 的viewResolver的优先级是最低的。  
         -->  
        <bean class="org.springframework.web.servlet.view.BeanNameViewResolver">  
            <property name="order" value="1"/>  
        </bean>  
        <!--<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">-->  
            <!--<property name="prefix" value="/WEB-INF/"/>-->  
            <!--<property name="suffix" value=".html"/>-->  
        <!--</bean>-->  
        <!--基于json格式的mvc交互-->  
        <bean name="jsonView" class="com.test.myapp.MappingFastJsonJsonView">  
            <property name="contentType" value="application/json;charset=UTF-8"/>  
        </bean>  
      
        <!-- spring mvc +servlet3.0上传文件配置,文件上传插件uploadify的应用  
         1)  在Web.xml的配置  
         需要在web.xml添加multipart-config,如下所示  
         <servlet>  
            <servlet-name>AcrWeb</servlet-name>  
            <servlet-class>  
                org.springframework.web.servlet.DispatcherServlet  
            </servlet-class>  
            <load-on-startup>1</load-on-startup>  
            <multipart-config>  
                <max-file-size>52428800</max-file-size>  
                <max-request-size>52428800</max-request-size>  
                <file-size-threshold>0</file-size-threshold>  
            </multipart-config>  
        </servlet>  
        2)  在spring的application.xml(名字不一定是application)的配置,需要在该配置文件下添加一个如下的bean  
           spring mvc +servlet3.0上传文件配置  
        <bean id="multipartResolver"  
              class="org.springframework.web.multipart.support.StandardServletMultipartResolver">  
        </bean>  
        3)  在jsp页面中需要引入一些相关的该插件的包  
         <script src="<c:url value="/asset/admin/js/uploadify/jquery.uploadify.min.js"/>"></script>  
        4)   定义一个选择文件的input框  
         <div class="box-body">  
           <span class="label input g1">上传apk</span>  
           <input id="apk_upload" name="apk_upload" type="file"/>  
           <input id="apkUrl" type="hidden" name="apkUrl"/>  
         </div>  
        5)  Input file与插件进行绑定  
        $("#apk_upload").uploadify({  
                swf: "<c:url value='/asset/admin/js/uploadify/uploadify.swf'/>",  
                //cancelImg : "<c:url value='/asset/admin/js/uploadify/uploadify-cancel.png'/>",  
                uploader: "/acr/admin/app/apkupload",  
                fileObjName: "file",//对应着文件输入框  
                300,  
                buttonText: '<img src="/acr/asset/admin/js/uploadify/upload.png" />',  
                // onInit: function () { $(".uploadify-queue").hide();  },  
                //removeCompleted : false,  
                onUploadSuccess : function(file, data, response) {  
                    $("#apkUrl").val(data);  
                },  
                onUploadError : function(file, errorCode, errorMsg, errorString) {  
                    alert('文件 ' + file.name + ' 上传失败: ' + errorString);  
                }  
            });  
             注意:该插件的uploadify.swf文件时放入到项目的某一个文件下面  
             Uploader的值对应的是url,该值映射到了springmvc的一个方法,该方法是文件上传的核心,  
             负责把文件写到指定位置的地方去。  
             6)  Spring 后台代码的实现  
             @RequestMapping(value = "/apkupload", method=RequestMethod.POST)  
        public @ResponseBody  String apkUpload(  
                @RequestParam MultipartFile file,  
                Model model,  
                HttpServletRequest request) throws IOException {  
            InputStream input = null;  
            OutputStream output = null;  
            String root = "H:/file";  
            //生成了文件名字  
            String filename = file.getOriginalFilename();  
            //文件要上传的位置  
            String fileFullName = buildUpPath(root, filename);  
            try {  
                File dir = new File(root);  
                if(!dir.exists()){  
                    dir.mkdirs();  
                }  
                input = file.getInputStream();  
                output = new FileOutputStream(new File(fileFullName));  
                //保存文件  
                IOUtils.copy(input, output);  
            } catch (Throwable e) {  
                throw e;  
            }finally{  
                IOUtils.closeQuietly(input);  
                IOUtils.closeQuietly(output);  
            }  
            return root+"/"+filename;  
        }  
           其中filename对应着步骤5的onUploadSuccess中的data  
        -->  
        <bean id="multipartResolver"  
              class="org.springframework.web.multipart.support.StandardServletMultipartResolver">  
        </bean>  
    </beans>  

    本文链接 http://tec.5lulu.com/code/k4n1g5v1ra8i5f.html

  • 相关阅读:
    Shell编程-05-Shell中条件测试与比较
    Shell编程-04-Shell中变量数值计算
    Shell编程-03-Shell中的特殊变量和扩展变量
    Shell编程-02-Shell变量
    Shell编程-01-Shell脚本初步入门
    Windows与Linux相互远程桌面连接
    awk基础05-自定义函数和脚本
    使用Kafka Connect创建测试数据生成器
    设置KAFKA
    Apache Kafka使用默认配置执行一些负载测试来完成性能测试和基准测试
  • 原文地址:https://www.cnblogs.com/wkrbky/p/5930648.html
Copyright © 2011-2022 走看看