spring boot 转xml格式报错解决方法_springboot1.5x版不支持velocity的解决方案

a7ba045a8517e02196233e0101c91e4c.png

springboot 在1.4版本中 融合了velocity,freemarker和thymeleaf模板。这个版本,如果想使用这些模板,只需要引入相应的starter pom就可以了。如 我想要在我的项目中使用thymeleaf模板。基本的步骤如下:

1. 引入starter 到pom

<!-- thymeleaf模板jar -->
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

2、在application的属性properties文件上配置 thymeleaf 相关属性

##thymeleaf start
#spring.thymeleaf.mode=HTML5
#spring.thymeleaf.encoding=UTF-8
#spring.thymeleaf.content-type=text/html
##开发时关闭缓存,不然没法看到实时页面
#spring.thymeleaf.cache=false
#spring.thymeleaf.prefix=classpath:/templates/
#spring.thymeleaf.suffix=.html
##thymeleaf end

只需要上述的两个步骤,就可以在视图层渲染页面了。其他的 velocity和freemarker大体类似,不做赘述。

但是,问题来了,一开始我也以为事情就这么简单。但是在我引入了 velocity 模板之后,idea提示我 找不到jar。what? 瞬间懵逼了。 没按套路出牌啊?大家看下面的错误提示。

6205d901e956b15de5d96426a8d1262f.png

错误提示 是unknown jar! 什么 ? velocity unkonwn? 不会吧? velocity 没集成到springboot? 我不信! 后来我翻了 spring-boot-dependencies 的pom 找,答案是果然没有!!咩有! 石化表情

上网一查,我才知道

由于springboot1.5和以上版本 已经完全抛弃了velocity视图模板,所以,如果我们的系统,一直以来都是用的 velocity,而又希望用更高版本(>=1.5)的springboot,这就左右为难了。

但是不用也不行啊!总不能为了一个velocity就修改springboot版本吧! 没办法,自己引吧!

1.引入jar 带版本号的

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-velocity</artifactId>
   <version>1.2.2.RELEASE</version>
</dependency>

2.使用xml文件 自定义 velocity解析器! application-b.xml 具体信息

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
		http://www.springframework.org/schema/beans
		http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<bean id="mvcVelocityEngine" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
        <property name="resourceLoaderPath" value="/templates/" /><!-- 模板存放的路径 -->
        <property name="configLocation" value="classpath:velocity.properties"/>
</bean>

<bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
        <property name="cache" value="false" />
        <property name="prefix" value="/templates/" /><!-- 视图文件的前缀,即存放的路径 -->
        <property name="suffix" value=".vm" /><!-- 视图文件的后缀名 -->
        <!--<property name="toolboxConfigLocation" value="/WEB-INF/tools.xml" /><!–toolbox配置文件路径–>-->
        <property name="dateToolAttribute" value="date" /><!--日期函数名称-->
        <property name="numberToolAttribute" value="number" /><!--数字函数名称-->
        <property name="contentType" value="text/html;charset=UTF-8" />
        <property name="exposeSpringMacroHelpers" value="true" /><!--是否使用spring对宏定义的支持-->
        <property name="exposeRequestAttributes" value="true" /><!--是否开放request属性-->
        <property name="requestContextAttribute" value="rc"/><!--request属性引用名称-->
        <property name="toolboxConfigLocation" value="/WEB-INF/toolbox.xml"/>

</bean>

3.将xml 引入到项目中

@ServletComponentScan
@SpringBootApplication
@ImportResource({"classpath:application-b.xml"})
public class Application extends SpringBootServletInitializer {

	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
		return application.sources(Application.class);
	}
}

4、如果想使用velocity 的自定义toolboox,还需要做如下的配置

在main下构建webapp目录,webapp目录下创建WEB-INF文件夹,然后把toolbox配置文件放到里边。

把toolbox 的xml地址 放到之前的 application-b.xml里。

经过我多次测试,只有放在这里,这有这样做才能被 velacity的 manager读取。所以只能是这样!

5.目前到这里,你以为就完了么? 答案是No! 在vm的页面上 使用自定义函数,你会发现,不好用! 没效果! 什么玩意啊! 根本就不调用好嘛!

不要着急 不要着急 !

在你调用的时候 ,你会惊喜的发现 后来有这两行提示

14-11-15 01:30:18:INFO org.apache.velocity.tools.view.servlet.ServletToolboxManager - Using config file '/WEB-INF/classes/velocity-toolbox.xml' 
14-11-15 01:30:18:WARN org.apache.velocity.tools.view.XMLToolboxManager - XMLToolboxManager has been deprecated. Please use org.apache.velocity.tools.ToolboxFactory instead. 

所以 原因大概已经清楚了! 因为你的velocity- toolbox 版本是2.0版本,XMLToolboxManager has been deprecated !

我还能怎么办!我也很无奈啊!

但是,经过我一下午的寻找终于找到了解决办法,方法也很简单,只要一招 ,妙解!

看清楚我下面的toolbox.xml怎么写的了么! xml的 root 元素 用<toolbox>替换<tools>!

<?xml version="1.0" encoding="UTF-8"?>
<toolbox>
        <data key="foo">this is foo</data>
        <tool>
                <key>urlSign</key>
                <scope>request</scope>
                <class>com.github.qinyinglian.common.UrlSignUtil</class>
        </tool>
</toolbox>

就可以了!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值