让 Spring3 支持 velocity tools 2.x

目前Spring3的 Velocity toolbox 还只是支持到 1.x 版本的,无法使用 velocity tools 2.x 的功能,因此需要进行稍微的改造进而支持 velocity tools 2.x。

1. 使用 velocity tools 2.x 的ToolManager重写createVelocityContext() 方法,以加载tools 2.x:

/**
 * Spring3默认的 createVelocityContext 方法中采用的是 tools-1.x 的 ToolboxManager, ServletToolboxManager等类
 * 加载toolbox,但是 tools 2.x 中已经废弃了这些类,导致了无法加载tools 2.x。
 * 所以,这里采用tools 2.x中新的 ToolManager方式重写此方法加载toolbox2.x。
 */
public class VelocityLayoutToolboxView extends VelocityLayoutView 
{
	@Override
	protected Context createVelocityContext(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception
	{
		ViewToolContext ctx = new ViewToolContext(this.getVelocityEngine(), request, response, this.getServletContext());
		
		if(this.getToolboxConfigLocation() != null)
		{
			ToolManager tm = new ToolManager();
			tm.setVelocityEngine(this.getVelocityEngine());
			tm.configure(this.getServletContext().getRealPath(this.getToolboxConfigLocation()));
			
			for(String scope : Scope.values())
			{
				ctx.addToolbox(tm.getToolboxFactory().createToolbox(scope));
			}
		}
		
		if(model != null && !mode.isEmpty())
		{
			ctx.putAll(model);
		}
		
		return ctx;
	}
}


2. 指定 VelocityLayoutViewResolver 的 viewClass 属性为自己扩展的类:VelocityLayoutToolboxView
<bean class="org.springframework.web.servlet.view.velocity.VelocityLayoutViewResoler">
	<property name="..." value="..."/>
	<property name="viewClass" value="com.my.VelocityLayoutToolboxView"/>
	<property name="toolboxConfigLocation" value="/WEB-INF/tools.xml"/>
</bean>


3. 就可以使用 velocity tools 2.x 的功能了。

转载于:https://my.oschina.net/jsan/blog/202956

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
D、设置模板文件存放路径的配置为:`spring.velocity.resource-loader-path`。在Spring Boot中集成Velocity模板引擎时,需要进行相关配置以便正确地加载和渲染模板文件。常见的配置方式有两种: 1. 在`application.properties`或`application.yml`配置文件中定义Velocity的相关属性,如模板文件的位置、缓存的开关等。示例: ``` spring.velocity.resource-loader-path=classpath:templates/ spring.velocity.cache=false spring.velocity.charset=UTF-8 spring.velocity.suffix=.vm ``` 2. 使用Java代码配置Velocity模板引擎的相关属性。示例: ```java @Configuration public class VelocityConfig { @Bean public VelocityConfigurer velocityConfigurer() { VelocityConfigurer configurer = new VelocityConfigurer(); configurer.setResourceLoaderPath("classpath:templates/"); configurer.setVelocityProperties(velocityProperties()); return configurer; } @Bean public Properties velocityProperties() { Properties properties = new Properties(); properties.setProperty("input.encoding", "UTF-8"); properties.setProperty("output.encoding", "UTF-8"); properties.setProperty("directive.set.null.allowed", "true"); properties.setProperty("resource.manager.defaultcache.size", "100"); return properties; } @Bean public ViewResolver velocityViewResolver() { VelocityViewResolver resolver = new VelocityViewResolver(); resolver.setSuffix(".vm"); resolver.setCache(false); resolver.setContentType("text/html;charset=UTF-8"); resolver.setExposeSpringMacroHelpers(true); resolver.setExposeRequestAttributes(true); resolver.setExposeSessionAttributes(true); return resolver; } } ``` 其中,`spring.velocity.resource-loader-path`表示模板文件的路径,`spring.velocity.cache`表示是否开启缓存,`spring.velocity.charset`表示字符编码,`spring.velocity.suffix`表示模板文件的后缀名。因此,选项D是正确的。选项A和B的属性名称不正确;选项C的属性名称正确,但是它是用于设置模板文件的后缀名,而不是用于开启缓存。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值