Spring MVC configuration file Dispatcher-servlet.xml file details

Source: Internet
Author: User

<?XML version= "1.0" encoding= "UTF-8"?>  <Beansxmlns= "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/contex T/spring-context-3.2.xsd Http://www.springframework.org/schema/mvc http://www.springframework.or G/schema/mvc/spring-mvc-3.2.xsd ">      <!--Read the database configuration information using the Propertyplaceholderconfigurer provided by spring. Properties 1, here the classpath can be considered as the src-2 in the project, the property name is locations , using sub-tags <list></list> can specify a configuration file for multiple databases, where an order property is specified to represent its load order. The ignoreunresolvableplaceholders is set to true if it ignores unresolved Placeholder, if more than one propertyplaceholderconfigurer is configured <bean Id= "PropertyConfigurerForProject2" class= " Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer "> <property name=" Order "value=" 2 "/&      Gt <property name= "Ignoreunresolvableplaceholders" value= "true"/> <property name= "Locations" > <li St> <value>classpath:/spring/include/jdbc-parms.properties</value> <value>classpath :/spring/include/base-config.properties</value> </list> </property> </bean> -      <BeanID= "Propertyconfigurer"class= "Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">          < Propertyname= "Ignoreunresolvableplaceholders"value= "true"/>          < Propertyname= "Location"value= "Classpath:/application.properties"/>      </Bean>      <!--Note Detector, after the XML is configured with this tag, spring can automatically scan the Java file under Base-pack or under the sub-package, if scanning to a class of these annotations such as @component @[email protected], Register these classes as Beans Note: If <context:component-scan> is configured, the <context:annotation-config/> tag can be configured without XML.      Because the former contains the latter. The <context:annotation-config/> also provides two sub-label 1. <context:include-filter> 2.<context:exclude-filter> <context:component-scan> There is a Use-default-filters property, and the property defaults to True, which means that all classes labeled @component under the specified package are scanned and registered as beans. That is, @component's child annotation @service,@ Reposity and so on. So if it is only in the config file so write <context:component-scan base-package= "Com.test.myapp.web"/> Use-default-filter at this time is true, then      The Java classes are scanned for all of June under the Base-package package or sub-package, and the matching Java class is registered as a bean. You can see that the granularity of this scan is a bit too large, what if you just want to scan the controller under the specified package? At this point, the sub-label <context:incluce-filter> will play a chivalrous place. <context:component-scan base-package= "Com.test.myapp.web.Controller" > <context:include-filter type as shown below = "Annotation" expression= "Org.springframework.stereotype.Controller"/> </context:compoNent-scan> This will only scan the Java class under @controller under the Base-package designation and register as a bean. However, because Use-dafault-filter is not specified above, the default is true, so when the above configuration is changed to the following, it will produce a result contrary to your expectation (note that the Base-package package is worth changing) <context: Component-scan base-package= "Com.test.myapp.web" > <context:include-filter type= "annotation" expression= " Org.springframework.stereotype.Controller "/> </context:component-scan> at this time, spring not only scanned the @controller, but also scanned the specified The Java class for annotations @service under the Child package service package where the package is located does not work at this time, as long as the Use-default-filter is set to false.      This avoids the elegant approach of configuring multiple package names in Base-packeage to solve this problem. In addition, I can find in the project that the base-package specified in the package contains no annotations, so do not scan, you can specify <context:exclude-filter> to filter, that the package does not need to be scanned. Combined with the above instructions use-dafault-filters= "false" in case:<context:exclude-filter> specified does not scan,<context:include-filter> specified scan -      <Context:component-scanBase-package= "Com.test.myapp">          <Context:include-filtertype= "Annotation"expression= "Org.springframework.stereotype.Controller"/>      </Context:component-scan>        <!-- View parser, according to the name of the View new Modelandview (name), find the corresponding bean configuration in the configuration file This view parser is a bit like xmlviewresolver.       It is also by matching the returned logical view name to the defined view Bean object. The difference is two, one is that beannameviewresolver requires that the view Bean object be defined in spring's application context, and Xmlviewresolver is looking for the view bean object in the specified configuration file.       The second is that Beannameviewresolver does not cache the view.       If not set viewresolver,spring use Internalresourceviewresolver for parsing. Spring implements non-abstract classes of viewresolver and we often use the following four kinds of viewresolver: 1, internalresourceviewresolver resolves a logical view name to a path 2, Beannamev Iewresolver parses the logical view name into the bean's Name property, which, according to the name attribute, is the same as the Bean 3, resourcebundleresolver, and beannameviewresolver that define the view, except that the defined View-bean in a properties file, this class is used to load the properties file 4, Xmlviewresolver, and Resourcebundleresolver,       Just the definition of View-bean in an XML file, using this class to load the XML file Dispatcherservlet will load all the viewresolver into a list and parse it by priority. If we don't want to use only one view parser, you can define multiple viewresolver in [spring-dispatcher-name]-servlet.xml: note the smaller the value in order, the higher the priority.       The priority of Viewresolver with ID viewresolver is the lowest.  -      <Beanclass= "Org.springframework.web.servlet.view.BeanNameViewResolver">          < Propertyname= "Order"value= "1"/>      </Bean>      <!--<bean class= "Org.springframework.web.servlet.view.InternalResourceViewResolver" > -          <!--<property name= "prefix" value= "/web-inf/"/> -          <!--<property name= "suffix" value= ". html"/> -      <!--</bean> -      <!--MVC interaction based on JSON format -      <Beanname= "Jsonview"class= "Com.test.myapp.MappingFastJsonJsonView">          < Propertyname= "ContentType"value= "Application/json;charset=utf-8"/>      </Bean>        <!-- Spring MVC +servlet3.0 Upload file configuration, file upload plugin uploadify application 1) the configuration in Web. XML needs to be added Multipart-config in Web. Xml as follows <s ervlet> <servlet-name>AcrWeb</servlet-name> <servlet-class> org.spring Framework.web.servlet.DispatcherServlet </servlet-class> <load-on-startup>1</load-on-star tup> <multipart-config> <max-file-size>52428800</max-file-size> &L T;max-request-size>52428800</max-request-size> <file-size-threshold>0</file-size-threshold&          Gt </multipart-config> </servlet> 2) in spring Application.xml (the name is not necessarily application) configuration, you need to add a following BEA under the configuration file N Spring MVC +servlet3.0 upload file configuration <bean id= "Multipartresolver" class= "Org.springframework.web.mult Ipart.support.StandardServletMultipartResolver "> </bean> 3) in the JSP page need to introduce some related to the plug-in package <script SRC = "<c:url Value= "/asset/admin/js/uploadify/jquery.uploadify.min.js"/> "></script> 4) defines an input box for the selected file <di V class= "Box-body" > <span class= "label input G1" > Upload apk</span> <input id= "Apk_upload" nam  E= "Apk_upload" type= "file"/> <input id= "Apkurl" type= "hidden" name= "Apkurl"/> </div> 5) The Input file is bound to the plug-in $ ("#apk_upload"). Uploadify ({swf: "<c:url value= '/asset/admin/js/uploadify/uploadi              fy.swf '/> ',//cancelimg: "<c:url value= '/asset/admin/js/uploadify/uploadify-cancel.png '/>",              Uploader: "/acr/admin/app/apkupload", Fileobjname: "File",//corresponds to the document input box width:300, ButtonText: '  ',//Oninit:function () {$ (". u  Ploadify-queue "). Hide ();              },//removecompleted:false, onuploadsuccess:function (file, data, response) {    $ ("#apkUrl"). val (data); }, Onuploaderror:function (file, ErrorCode, ErrorMsg, errorstring) {alert (' file ' + File.nam              E + ' upload failed: ' + errorstring);           }          }); Note: The plugin's uploadify.swf file is placed under a file in the project under the uploader value corresponding to the URL, which maps to a Springmvc method, which is the core of the file upload, is responsible for writing the file to the location of the site           Side to go. 6) Spring Background Code implementation @RequestMapping (value = "/apkupload", method=requestmethod.post) public @ResponseBody S Tring apkupload (@RequestParam multipartfile file, model, HttpServletRequest          Request) throws IOException {InputStream input = null;          OutputStream output = null;          String root = "H:/file";          Generated file name String filename = File.getoriginalfilename ();          File to upload the location of 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));          Save file Ioutils.copy (input, output);          } catch (Throwable e) {throw e;              }finally{ioutils.closequietly (input);          ioutils.closequietly (output);      } return root+ "/" +filename;       where filename corresponds to data in step 5 of Onuploadsuccess -      <BeanID= "Multipartresolver"class= "Org.springframework.web.multipart.support.StandardServletMultipartResolver">      </Bean>  </Beans>  

This article links http://tec.5lulu.com/code/k4n1g5v1ra8i5f.html

Spring MVC configuration file Dispatcher-servlet.xml file details

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.