Velocity配置详解(三)
2016-04-13 11:02
176 查看
一 springmvc中velocity配置详解
1.1 web.xml配置
<context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath*:common/common.xml </param-value> </context-param>注: common.xml可以随便取名,知道是velocity的配置就行。
1.2 common.xml (velocity.xml的配置)
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans default-autowire="byName"> <!-- 配置velocity引擎 --> <bean id="velocityConfigurer" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer"> <property name="resourceLoaderPath"> <value>/WEB-INF/templates</value> <!-- 模板存放的路径 --> </property> <property name="configLocation"> <value>classpath:common/velocity.properties</value> </property> <property name="velocityProperties"> <props> <prop key="directive.foreach.counter.name">loopCounter</prop> <prop key="directive.foreach.counter.initial.value">0</prop> <prop key="directive.foreach.iterator.name">loopHasNext</prop> </props> </property> </bean> <!--配置视图的显示:配置附加工具,以及将后缀为vm的文件交给下面的Resolver处理--> <bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver"> <property name="toolboxConfigLocation" value="/WEB-INF/toolbox.xml"/> <!--toolbox配置文件路径--> <property name="prefix" value="/"/> <!-- 视图文件的前缀,即存放的路径 --> <property name="suffix" value=".vm" /> <!-- 视图文件的后缀名 --> <property name="layoutUrl" value="layout/layout.vm" /> <!--指定layout文件--> <property name="contentType" value="text/html;charset=UTF-8" /> <!--指定编码--> <property name="exposeSpringMacroHelpers" value="true" /> <!--是否使用spring对宏定义的支持--> <property name="layoutKey" value="layout"></property> <property name="screenContentKey" value="screen_content" /> <property name="dateToolAttribute"> <value>dateTool</value> <!--日期函数名称--> </property> <property name="numberToolAttribute"> <value>numberTool</value> <!--数字函数名称--> </property> <property name="allowRequestOverride" value="true" /> <property name="exposeRequestAttributes" value="true" /> <!--是否开放request属性--> <property name="exposeSessionAttributes" value="true" /> <!--是否开放session属性--> <property name="requestContextAttribute" value="rc"/> <!--request属性引用名称--> </bean> </beans>
注: 配置layoutUrl设定系统默认的模板路径
layoutKey设定模板文件键值,设定该值后就可以在vm文件中使用该键值设置模板路径,
screenContentKey表示指定vm文件显示位置
1.3 velocity.properties配置
#模板编码: input.encoding=ISO-8859-1 //模板输入编码 output.encoding=ISO-8859-1 //模板输出编码 #foreach配置 directive.foreach.counter.name = velocityCount //计数器名称 directive.foreach.counter.initial.value = 1 //计数器初始值 directive.foreach.maxloops = -1 //最大循环次数,-1为默认不限制 directive.foreach.iterator.name = velocityHasNex //迭代器名称 #set配置 directive.set.null.allowed = false //是否可设置空值 #include配置 directive.include.output.errormsg.start = <!-- include error : //错误信息提示开始字符串 directive.include.output.errormsg.end = see error log --> //错误信息提示结束字符串 #parse配置 directive.parse.max.depth = 10 //解析深度 模板加载器配置 resource.loader = file //模板加载器类型,默认为文件,可定义多个 file.resource.loader.description = Velocity File Resource Loader //加载器描述 file.resource.loader.class = 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 //宏参数是否启用严格模式 资源管理器配置 resource.manager.class = Velocity.Runtime.Resource.ResourceManagerImpl //管理器类名称 resource.manager.cache.class = Velocity.Runtime.Resource.ResourceCacheImpl //缓存器类名称 解析器池配置 parser.pool.class = Velocity.Runtime.ParserPoolImpl //解析池类名称 parser.pool.size = 40 //初始大小 #evaluate配置 directive.evaluate.context.class = Velocity.VelocityContext //上下问类名称 可插入introspector配置 runtime.introspector.uberspect = Velocity.Util.Introspection.UberspectImpl //默认introspector类名称
如:我们项目中使用的(根据实际情况,配置几项即可)
tools.view.servlet.error.template = Error.vm input.encoding=UTF-8 output.encoding=UTF-8 runtime.log = velocity.log velocimacro.library=VM_global_library.vm runtime.log.invalid.references = true file.resource.loader.cache=false velocimacro.library.autoreload = true
1.4 toolbox.xml配置
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <toolbox> <tool> <key>date</key> <scope>request</scope> <class> org.apache.velocity.tools.generic.DateTool </class> <parameter name="format" value="yyyy-MM-dd HH:mm:ss"/> </tool> <tool> <key>link</key> <scope>request</scope> <class>org.apache.velocity.tools.view.tools.LinkTool</class> </tool> <tool> <key>stringUtils</key> <scope>request</scope> <class>org.apache.velocity.util.StringUtils</class> </tool> <tool> <key>math</key> <scope>application</scope> <class>org.apache.velocity.tools.generic.MathTool</class> </tool> <tool> <key>esc</key> <scope>request</scope> <class>org.apache.velocity.tools.generic.EscapeTool</class> </tool> <tool> <key>params</key> <scope>request</scope> <class>org.apache.velocity.tools.view.tools.ParameterParser</class> </tool> </toolbox>注:如果配置了toolbox.xml则在velocity模板中可直接使用java后台代码了。
1.5 layout.vm布局文件配置
路径:根据common.xml配置,此时我的位置为: /WEB-INF/templates/layout/layout.vm<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no" /> <title id="title1">$!page_title</title> #parse("/layout/commonCss.vm") <!-- 公用css --> #parse("/layout/commonJs.vm") <!-- 公用js --> </head> <body> $screen_content </body> </html>
1.6 default.vm布局文件配置
位置为: /WEB-INF/templates/layout/default.vm$screen_content
1.7 使用范例
1.7.1 普通页面
#set($page_title="普通页面") <div> <p>显示页面内容,好处:加载默认样式,样式统一控制</p> </div>注:因为头部与尾部都是统一控制的,所以只需显示中间的内容即可。
1.7.2 自定义页面
#set($page_title="自定义样式页面") #set($layout="/layout/default.vm") <div> <p>设置某页面不使用 velocity配置默认的layout.vm模板,好处:不会加载默认样式,可以自定义</p> </div>
总结:
通过以上配置后普通页面velocity会自动套用layout/layout.vm模板
如果登录页面需套用自己独特的模板则如下
如:
可以在登录页面中添加:
#set($layout="/layout/default.vm")则登录页面将套用"default.vm"模板,样式需自定义;尤其是样式冲突时,非常好用
相关文章推荐
- Android之WebView加载网页
- MySQL存储过程
- 存储过程-求当前日期是这个月的第几周
- seajs官方文档地址
- 权限和特征的对应
- 数据存储全方案-文件存储、sharedPreferences、sqlite
- ubuntu下swoole安装
- dyld: Library not loaded: @rpath/libswiftCore.dylib 错误的解决
- 401,404的布尔值居然是False,又被坑了
- 二叉树的非递归遍历算法
- Python常见错误归纳
- putty pscp.exe使用
- SVN创建分支/合并分支/切换分支
- 一个自定义函数的例子
- LeetCode152:Maximum Product Subarray
- 学习笔记:HSB、HSL
- 摇一摇js代码
- 日期格式化
- 计算机网络学习笔记--数据链据层之MAC子层(整理)
- 利用 ubuntu 终端安装 vmware-tools