我是靠谱客的博主 高挑烧鹅,最近开发中收集的这篇文章主要介绍springMvc配置多视图,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

    百度查看了各种文档要么是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

最后

以上就是高挑烧鹅为你收集整理的springMvc配置多视图的全部内容,希望文章能够帮你解决springMvc配置多视图所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(54)

评论列表共有 0 条评论

立即
投稿
返回
顶部