服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - Spring常用配置及解析类说明

Spring常用配置及解析类说明

2021-02-21 11:57strivezxq Java教程

这篇文章主要介绍了Spring常用配置及解析类说明,涉及Spring常用配置项的方法以及它的解析类等相关内容,具有一定参考价值,需要的朋友可以了解下。

springMVC配置用法的文章很多,但具体描述清楚的不多,这里主要介绍下常用的配置项的用法,以及它的解析类,springMVC处理内容有两种方式,一种是converter,另一种是ViewResolver,两种都能处理json,xml以及form内容格式。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<?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配制文件中添加如下代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<!--解析返回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中添加如下代码

?
1
2
3
4
5
6
@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常用配置及解析类说明的全部内容,希望对大家有所帮助。如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

原文链接:http://blog.csdn.net/strivezxq/article/details/45457067

延伸 · 阅读

精彩推荐