Spring commonly used to configure and parse class descriptions
- 2020-11-26 18:46:58
- OfStack
There are many articles on the usage of springMVC configuration, but the details are not very clear. This article mainly introduces the usage of common configuration items and their parsing classes. springMVC can handle content in two ways, one is converter, the other is ViewResolver, both of which 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/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 controller I'm going to use the configuration, so I'm going to load the configuration because 1 Generally this configuration is provided by DispatchServlet To load, and spring The monitor class is not in 1 In context, see why
http://blog.csdn.net/strivezxq/article/details/43795081 This article explains it in detail spring Initialization process -->
<context:property-placeholder location="classpath:app.properties" />
<!--Scans the classpath for annotated components @Component, @Repository, @Service, and @Controller
through use-default-filters="false", You can set which comments to scan only, 1 a springMVC The 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.stereotype.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 context:annotation-configr The function of, so this configuration is basically not necessary to configure, activation in bean Various comments detected in the 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 />
<!-- Will be in Spring MVC Definition in context 1 a org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler . It will be like 1 An inspector, yes DispatcherServlet the URL Do the screening, and if it turns out to be a static resource request, redirect the request Web Application server by default Servlet Processing, if not static resource request, only by DispatcherServlet Let's move on.
1 a Web Application server by default Servlet The name is "default" , so DefaultServletHttpRequestHandler can Find it. If you have all Web The default for the application server Servlet The name is not "default" , you need to pass default-servlet-name Attribute indication Fixed:
<mvc:default-servlet-handler default-servlet-name=" The use of Web The server is used by default Servlet The name of the " />
Tomcat, Jetty, JBoss, and GlassFish Default name is default, eg: web.xml In the
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 not configured springdefault-servlet-name Default will be set, already support commonly used web The server
-->
<mvc:default-servlet-handler />
<!-- Allows static resources to be placed anywhere To deal with 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>
<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 class="com.fpx.common.auth.mgt.framework.interceptor.ContextInterceptor" />
</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
Configure the resolution class: org.springframework.web.servlet.config.AnnotationDrivenBeanDefinitionParser
configuration content-negotiation-anager Can be found in url Set the content type parameter , 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>
<prop key="xml">application/xml</prop>
</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
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>
<prop key="xml">application/xml</prop>
</props>
</property>
</bean>
<!-- Content parser , you can p:parameterName="format" To configure the return parameter type Through the p:defaultContentType Configure the default request content type,
c:qualityValue="0.5" You can set the priority of the content type, If you use mvc:annotation-driven And annotation mode ( @RequestBody ) , The following configuration is not valid -->
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="contentNegotiationManager" ref= "contentNegotiationManagerFactoryBean">
</property>
<property name="defaultViews">
<list>
<bean
class="org.springframework.web.servlet.view.json.MappingJackson2JsonView">
<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="ignoreAcceptHeader" 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" />
</map>
</property>
<property name="packagesToScan">
<list>
<value>com.api.domain</value>
<value>com.api.web.controller.vo</value>
</list>
</property>
</bean>
<bean id="jstlViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<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 the content type , The default is 1.0 The higher the priority, the higher the priority -->
<bean id="stringHttpMessageConverter"
class="org.springframework.http.converter.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"
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" />
<bean id="marshallingHttpMessageConverter"
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="org.springframework.http.MediaType" c:type="application" c:subtype="xml" c:qualityValue="0.5"/>
</util:list>
</property>
</bean>
SpringMVC returns the json configuration steps as follows:
1. Add jackson. jar package
2. Add the following code to the ES19en. xml configuration file
<!-- Parsing return JSON -->
<!-- <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list >
<ref bean="mappingJacksonHttpMessageConverter" />
</list>
</property>
</bean>
<bean id="mappingJacksonHttpMessageConverter"
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>
3. Add the following code to 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 of type list or Map
conclusion
That's the end of this article on common configurations and parsing classes for Spring, and I hope it helps. Those who are interested can continue to see this site:
Spring USES configuration files and @value to inject attribute value code details
spring configuration scan multiple package problem resolution
Details of the Bean lifecycle used for Spring configuration
If there is any deficiency, please let me know. Thank you for your support!