Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

How to render Velocity Template?

Bob Jones June 30, 2015

I'm writing a JIRA plugin and I need to render a velocity template that I have written. My code is in src/main/package/MyClass.java and my template is in /src/main/resources/templates/packageName/template.vm. How should I render the template from MyClass.java so I can get the String contents of it?

1 answer

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

0 votes
Artem Triliser June 30, 2015

Hello Bob.

It depends on what kind of plugin you develop.

If you develop the servlet it looks like:

public void doFilter(ServletRequest request,ServletResponse response,FilterChain chain) throws IOException, ServletException {
final Map<String, Object> context = new HashMap<String, Object>();
context.put("yourVarName", "Your Value");
response.setContentType("text/html;charset=utf-8");
templateRenderer.render("templates/packageName/template.vm", context, response.getWriter());
}

But if its Web Action for example so you need to configure the atlassian-plugin.xml

Like the following:

<webwork1 key="actions" name="Name" class="java.lang.Object">
        <actions>
            <action name="package.MyClass" alias="MyClass">
                <view name="success">templates/packageName/template.vm</view>
            </action>
Bob Jones June 30, 2015

In your first example you use templateRenderer without ever declaring it. How do I get an instance of it? Also, can you give me a link that describes what a web action is? Thanks

Artem Triliser June 30, 2015

TemplateRenderer you must initiate via the constructor like this: import com.atlassian.templaterenderer.TemplateRenderer; public class YourClass { private final TemplateRenderer templateRenderer; public YourClass(TemplateRenderer templateRenderer) { this.templateRenderer = templateRenderer; } } Also you need to import it in the atlassian-plugin.xml <component-import key="renderer" interface="com.atlassian.templaterenderer.velocity.one.six.VelocityTemplateRenderer"/> Here is the link for WebAction documentation. https://developer.atlassian.com/jiradev/jira-architecture/building-jira-add-ons/jira-plugins2-overview/jira-plugin-module-types/webwork-plugin-module

Bob Jones June 30, 2015

Eclipse is telling me that, "The import com.atlassian.templaterenderer cannot be resolved". I have added the line to my atlassian-plugin.xml as well.

Artem Triliser June 30, 2015

Add a dependency <dependency> <groupId>com.atlassian.templaterenderer</groupId> <artifactId>atlassian-template-renderer-api</artifactId> <version>1.3.1</version> <scope>provided</scope> </dependency> Into your pom.xml

Bob Jones June 30, 2015

I added that to pom.xml and I still get the same error in Eclipse. When I build the project using the atlas-mvn command, the error I get is: "cannot find symbol class TemplateRenderer"

Artem Triliser June 30, 2015

Could you show your java class, atlassian-plugin.xml and pom.xml dependencies?

TAGS
AUG Leaders

Atlassian Community Events