Spring常用配置及解析类说明
程序员文章站
2023-12-14 23:09:58
springmvc配置用法的文章很多,但具体描述清楚的不多,这里主要介绍下常用的配置项的用法,以及它的解析类,springmvc处理内容有两种方式,一种是converter...
springmvc配置用法的文章很多,但具体描述清楚的不多,这里主要介绍下常用的配置项的用法,以及它的解析类,springmvc处理内容有两种方式,一种是converter,另一种是viewresolver,两种都能处理json,xml以及form内容格式。
<?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"> <!-- 如果controller里要用到配置,才需要加载配置,因为一般这个配置由dispatchservlet来加载,和spring监听类不在一个上下文里,想要知道原因请看 http://blog.csdn.net/strivezxq/article/details/43795081 这篇文章详细解析了spring初始化过程 --> <context:property-placeholder location="classpath:app.properties" /> <!--scans the classpath for annotated components @component, @repository, @service, and @controller 通过use-default-filters="false",可以设置只扫描哪些注释,一般springmvc配置只加载下面两种注释 --> <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">已经包含了context:annotation-configr的功能,所以这个配置基本没必要配置,激活在bean类中被检测到的各种注释: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 /> <!--会在spring mvc上下文中定义一个 org.springframework.web.servlet.resource.defaultservlethttprequesthandler, 它会像一个检查员,对进入dispatcherservlet的url进行筛查,如果发现是静态资源的请求,就将该请求转由web应用服务器默认的 servlet处理,如果不是静态资源的请求,才由dispatcherservlet继续处理。 一般web应用服务器默认的servlet名称是"default",因此defaultservlethttprequesthandler可以 找到它。如果你所有的web应用服务器的默认servlet名称不是"default",则需要通过default-servlet-name属性显示指 定: <mvc:default-servlet-handler default-servlet-name="所使用的web服务器默认使用的servlet名称" /> 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> 如果不配置springdefault-servlet-name 默认会设置,已经支持常用的web服务器 --> <mvc:default-servlet-handler /> <!-- 允许静态资源放在任何地方 ,处理类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> 下面标签实现 --> <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 配置解析类:org.springframework.web.servlet.config.annotationdrivenbeandefinitionparser 配置content-negotiation-anager可以在url里设置内容类型参数,可以设置默认内容类型 <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> <!-- 内容管理工厂 --> <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> <!-- 内容解析器 ,可以p:parametername="format"来配置返回参数类型 ,通过p:defaultcontenttype配置默认请求内容类型, c:qualityvalue="0.5" 可以设置内容类型的优先级, 如果使用了mvc:annotation-driven 和注解方式(@requestbody), 下面配置是不生效的--> <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" 可以设置内容类型的优先级,默认是1.0,越大优先级越高 --> <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返回json配置步骤如下:
1、添加jackson.jar包
2、在applicationcontext.xml配制文件中添加如下代码
<!--解析返回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、在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; }
返回值可以为list也可以为map类型
总结
以上就是本文关于spring常用配置及解析类说明的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站:
如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!
推荐阅读
-
Spring常用配置及解析类说明
-
Spring boot工具类静态属性注入及多环境配置详解
-
spring5 源码深度解析----- 被面试官给虐懵了,竟然是因为我不懂@Configuration配置类及@Bean的原理
-
Spring boot工具类静态属性注入及多环境配置详解
-
Spring AOP AspectJ使用及配置过程解析
-
Spring5源码解析6-ConfigurationClassParser 解析配置类
-
Spring Cloud Eureka 常用配置及说明
-
Spring interceptor拦截器配置及用法解析
-
iOS所有常用证书,appID,Provisioning Profiles配置说明及制作教程
-
简述pm2常用命令集合及配置文件说明