Springboot 1.5x does not support velocity solutions

Keywords: Thymeleaf Spring xml SpringBoot

springboot integrates velocity, freemarker and thymeleaf templates in version 1.4. In this version, if you want to use these templates, you just need to introduce the corresponding starter pom. If I want to use thymeleaf template in my project. The basic steps are as follows:

1. Introducing starter to pom

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

2. Configure thymeleaf-related properties on the application properties file

##thymeleaf start
#spring.thymeleaf.mode=HTML5
#spring.thymeleaf.encoding=UTF-8
#spring.thymeleaf.content-type=text/html
##Close the cache at development time or you won't be able to see real-time pages
#spring.thymeleaf.cache=false
#spring.thymeleaf.prefix=classpath:/templates/
#spring.thymeleaf.suffix=.html
##thymeleaf end

As long as the above two steps, you can render the page in the view layer. Other velocities are similar to freemarker s in general, without further elaboration.


But the problem came. At first I thought it was as simple as that. But after I introduced the velocity template, idea prompted me jar could not be found. what? Instantly confused. Didn't you play the card according to the routine? Look at the following error tips.

The error prompt is unknown jar! What? velocity unkonwn? No? velocity is not integrated into spring boot? I don't believe it! Later, I got tired of pom searching for spring-boot-dependencies, and the answer was no!! Baa! Petrified expression

I just know when I check on the internet.

Since Springboot 1.5 and above have completely abandoned the velocity view template, it would be a dilemma if our system had always used velocity and wanted to use a higher version of springboot (>= 1.5).

But no need, no way! You can't modify the spring boot version just for a velocity! No way, you can quote it yourself!

1. Introducing jar with version number

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

2. Customize the velocity parser with xml file!

3. Introducing xml into the project

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

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

4. If you want to use velocity's custom toolboox, you need to configure it as follows

Build the webapp directory under main, create the WEB-INF folder under webapp directory, and then put the toolbox configuration file inside.

Put the XML address of toolbox in the previous application-b.xml.

After many tests, I found that only by putting it here can it be read by velacity's manager. So this is the only way!


5. So far, do you think it's over?

The answer is No! Using custom functions on vm pages, you will find that it is not easy to use! No effect! What is it? It's not good to call it at all! ____________

Don't worry, don't worry!

When you call it, you'll be surprised to find these two lines of hints later.

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. 

So the reason is probably clear! Because your velocity-toolbox version is version 2.0, the XML Toolbox Manager has been deprecated!

What else can I do? I am also helpless!

However, after an afternoon of searching, I finally found a solution, the method is also very simple, as long as one move, the magic solution! ___________

See how I write toolbox.xml below! The root element of xml is replaced by < Tools > with < toolbox >!

<?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>

That's all right!

Note: If you want to know more about my other technology sharing, please pay attention to my micro-signal brave_gua.

More wonderful content is waiting for you! In addition, it is not easy to edit this article hard. Please support it. If you have any questions, please comment and thank you.

Posted by rashpal on Sat, 08 Jun 2019 13:44:26 -0700