Integrating Spring MVC with Velocity Template Engine!

Just for the fun of it.

pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
                             http://maven.apache.org/maven-v4_0_0.xsd">
 
    <modelVersion>4.0.0</modelVersion>
 
    <groupId>biz.tugay</groupId>
    <artifactId>smvcpoc</artifactId>
 
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
 
    <name>smvcpoc Maven Webapp</name>
    <url>http://maven.apache.org</url>
 
    <dependencies>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.0.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.0.0.RELEASE</version>
            <scope>compile</scope>
        </dependency>
 
        <!-- contains: VelocityEngineFactory -->
        <!-- http://mvnrepository.com/artifact/org.springframework/spring-context-support -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>4.1.4.RELEASE</version>
        </dependency>
 
        <!-- http://mvnrepository.com/artifact/org.apache.velocity/velocity -->
        <dependency>
            <groupId>org.apache.velocity</groupId>
            <artifactId>velocity</artifactId>
            <version>1.7</version>
        </dependency>
 
        <!-- http://mvnrepository.com/artifact/org.apache.velocity/velocity-tools -->
        <dependency>
            <groupId>org.apache.velocity</groupId>
            <artifactId>velocity-tools</artifactId>
            <version>2.0</version>
        </dependency>
    </dependencies>
 
    <build>
        <finalName>smvcpoc</finalName>
        <plugins>
            <plugin>
                <groupId>org.eclipse.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <version>9.2.1.v20140609</version>
                <configuration>
                    <scanIntervalSeconds>2</scanIntervalSeconds>
                    <webApp>
                        <contextPath>/</contextPath>
                    </webApp>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                             http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="3.0">
 
    <servlet>
        <servlet-name>springDispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/servletContext.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
 
    <servlet-mapping>
        <servlet-name>springDispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
 
</web-app>

servletContext.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-4.0.xsd">
 
    <bean id="repidController" class="biz.tugay.RepidController" />
 
    <bean id="velocityConfig"
          class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
        <property name="resourceLoaderPath" value="/"/>
    </bean>
 
    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
        <property name="cache" value="false" />
        <property name="prefix" value="/WEB-INF/views/" />
        <property name="exposeSpringMacroHelpers" value="true"/>
        <property name="contentType" value="text/html;charset=UTF-8" />
    </bean>
 
</beans>

index.vm
<!DOCTYPE html>
<html>
<head>
    <title>Repid Type POC</title>
    <style type="text/css">
        table {
            border-collapse: collapse;
        }
        th, td {
            padding: 5px 10px;
            border: 1px solid #999;
        }
        th {
            background-color: #eee;
        }
    </style>
</head>
<body>
 
<h1>Repid Type Table</h1>
 
#* @vtlvariable name="repidTypeColumns" type="java.util.List<biz.tugay.RepidTypeTableColumn>" *#
#* @vtlvariable name="repidTypeRows" type="java.util.List<biz.tugay.RepidTypeTableRow>" *#
 
<table id="simpleTable" class="tablesorter">
    <thead>
    <tr>
        #foreach($repidTypeColumn in $repidTypeColumns)
            <th>$repidTypeColumn.columnHeaderText</th>
        #end
    </tr>
    </thead>
    <tbody>
        #foreach($repidTypeRow in $repidTypeRows)
        <tr>
            #foreach($repidTypeCell in $repidTypeRow.repidTypeTableCells)
                <td>$repidTypeCell.data</td>
            #end
        </tr>
        #end
    </tbody>
</table>
</body>
</html>

RepidController.java
package biz.tugay;
 
/* User: koray@tugay.biz Date: 2016/06/09 */
 
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
 
import java.io.File;
import java.io.IOException;
 
@Controller
@RequestMapping(value = "/repid")
public class RepidController {
    @RequestMapping(method = RequestMethod.GET, value = "/repidType")
    public ModelAndView foo() throws IOException {
        final RepidTypeTable repidTypeTable = new RepidTypeTable(new File("zbde.meta"), new File("koray.txt"));
        final ModelAndView modelAndView = new ModelAndView("index.vm");
        modelAndView.addObject("repidTypeColumns", repidTypeTable.getRepidTypeTableColumns());
        modelAndView.addObject("repidTypeRows", repidTypeTable.getRepidTypeTableRows());
        return modelAndView;
    }
}