springMvc配置多视图

    百度查看了各种文档要么是springmvc版本较旧,要么是文章到处抄袭没一篇正确完整的,最怕那种转载还转漏了的博文误人子弟。

先说下我的jar版本主要是spring-webmvc的版本spring-webmvc-4.0.9.RELEASE.jar.

一、springmvc的servlet配置(为了代码完整好理解这个也贴出来吧)
<servlet>
  <servlet-name>springMvc</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
    <description>spring mvc 配置文件</description>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:spring-mvc.xml</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
  <servlet-name>springMvc</servlet-name>
  <url-pattern>/</url-pattern>
</servlet-mapping>
二、spring-mvc.xml中配置
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
       <property name="mediaTypes">
           <map>
               <entry key="atom" value="application/atom+xml"/>
               <entry key="html" value="text/html"/>
               <entry key="json" value="application/json"/>
           </map>
       </property>
       <property name="viewResolvers">
           <list>
			<!-- velocity视图解析器 -->
		    <bean id="velocityViewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
		        <property name="order" value="1" />
		        <property name="contentType" value="text/html;charset=UTF-8" />
		        <property name="cache" value="true"/>
		        <property name="prefix" value="/webpage/"/>
		        <property name="suffix" value=".vm"/>
		        <property name="requestContextAttribute" value="rc" />
		        <property name="toolboxConfigLocation" value="/WEB-INF/classes/velocityToolBox.xml" />
		    </bean>
		    <!-- FreeMarker视图解析 -->
		    <bean id="freeMarkerViewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
		     	<property name="order" value="2" />
		        <property name="contentType" value="text/html;charset=UTF-8" />
		        <property name="requestContextAttribute" value="rc" />  
		        <property name="suffix" value=".ftl"/>
		    </bean>
		    <!-- jsp视图解析器 -->
               <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
                   <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
                   <property name="contentType" value="text/html;charset=UTF-8" />
                   <property name="prefix" value="/WEB-INF/classes/webpage/" />
                   <property name="suffix" value=".jsp" />
                   <property name="order" value="3" />
               </bean>
           </list>
       </property>
       <property name="defaultViews">
           <list>
               <bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView" />
           </list>
       </property>
</bean>

<!-- FreeMarker环境配置 -->
<bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
    <property name="templateLoaderPath" value="/WEB-INF/classes/webpage/" />
    <property name="defaultEncoding" value="UTF-8" />
</bean>
<!-- velocity环境配置 -->
<bean id="velocityConfig" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
    <property name="configLocation" value="/WEB-INF/classes/velocity.properties" />
</bean>
三、velocity的velocityToolBox.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<toolbox>
	<tool>
		<key>date</key>
		<scope>application</scope>
		<class>org.apache.velocity.tools.generic.DateTool</class>
	</tool>
</toolbox>

        velocity的velocity.properties配置

runtime.log.logsystem.class = org.apache.velocity.runtime.log.Log4JLogChute

runtime.log.invalid.references = true

input.encoding=UTF-8
output.encoding=UTF-8

directive.foreach.counter.name = velocityCount
directive.foreach.counter.initial.value = 1
directive.foreach.maxloops = -1

directive.foreach.iterator.name = velocityHasNext

directive.set.null.allowed = false

directive.if.tostring.nullcheck = true

directive.include.output.errormsg.start = <!-- include error :
directive.include.output.errormsg.end   =  see error log -->

directive.parse.max.depth = 10

resource.loader = class

class.resource.loader.description = Velocity File Resource Loader
class.resource.loader.class = org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
class.resource.loader.path = .
class.resource.loader.cache = false
class.resource.loader.modificationCheckInterval = 2

file.resource.loader.description = Velocity File Resource Loader
file.resource.loader.class = org.apache.velocity.runtime.resource.loader.FileResourceLoader
file.resource.loader.path = .
file.resource.loader.cache = false
file.resource.loader.modificationCheckInterval = 2

velocimacro.permissions.allow.inline = true
velocimacro.permissions.allow.inline.to.replace.global = false
velocimacro.permissions.allow.inline.local.scope = false

velocimacro.context.localscope = false
velocimacro.max.depth = 20

velocimacro.arguments.strict = false

runtime.references.strict = false

runtime.interpolate.string.literals = true


resource.manager.class = org.apache.velocity.runtime.resource.ResourceManagerImpl
resource.manager.cache.class = org.apache.velocity.runtime.resource.ResourceCacheImpl

parser.pool.class = org.apache.velocity.runtime.ParserPoolImpl
parser.pool.size = 20

directive.evaluate.context.class = org.apache.velocity.VelocityContext

runtime.introspector.uberspect = org.apache.velocity.util.introspection.UberspectImpl

introspector.restrict.packages = java.lang.reflect

introspector.restrict.classes = java.lang.Class
introspector.restrict.classes = java.lang.ClassLoader
                
introspector.restrict.classes = java.lang.Compiler
introspector.restrict.classes = java.lang.InheritableThreadLocal
introspector.restrict.classes = java.lang.Package
introspector.restrict.classes = java.lang.Process
introspector.restrict.classes = java.lang.Runtime
introspector.restrict.classes = java.lang.RuntimePermission
introspector.restrict.classes = java.lang.SecurityManager
introspector.restrict.classes = java.lang.System
introspector.restrict.classes = java.lang.Thread
introspector.restrict.classes = java.lang.ThreadGroup
introspector.restrict.classes = java.lang.ThreadLocal

userdirective=org.jeecgframework.p3.core.directive.AuthFilterTagDirective,org.jeecgframework.p3.core.directive.AuthOperateTagDirective,com.jeecg.core.biz.tag.select.dict.tag.SelectDictTag,com.jeecg.core.biz.tag.select.table.tag.SelectTableTag
四、敲完了测试

180223_xXWv_1377077.png

视图文件在class的webpage文件夹中:

180238_KP8i_1377077.png

浏览器测试三种视图都通过对应视图编译器解析了并响应浏览器

180249_MvdW_1377077.png

首先了解一下:

1 .InternalResourceViewResolver 、FreeMarkerViewResolver、VelocityViewResolver都继承了UrlBasedViewResolver这个类属于拼接url路径查找对应视图文件。

2 .视图解析器查找对应路径视图时没找到返回null,这样才会交由下一个(低优先权的解析器来解析)

值得注意的地方:

1、由于InternalResourceViewResolver比较特殊,对应路径视图文件不存在时不会返回null,就无法交由其他解析器来解析,所以配置多视图InternalResourceViewResolver优先权就放最低。

 

 

转载于:https://my.oschina.net/u/1377077/blog/1031484

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值