Skip to main content

dawdler-client-plug-velocity

Module Introduction​

A plugin for the Velocity template engine, currently upgraded to the latest version 2.4.1 (it previously went through versions 1.6, 1.7, 2.2 and 2.3).

1. Adding the dependency in pom​

<groupId>club.dawdler</groupId>
<artifactId>dawdler-client-plug-velocity</artifactId>

2. properties file reference​

Refer to the official documentation for the detailed configuration

velocimacro.library.autoreload=true
resource.loader.cache=false
resource.loader.class.cache=false
resource.default_encoding=utf-8
output.encoding=utf-8
template.path=/templates/
template.suffix=.vm
#Custom directives; refer to the Velocity official documentation to extend them
runtime.custom_directives=club.dawdler.clientplug.velocity.direct.PageDirect,club.dawdler.clientplug.velocity.direct.ControlDirect

3. Built-in commonly used directives​

3.1 Pagination directive #pages​

The #pages directive provides one-line pagination. Usage:

Example 1:

<!-- ~p is a marker with a fixed syntax, used to pass the dynamic page marker -->
<!-- Renders Home Previous 1 2 3 4 5 6 7 8 9 Next Last -->
#pages("user/list.html?page=~p")

Example 2:

<!-- Renders Home Previous 1 2 3 4 5 6 7 8 9 Next Last with an extra parameter visible -->
#pages("user/list.html?page=~p&visible=1")

Example 3:

<!-- adminStyle is a custom style; examples 1 and 2 use the system default because no style is passed. -->
#pages("user/list.html?page=~p&visible=1","adminStyle")

Extending with a custom style

See the export method in the PageStyle source code:

//See the source code for the related comments
public static void export(String prefix, String first, String up, String pages, String pageOn, String last,
String end, String stepPage)

For example:

//adminStyle extension implementation

export("adminStyle", "<span><a href=\"" + CONTENT_MARK + "\">Home</a></span>",
"<a class=\"prev\" href=\"" + CONTENT_MARK + "\"></a>",
"<a href=\"" + CONTENT_MARK + "\">" + P_MARK + "</a>",
"<strong><font color=\"red\">" + P_MARK + "</font></strong>",
"<a class=\"nxt\" href=\"" + CONTENT_MARK + "\"></a>",
"<span><a href=\"" + CONTENT_MARK + "\">Last</a></span>", null);

3.2 XSS filter directive #XSSFilter​

Used to filter XSS script attacks. Usage:

//Filter a string directly
#XSSFilter("<script>alert(1);</script>")

//Example: output a property of an entity object; `thread` is an object returned by the backend and placed in the Velocity context.
#XSSFilter($thread.content)

3.3 Other directives​

The #tree directive is deprecated and not covered here; for such trees it is recommended to use a front-end component.

The #control directive is a set of directives implementing dynamic forms; contact me if you need it. It is not covered here.

4. Configuring template paths​

Configure the template path and suffix in velocity.properties

template.path=/templates/
template.suffix=.vm

5. Using VelocityToolBox​

Implementing custom directives is complicated, so to make utility-class methods easier to use, an extension called VelocityToolBox is provided.

Usage:

  1. Write a class extending VelocityToolBox; an alias must be passed to the constructor.
public class MyTool extends VelocityToolBox{

public MyTool(String name) {
super(name);//Alias used by Velocity
}
//Define a method that converts to upper case
public String toUpperCase(String content) {
if(content == null)
return null;
return content.toUpperCase();
}
}
  1. Create a toolBoxes.properties file under resources and configure it; the key in the properties file is the alias and the value is the class name.

toolBoxes.properties supports multi-environment configuration. See Unified Configuration Center and Multi-Environment Support.

myTool=com.anywide.yyg.user.velocity.tool.MyTool
  1. Use it in Velocity
<html>
<head>
<title>Velocity custom tool class</title>
</head>

<body>
$myTool.toUpperCase("hello")
</body>

</html>

6. Using Velocity in a controller​

@ResponseBody
@RequestMapping(value = "/list.html", method = RequestMethod.GET, viewType = ViewType.velocity)
public void list(Integer pageOn, Integer hostId, ViewForward viewForward) {
if (pageOn == null) {
pageOn = 1;
}
viewForward.setTemplatePath("/vm/list");
PageResult<List<Vm>> pageResult = vmService.selectPageList(hostId, pageOn, 10);
Map<String, Object> data = new HashMap<>();
data.put("data", pageResult.getData());
data.put("page", pageResult.getPage());
viewForward.setData(data);
}