Spring MVC Introductory Tutorial (ii)

Source: Internet
Author: User

The first Article Macro-describes the spring MVC concept and shares a quick-start example (article link).


This article is mainly about spring MVC's configuration files.


first of all, let's talk about Web. xml: information that is automatically loaded into memory at the start of a website, such as server configuration parameters,<listener> Listener,<filter> Filter,<servlet> and so on. As another example, if you use the Spring framework in your project, you must define Contextloaderlistener, and when you start the Web container, the spring is automatically assembled Configuration information for the applicationcontext.xml. A typical web. xml file is posted here, and the comments are written in detail:

<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> <!--here is the designation Spring configuration file: contextconfiglocation-<context-param> <param-name>contextconfiglocation</ Param-name><param-value>/web-inf/configs/spring/applicationcontext*.xml</param-value> </ Context-param> <!--start the class file required by the spring container--<listener><listener-class> Org.springframework.web.context.contextloaderlistener</listener-class> </listener> <!-- Dispatcherservlet, core of Spring MVC--<servlet><servlet-name>mvc-dispatcher</servlet-name>< Servlet-class> org.springframework.web.servlet.dispatcherservlet</servlet-class><!--Dispatcherservlet The corresponding context configuration, default to/web-inf/$servlet-name$-servlet.xml--><init-param> <param-name>cont extconfiglocation</param-name> <!--Specify the location of the spring MVC configuration file, which can be unspecified, and that is the default setting, which says--&LT;PA Ram-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 intercept all requests--><url-pattern>/</ Url-pattern></servlet-mapping></web-app></span>



again, the Spring MVC configuration file mvc-dispatcher-servlet.xml: when the Web project starts, Spring MVC is configured when the appropriate servlet is started. A typical spring MVC configuration file is as follows:

<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 is used to provide its associated spring MVC configuration--><!--enable spring annotation-based di--><context:annotation-config/ ><!--Dispatcherservlet context, managing only @controller-type beans, ignoring other types of beans, such as @service--><context:component-scan Base-package= "Com.xidian.mvcdemo" >&Lt;context:include-filter type= "Annotation" expression= "Org.springframework.stereotype.Controller"/></ context:component-scan><!--handlermapping, without configuration, Spring MVC can be started by default. The defaultannotationhandlermapping annotation-driven handlermapping--><!--expanded the annotation function to bind the request parameters to the controller parameters. For example, can be added to a method in the class, further specify the URL, greatly improve the development efficiency--><mvc:annotation-driven/><!--static resource processing, CSS, JS, IMGs, ETC., Map the specific resource path for the Web project--><mvc:resources mapping= "/resources/**" location= "/resources/"/><!--configuration Viewresolver, The view resolver can also have multiple, as long as the corresponding order is attached, the actual runtime will load--><beanclass= " 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 provided in order to implement the file's upper and lower--><!--200*1024*1024 that is 200M The Resolvelazily property is enabled to defer file parsing in order to capture file size exceptions--><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>



Finally, the Spring container configuration file applicationcontext.xml: There is not much involved, specific configuration details can be seen in the spring Introductory tutorial (a) article link, In the later MyBatis series, a simple demo,spring MVC starter Series is three articles, and an article specifically notes some of the tags that are commonly used in development and some of the Spring MVC development ideas.

<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 htt P://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd "> <!- -Enable spring annotation-based di--><context:annotation-config/><context:component-scan base-package= " Com.xidian.mvcdemo "> <!--hint Spring does not need to manage Com.xidian.mvc.demo object under Controller property adornment--<context: Exclude-filter type= "Annotation" expression= "Org.springframework.stereotype.Controller"/> </context:component-scan></beans></span >





Did you see it? Welcome to discuss Http://blog.csdn.net/code_7




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Spring MVC Introductory Tutorial (ii)

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.