SPRINGMVC Common Configuration Instructions

Source: Internet
Author: User

SPRINGMVC configuration uses a lot of articles, but the specific description is not much, here the main introduction of the use of common configuration items, and its parsing class, SPRINGMVC processing content in two ways, one is converter, the other is Viewresolver, Both can handle json,xml and form content formats.


<?xml version= "1.0" encoding= "UTF-8" standalone= "no"? ><beans xmlns= "Http://www.springframework.org/schema /beans "xmlns:context=" Http://www.springframework.org/schema/context "xmlns:aop=" http://www.springframework.org/ Schema/aop "xmlns:mvc=" Http://www.springframework.org/schema/mvc "xmlns:p=" Http://www.springframework.org/schema /P "xmlns:xsi=" Http://www.w3.org/2001/XMLSchema-instance "xmlns:util=" Http://www.springframework.org/schema/util "Xmlns:c=" http://www.springframework.org/schema/c "xsi:schemalocation=" http://www.springframework.org/schema/ Beans Http://www.springframework.org/schema/beans/spring-beans-3.2.xsd Http://www.springframework.org/schema/con Text 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 Http://www.springframework.org/schema/util http: Www.springframework.org/schema/util/spring-util.xsd         HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/spring-aop-3.2.xsd ">< !--if the controller to use the configuration, you need to load the configuration, because this configuration is usually loaded by Dispatchservlet, and the spring listener class is not in a context, want to know why see http://blog.csdn.net/ strivezxq/article/details/43795081 This article provides a detailed explanation of the spring initialization process--<context:property-placeholder location= " Classpath:app.properties "/><!--Scans the classpath for annotated components @Component, @Repository, @Service, and @Controller by use-default-filters= "false", you can set only which annotations are scanned, and the general SPRINGMVC configuration only loads the following two types of annotations--><context:component-scan Base-package= "Your.base.package" use-default-filters= "false" ><context:include-filter type= "annotation" expression= "Org.springframework.stereotype.Controller"/><context:include-filter type= "annotation" expression= "Org.springframework.web.bind.annotation.ControllerAdvice"/></context:component-scan><! --<context:component-scan Annotation-config = "true" > already contains CONTEXT:ANNOTATION-CONFIGR functions, so this configuration is basically not necessary to configure, activates various notes detected in the Bean class: Spring's @Required and @Autowired, as well as JSR @PostConstruct, @PreDestroy and @Resource (if available), Jax-ws ' s @WebServiceRef (if available), EJB3 ' s @EJB (if available), and JPA ' s @PersistenceContext The and @PersistenceUnit (if available)--><context:annotation-config/>< defines a!--in the spring MVC context Org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler, it would be like an inspector, The URL to enter the Dispatcherservlet is screened, and if a request for a static resource is found, the request is forwarded to the Web application server's default servlet processing, which is processed by Dispatcherservlet if it is not a request for a static resource. General Web application Server The default servlet name is "default", so Defaultservlethttprequesthandler can find it. If your default servlet name for all Web application servers is not "default", you need to display the specified through the Default-servlet-name property: <mvc:default-servlet-handler Default-servlet-name= "The Web server used by default uses the servlet name"/>tomcat, Jetty, JBoss, and GlassFish default, Eg:web.xml 1.     <servlet-mapping> 2.     <servlet-name>default</servlet-name> 3. <url-pattern>*.jpg</url-pattern> 4. </servlet-mappIng> 5.     <servlet-mapping> 6.     <servlet-name>default</servlet-name> 7. <url-pattern>*.js</url-pattern> 8. </servlet-mapping> 9.     <servlet-mapping> 10.     <servlet-name>default</servlet-name> 11. <url-pattern>*.css</url-pattern> 12. </servlet-mapping> If you do not configure springdefault-servlet-name default settings, you have supported common Web server--&GT;&LT;MVC: Default-servlet-handler/><!--allows static resources to be placed anywhere, Processing class Org.springframework.web.servlet.resource.resourcehttprequesthandler<bean id= "ResourceHttpRequestHandler" class= "Org.springframework.web.servlet.resource.ResourceHttpRequestHandler" > <property name= "Locations" Value= "classpath:/meta-inf/resources/" ></property> </bean><bean class= " Org.springframework.web.servlet.handler.SimpleUrlHandlerMapping "> <property name=" Mappings "> <props&            Gt <prop key= "/resources/**" >resourcehttPrequesthandler</prop> </props> </property></bean> The following label implements--><mvc:resources mapping= "/resources/**" location= "/resources/" ></mvc:resources><!--Register "global" interceptor beans to apply To all registered handlermappings. Each inteceptor must implement the Org.springframework.web.servlet.HandlerInterceptor Ororg.springframework.web.context.request.WebRequestInterceptor interface--><mvc:interceptors> &LT;MVC: interceptor> <mvc:mapping path= "/**"/> <mvc:exclude-mapping path= "/css/**"/> <mvc : exclude-mapping path= "/js/**"/> <mvc:exclude-mapping path= "/images/**"/> <bean class= "com.fpx . Common.auth.mgt.framework.interceptor.ContextInterceptor "/> </mvc:interceptor></mvc:interceptors ><!--Turns on support for mapping requests to Spring MVC @Controller methodsalso Registers default formatters and V Alidators for use across all @Controllers configuration resolutionClass: Org.springframework.web.servlet.config.AnnotationDrivenBeanDefinitionParser configuration Content-negotiation-anager You can set the content type parameter in the URL , you can set the default content type <bean id= "Contentnegotiationmanagerfactorybean" class= " Org.springframework.web.accept.ContentNegotiationManagerFactoryBean "p:favorpathextension=" false "P: Favorparameter= "true" p:parametername= "format" p:ignoreacceptheader= "true" p:defaultcontenttype= "Application/json "> <property name=" mediatypes "> <props> <prop key=" JSON ">application/json</prop> &L T;prop key= "xml" >application/xml</prop> </props> &LT;/PROPERTY&GT;&LT;/BEAN&GT;--&GT;&LT;MVC: Annotation-driven content-negotiation-anager= "Contentnegotiationmanagerfactorybean" > <mvc: message-converters> <ref bean= "Stringhttpmessageconverter"/> <ref bean= "JsonHttpMessageConverte R "/> <ref bean=" Marshallinghttpmessageconverter "/> </mvc:message-converters></mvc:annotation -driven><!--ContentManage Factory--<bean class= "Org.springframework.web.accept.ContentNegotiationManagerFactoryBean" P:favorpathextension= "false" p:favorparameter= "true" p:parametername= "format" p:ignoreaccepth                    Eader= "true" p:defaultcontenttype= "Application/json" > <property name= "MediaTypes" >                        <props> <prop key= "JSON" >application/json</prop> <prop key= "xml" >application/xml</prop> </props> </propert y> </bean><!--Content parser, you can configure the return parameter type p:parametername= "format", configure the default request content type by P:defaultcontenttype, c:qual Ityvalue= "0.5" can set the priority of the content type, if Mvc:annotation-driven and annotations (@RequestBody) are used, the configuration below is not effective--<bean class= " Org.springframework.web.servlet.view.ContentNegotiatingViewResolver "> <property name=" Contentnegotiationma Nager "ref=" ContentnegotiationmaNagerfactorybean "> </property> <property name=" Defaultviews "> <list> <bean class= "org.springframework.web.servlet.view.json.Mapping                         Jackson2jsonview "> <property name=" Modelkey "value=" Resultvo "/>                    <property name= "Extractvaluefromsinglekeymodel" value= "true"/> </bean> <bean class= "Org.springframework.web.servlet.view.xml.MarshallingView" > <constructor-arg                    ref= "Jaxb2marshaller"/> <property name= "ContentType" value= "Application/xml"/> </bean> </list> </property> <!--<property Name= "Ignore  AcceptHeader "value=" true "/>-</bean> <!--XML view using a JAXB marshaller--<bean Id= "Jaxb2marshaller" class= "Org.springframework.oxm.jaxb.Jaxb2Marshaller" > <property name= "marshallerproperties" > <map> <entry key= "Jaxb.formatted.output" > <value type= "boolean" >true</value> </entry> <entry key= "jaxb.encoding" value= "UTF-8"/&               Gt                    </map> </property> <property name= "Packagestoscan" > <list> <value>com.api.domain</value> <value>com.api.web.controller.vo</value&               Gt </list> </property> </bean> <bean id= "Jstlviewresolver" class= "ORG.SPRINGFR Amework.web.servlet.view.InternalResourceViewResolver "> <property name=" Order "value=" 2 "/> &lt          ;p roperty name= "Viewclass" value= "Org.springframework.web.servlet.view.JstlView"/><property name= "prefix" value= "/views/"/> <property name= "suffix" value= "/> <property Name= "Requestcontextattribute" value= "RC"/> </bean><!--c:qualityvalue= "0.5" can set the priority of the content type, The default is 1.0, the higher the priority, and the <bean id= "Stringhttpmessageconverter" class= "Org.springframework.http.converter.Stri                    Nghttpmessageconverter "> <property name=" supportedmediatypes "> <list> <value>text/plain;charset=UTF-8</value> <value>text/html;charset=utf-8</value          > </list> </property> </bean> <bean id= "Jsonhttpmessageconverter" class= "Org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/> <bean id= "Marshal          Linghttpmessageconverter "class=" Org.springframework.http.converter.xml.MarshallingHttpMessageConverter "> <constructor-arg ref= "Jaxb2marsHaller "/> <!--<property name=" supportedmediatypes "value=" Application/xml "></property>- <property name= "Supportedmediatypes" > <util:list> <bean class= "O Rg.springframework.http.MediaType "c:type=" Application "c:subtype=" xml "c:qualityvalue=" 0.5 "/> </util: List> </property> </bean>


SPRINGMVC Common Configuration Instructions

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.