Spring Template - 1.0

Extension Id

com.castsoftware.java.springtemplate

What’s new?

Please see Spring Template - 1.0 - Release Notes for more information.

Description

Spring Template Extension provide support for Spring Web MVC applications, which uses Apache FreeMarker or Apache Velocity.

In what situation should you install this extension?

If your Java Spring Web MVC application uses the Apache FreeMarker or Apache Velocity Framework.

Technology support

Component Version Supported Supported Technology
spring-webmvc ≤ 4.3.x JAVA
spring-context ≤ 4.3.x JAVA

Compatibility

This extension is compatible with:

Core release Supported
≥ 8.3.x

Download and installation instructions

The extension will not be automatically downloaded and installed. If you need to use it, you should manually install the extension using the Application - Extensions interface.

What results can you expect?

Objects

Icon Description
Java Spring Template Parameter
Link Type Source and destination of link Supported Velocity Engine Core Methods
relyonLink Between CAST Java Spring Template Parameter and respective Context JAVA class.
Model Attributeorg.springframework.ui.Model.addAttribute
org.springframework.ui.Model.addAllAttributes
org.springframework.ui.Model.mergeAttributes
org.springframework.ui.ModelMap.addAttribute
org.springframework.ui.ModelMap.addAllAttributes
org.springframework.ui.ModelMap.mergeAttributes

NOTE: Java Spring Template Parameter object will be created, if the Model Attribute APIs are called from Spring Controller methods which returns template name string
callLink Between HTML source code objects and Java Velocity Call To VTLContext or Java Freemarker FTL Call objects.
callLink Between Java Velocity Call To VTLContext or Java Freemarker FTL Call objects and JAVA methods.

Code Examples

Velocity Spring

Context Class - Tutorial.java
package com.baeldung.mvc.velocity.domain;

public class Tutorial {

    private final Integer tutId;
    private final String title;
    private final String description;
    private final String author;

    public Tutorial(Integer tutId, String title, String description, String author) {
        this.tutId = tutId;
        this.title = title;
        this.description = description;
        this.author = author;
    }

    public Integer getTutId() {
        return tutId;
    }

    public String getTitle() {
        return title;
    }

    public String getDescription() {
        return description;
    }

    public String getAuthor() {
        return author;
    }

}
Spring Controller Class - MainController.java
package com.baeldung.mvc.velocity.controller;

import com.baeldung.mvc.velocity.domain.Tutorial;
import com.baeldung.mvc.velocity.service.ITutorialsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

@Controller
@RequestMapping("/")
public class MainController {

    @Autowired
    private ITutorialsService tutService;

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String welcomePage() {
        return "index";
    }

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String listTutorialsPage(Model model) {
        List<Tutorial> list = tutService.listTutorials();
        model.addAttribute("tutorials", list);
        return "list";
    }

    public ITutorialsService getTutService() {
        return tutService;
    }

    public void setTutService(ITutorialsService tutService) {
        this.tutService = tutService;
    }

}
Velocity Template - list.vm
<h1>Index</h1>
 
<h2>Tutorials list</h2>
<table border="1">
<tr>
 <th>Tutorial Id</th>
 <th>Tutorial Title</th>
 <th>Tutorial Description</th>
 <th>Tutorial Author</th>
</tr>
#foreach($tut in $tutorials)
  <tr>
   <td id="tutId_$foreach.count">$tut.tutId</td>
   <td id="title_$foreach.count">$tut.title</td>
   <td id="desc_$foreach.count">$tut.description</td>
   <td id="auth_$foreach.count">$tut.author</td>
  </tr>
#end
</table>