There are many articles on the usage of springMVC configuration, but not many are clearly described. Here we mainly introduce the usage of commonly used configuration items and its parsing classes. There are two ways to process content in springMVC, one is converter, and 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/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 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 configuration is used in the controller, the configuration needs to be loaded, because generally this configuration is loaded by the DispatchServlet and is not in the same context as the spring listening class. If you want to know the reason, please see http://blog.csdn.net/strivezxq/article/details/43795081 This article analyzes the spring initialization process in detail-> <context:property-placeholder location="classpath:app.properties" /> <!--Scans the classpath for annotated components @Component, @Repository, @Service, and @Controller Use-default-filters="false", you can set which comments are only scanned. Generally, springMVC configuration only loads the following two comments-> <context:component-scan base-package="your.base.package" use-default-filters="false"> <context:include-filter type="annotation" expression="org.springframework.steretype.Controller"/> <context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/> </context:component-scan> <!-- <context:component-scan annotation-config = "true">It already contains the function of context:annotation-configr, so there is basically no need to configure this configuration, activate various annotations detected in the bean class: Spring's @Required and @Autowired, as well as JSR 250's @PostConstruct, @PreDestroy and @Resource (if available), JAX-WS's @WebServiceRef (if available), EJB3's @EJB (if available), and JPA's @PersistenceContext and @PersistenceUnit (if available) --> <context:annotation-config /> <!--It will define an org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler in the Spring MVC context. It will screen the URL entering the DispatcherServlet like an inspector. If it is found that it is a static resource, the request will be transferred to the default servlet of the web application server. If it is not a static resource request, the DispatcherServlet will continue to process. The default Servlet name of a web application server is "default", so DefaultServletHttpRequestHandler can find it. If the default servlet name of all your web application servers is not "default", you need to display the specified via the default-servlet-name attribute: <mvc:default-servlet-handler default-servlet-name="The default servlet name used by the web server" /> Tomcat, Jetty, JBoss, and GlassFish default name is 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 springdefault-servlet-name is not configured, the default setting will be supported, and the commonly used web servers are already supported--> <mvc:default-servlet-handler /> <!-- Allow static resources to be placed anywhere and handle the class org.springframework.web.servlet.resource.ResourceHttpRequestHandler <bean id="resourceHttpRequestHandler"> <property name="locations" value="classpath:/META-INF/resources/"></property> </bean> <bean> <property name="mappings"> <props> <prop key="/resources/**">resourceHttpRequestHandler</prop> </props> </property> </bean> The following tag implementation-> <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 or org.springframework.web.context.request.WebRequestInterceptor interface --> <mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/**" /> <mvc:exclude-mapping path="/css/**" /> <mvc:exclude-mapping path="/js/**" /> <mvc:exclude-mapping path="/images/**" /> <bean /> </mvc:interceptor> </mvc:interceptors> <!-- Turns on support for mapping requests to Spring MVC @Controller methods Also registers default Formatters and Validators for use across all @Controllers Configuration parsing class: org.springframework.web.servlet.config.AnnotationDrivenBeanDefinitionParser Configuration content-negotiation-anager can set content type parameters in the url, and the default content type can be set <bean id="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> <prop key="xml">application/xml</prop> </props> </props> </property> </bean> --> <mvc:annotation-driven content-negotiation-anager="contentNegotiationManagerFactoryBean"> <mvc:message-converters> <ref bean="stringHttpMessageConverter" /> <ref bean="jsonHttpMessageConverter" /> <ref bean="marshallingHttpMessageConverter" /> </mvc:message-converters> </mvc:annotation-driven> <!-- Content Management Factory --> <bean 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> <prop key="xml">application/xml</prop> </props> </property> </bean> <!-- Content parser, you can configure the return parameter type by p:parameterName="format" and configure the default requested content type through p:defaultContentType. c:qualityValue="0.5" can set the priority of the content type. If mvc:annotation-driven and annotation method (@RequestBody), the following configuration is not effective --> <bean> <property name="contentNegotiationManager" ref= "contentNegotiationManagerFactoryBean"> </property> <property name="defaultViews"> <list> <bean > <property name="modelKey" value="resultVo" /> <property name="extractValueFromSingleKeyModel" value="true" /> </bean> <bean> <constructor-arg ref="jaxb2Marshaller" /> <property name="contentType" value="application/xml" /> </bean> </list> </property> <!-- <property name="ignoreAcceptHeader" value="true" /> --> </bean> <!-- XML view using a JAXB marshaller --> <bean id="jaxb2Marshaller"> <property name="marshallerProperties"> <map> <entry key="jaxb.formatted.output"> <value type="boolean">true</value> </entry> <entry key="jaxb.encoding" value="UTF-8" /> </map> </property> <property name="packagesToScan"> <list> <value>com.api.domain</value> <value>com.api.web.controller.vo</value> </list> </property> </bean> <bean id="jstlViewResolver" > <property name="order" value="2" /> <property 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" You can set the priority of content types. The default is 1.0. The higher the priority, the higher the priority--> <bean id="stringHttpMessageConverter" > <property name="supportedMediaTypes"> <list> <value>text/plain;charset=UTF-8</value> <value>text/html;charset=UTF-8</value> </list> </property> </bean> <bean id="jsonHttpMessageConverter" /> <bean id="marshallingHttpMessageConverter" > <constructor-arg ref="jaxb2Marshaller" /> <!-- <property name="supportedMediaTypes" value="application/xml"></property> --> <property name="supportedMediaTypes"> <util:list> <bean c:type="application" c:subtype="xml" c:qualityValue="0.5"/> </util:list> </property> </bean>
SpringMVC returns json configuration steps as follows:
1. Add the jackson.jar package
2. Add the following code to the applicationContext.xml configuration file
<!--Passing returns JSON --><!-- <bean> --> <bean> <property name="messageConverters"> <list > <ref bean="mappingJacksonHttpMessageConverter" /> </list> </property> </bean> <bean id="mappingJacksonHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/html;charset=UTF-8</value> </list> </property> </bean>
3. Add the following code to the controller
@RequestMapping(value="/chinese/listTree", method = RequestMethod.POST) @ResponseBody public List getlistChinese(Model model){ List<User> list = (List<ChineseCategory>) commonMgr.find("from User"); return list; }The return value can be list or Map type
Summarize
The above is all the content of this article about the commonly used configurations and analysis classes of Spring. I hope it will be helpful to everyone. Interested friends can continue to refer to this site:
Detailed explanation of the code for injecting attribute value using configuration files and @value in Spring
Analysis of problem of scanning multiple packages in spring configuration
Detailed explanation of the life cycle of Spring configuration usage
If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!