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"> <property name="locations" value="classpath:/META-INF/resources/"></property> </bean> <bean> <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 /> </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" 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 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> <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" 可以設置內容類型的優先級,默認是1.0,越大優先級越高--> <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返回json配置步驟如下:
1、添加jackson.jar包
2、在applicationContext.xml配製文件中添加如下代碼
<!--解析返回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、在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中利用配置文件和@value注入屬性值代碼詳解
spring配置掃描多個包問題解析
Spring配置使用之Bean生命週期詳解
如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!