I will write it as a memo for myself. I made it because I wanted an environment to check the behavior of Velocity while reading the official website.
--Tomcat 9 (Servlet container) --Eclipse (Integrated Development Environment)
This is a Git repository with a set of source code created by following the steps below. https://github.com/vicboss1002/velocity_sample
I downloaded and installed ** 32-bit / 64-bit Windows Service Installer ** from the following site. https://tomcat.apache.org/download-90.cgi
maven-archetype-webapp
in ** Select an Archetype **
pom.xml
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.velocity.tools</groupId>
<artifactId>velocity-tools-generic</artifactId>
<version>3.0</version>
</dependency>
<dependency>
<groupId>org.apache.velocity.tools</groupId>
<artifactId>velocity-tools-view</artifactId>
<version>3.0</version>
</dependency>
<dependency>
<groupId>org.apache.velocity.tools</groupId>
<artifactId>velocity-tools-view-jsp</artifactId>
<version>3.0</version>
</dependency>
<dependency>
<groupId>com.republicate</groupId>
<artifactId>webapp-slf4j-logger</artifactId>
<version>1.3</version>
</dependency>
web.xml
<!--Log output settings-->
<context-param>
<param-name>webapp-slf4j-logger.level</param-name>
<param-value>debug</param-value>
</context-param>
<context-param>
<param-name>webapp-slf4j-logger.format</param-name>
<param-value>%logger [%level] [%ip] %message</param-value>
</context-param>
<servlet>
<servlet-name>velocity</servlet-name>
<servlet-class>
org.apache.velocity.tools.view.VelocityViewServlet
</servlet-class>
<!-- Unless you plan to put your tools.xml and velocity.properties under
different folders or give them different names, then these two init-params
are unnecessary. The VelocityViewServlet will automatically look for these
files in the following locations. -->
<init-param>
<param-name>org.apache.velocity.toolbox</param-name>
<param-value>/WEB-INF/tools.xml</param-value>
</init-param>
<init-param>
<param-name>org.apache.velocity.properties</param-name>
<param-value>/WEB-INF/velocity.properties</param-value>
</init-param>
</servlet>
<!-- Map *.vm files to Velocity -->
<servlet-mapping>
<servlet-name>velocity</servlet-name>
<url-pattern>*.vm</url-pattern>
</servlet-mapping>
sample.vm
#set($text = "Velocity World!")
Hello $text
Launch Tomcat from the ** Servers ** view of Eclipse
Display and confirm ** sample.vm ** via the browser
Enter http: // localhost: 8080 / velocity_sample / sample.vm
in the URL of the browser to display it.
That's it. It was confirmed that the Velocity process described in ** sample.vm ** was executed and the contents were displayed.
VelictyViewServlet
.MyVelocityViewServlet.java
public class MyVelocityViewServlet extends VelocityViewServlet {
private static final long serialVersionUID = 1L;
protected Template handleRequest(HttpServletRequest request,
HttpServletResponse response,
Context ctx)
{
ctx.put("boolTrue", true);
ctx.put("boolFalse", false);
ctx.put("number", 1234);
ctx.put("string", "abcd");
ctx.put("list", Arrays.asList("a", "b", "c", "d"));
Map<String, String> map = new HashMap<>();
map.put("key1", "value1");
map.put("key2", "value2");
ctx.put("map", map);
return super.handleRequest(request, response, ctx);
}
}
VelictyViewServlet
subclass as follows:web.xml
<servlet>
<servlet-name>velocity</servlet-name>
<!-- <servlet-class> -->
<!-- org.apache.velocity.tools.view.VelocityViewServlet -->
<!-- </servlet-class> -->
<servlet-class>velocity_sample.MyVelocityViewServlet</servlet-class>
<!-- Unless you plan to put your tools.xml and velocity.properties under
different folders or give them different names, then these two init-params
are unnecessary. The VelocityViewServlet will automatically look for these
files in the following locations. -->
<init-param>
<param-name>org.apache.velocity.toolbox</param-name>
<param-value>/WEB-INF/tools.xml</param-value>
</init-param>
<init-param>
<param-name>org.apache.velocity.properties</param-name>
<param-value>/WEB-INF/velocity.properties</param-value>
</init-param>
</servlet>
sample_of_context.vm
\${boolTrue}: ${boolTrue}<br/>
\${boolFalse}: ${boolFalse}<br/>
\${number}: ${number}<br/>
\${string}: ${string}<br/>
\${list}: ${list}<br/>
\${map}: ${map}<br/>
The output is as follows.
Recommended Posts