接上一篇文章,今天在弄注册用户功能的时候,想要使用springMVC的内容协商org.springframework.web.servlet.view.ContentNegotiatingViewResolver,
遇到两个小问题,这里写一写用于记忆。
1:在访问返回modelandview的时候也返回了json,
原因:<property name="defaultContentType" value="application/json" />这里的value配置错误
解决方法:把value改为<property name="defaultContentType" value="text/html" />即可
2:无法解析Velocity的 #springUrl了,
原因:
上图中圈住的地方用的不是针对Velocity的解析器。
解决方法:把解析器改为针对Velocity的解析器即可,如下图:
这样既可以用.json的后缀名的请求返回json,又可以用Velocity的 #springUrl啦,完整的配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:websocket="http://www.springframework.org/schema/websocket"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/websocket
http://www.springframework.org/schema/websocket/spring-websocket-4.0.xsd"
default-lazy-init="true">
<!-- 默认的注解映射的支持 -->
<mvc:annotation-driven />
<mvc:resources location="/images/**" mapping="/images/**" />
<mvc:resources location="/css/**" mapping="/css/**" />
<mvc:resources location="/pages/**" mapping="/pages/**" />
<!-- 配置跳转默认首页,访问http://127.0.0.2:8080/xiaoma,会解析为 /WEB-INF/pages/index.html,不经过controller-->
<mvc:view-controller path="/" view-name="login" />
<!-- 自动扫描该包,使SpringMVC认为包下用了@controller注解的类是控制器 -->
<context:component-scan base-package="com.ccms.mazhili.*.controller"></context:component-scan>
<!-- 扫描service、dao组件 -->
<context:component-scan base-package="com.ccms.mazhili.*.dao, com.ccms.mazhili.*.service"></context:component-scan>
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="applicationResources" />
<property name="useCodeAsDefaultMessage" value="true" />
</bean>
<!--
下面两个Bean是配置Velocity,方便在页面中用#springUrl()进行引入css、js文件,
需要的jar包为:spring-context-support, velocity-tools, velocity三个jar包,
VelocityViewResolver与VelocityLayoutViewResolver区别为:如果不需要使用Velocity的布局功能,就用VelocityViewResolver,
注意:当使用VelocityLayoutViewResolver时,layoutUrl的路径是相对于resourceLoaderPath的路径,而不是webapp的路径
-->
<bean id="velocityConfig" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
<property name="resourceLoaderPath" value="/WEB-INF/pages/"></property>
<property name="configLocation" value="classpath:velocity.properties"></property>
</bean>
<!-- SpringMVC内容协商解析多视图 -->
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<!-- 设置为true以忽略对Accept Header的支持 -->
<property name="ignoreAcceptHeader" value="true" />
<!-- 在没有扩展名时即: "/user/toRegisterUser" 时的默认展现形式 -->
<property name="defaultContentType" value="text/html" />
<!-- 扩展名至mimeType的映射,即 /user.json => application/json -->
<property name="mediaTypes">
<map>
<entry key="html" value="text/html" />
<entry key="json" value="application/json" />
</map>
</property>
<property name="viewResolvers">
<list>
<!-- 使用针对Velocity的视图解析器 -->
<bean class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
<property name="suffix" value=".html" />
<property name="contentType" value="text/html;charset=utf-8" />
<property name="exposeSpringMacroHelpers" value="true" /><!--是否使用spring对宏定义的支持 -->
<property name="exposeRequestAttributes" value="true" /><!--是否开放request属性 -->
<property name="requestContextAttribute" value="ctx" /><!--request属性引用名称 -->
</bean>
</list>
</property>
<property name="defaultViews">
<list>
<bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView" />
</list>
</property>
</bean>
<!-- 配置文件上传,如果没有使用文件上传可以不用配置,当然如果不配,那么配置文件中也不必引入上传组件包 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="UTF-8"></property>
<property name="maxUploadSize" value="10485760000"></property>
<!-- 内存中的最大值 -->
<property name="maxInMemorySize" value="40960"></property>
</bean>
</beans>
以上内容纯属我个人理解,若有不正之处,敬请指教!