百度查看了各類文檔要麼是springmvc版本較舊,要麼是文章處處抄襲沒一篇正確完整的,最怕那種轉載還轉漏了的博文誤人子弟。html
先說下個人jar版本主要是spring-webmvc的版本spring-webmvc-4.0.9.RELEASE.jar.java
<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>
<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>
<?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配置web
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
視圖文件在class的webpage文件夾中:spring
瀏覽器測試三種視圖都經過對應視圖編譯器解析了並響應瀏覽器apache
首先了解一下:json
1 .InternalResourceViewResolver 、FreeMarkerViewResolver、VelocityViewResolver都繼承了UrlBasedViewResolver這個類屬於拼接url路徑查找對應視圖文件。瀏覽器
2 .視圖解析器查找對應路徑視圖時沒找到返回null,這樣纔會交由下一個(低優先權的解析器來解析)spring-mvc
值得注意的地方:mvc
一、因爲InternalResourceViewResolver比較特殊,對應路徑視圖文件不存在時不會返回null,就沒法交由其餘解析器來解析,因此配置多視圖InternalResourceViewResolver優先權就放最低。app