[JAVA] Until you run the Apache Velocity sample code

Until you run the Apache Velocity sample code

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.

Things necessary

--Tomcat 9 (Servlet container) --Eclipse (Integrated Development Environment)

Complete source code

This is a Git repository with a set of source code created by following the steps below. https://github.com/vicboss1002/velocity_sample

procedure

  1. Install Tomcat
  2. Set Tomcat as a server in Eclipse
  3. Create a Maevn project in Eclipse
  4. Download dependent libraries with Maven
  5. Display the Velocity template file in your browser
  6. Pass variables from Java to the Velocity context for display

Install tomcat

I downloaded and installed ** 32-bit / 64-bit Windows Service Installer ** from the following site. https://tomcat.apache.org/download-90.cgi image.png

Set Tomcat as a server in Eclipse

  1. Open ** Window> Preferences> Server> Runtime Environment ** image.png image.png

Create a Maven project in Eclipse

  1. Select ** File> New> Maven Project ** image.png
  2. Select maven-archetype-webapp in ** Select an Archetype ** image.png
  3. Specify ** GroupId **, ** ArtifactId ** and create Maven project image.png image.png

Download dependent libraries with Maven

  1. In ** pom.xml **, specify the following for the dependent library

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>
  1. Run ** Project> Run As> Maven build ** image.png image.png

Display Velocity template file in browser

  1. Add ** VelocityViewServlet ** settings to web.xml Add the ** VelocityViewServlet ** settings to web.xml based on the description in here.

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>
  1. Create a template file directly under ** project root> src ** image.png

sample.vm


#set($text = "Velocity World!")
Hello $text
  1. Launch Tomcat from the ** Servers ** view of Eclipse image.png

  2. 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. image.png

That's it. It was confirmed that the Velocity process described in ** sample.vm ** was executed and the contents were displayed.

Pass variables from Java to Velocity context for display

  1. Create a subclass of 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);
	}
}
  1. Rewrite web.xml for the 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>
  1. Create a template file that calls the variables set from Java

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. image.png

Recommended Posts

Until you run the Apache Velocity sample code
Until you run apache on ubuntu on docker
Apache beam sample code
Until the code is executed
Until you run Hello World of JavaFX with VS Code + Gradle
Until you run CuPy (v11.1) on Ubuntu 20.04
Learn RSpec with Everyday Rails. Until you bundle install the sample app.
Until you run a Java program with the AWS SDK local to Windows
Using templates on the classpath with Apache Velocity
Until you run mruby in your browser (WebAssembly)
Java sample code 02
Java sample code 03
Java sample code 04
Java sample code 01
Until you try running Apache Kafka with docker image
Until you publish the app on the App Store by yourself
Until the Google Assistant sample runs on Android Things