JIRA custom plugin : How to pass variables from java class to vm template ?

Karel Striegel April 6, 2014

How can I pass a variable (e.g: String myvar= "Hello World";) from my java class to my template file called test.vm ?

I was following the Admin UI tutorial, but that part is not very clear, or not covered.

At this moment I have the following code :

String myvar = "Hello World";
context.put("myvar",myvar);
response.setContentType("text/html;charset=utf-8");
templateRenderer.render("test.vm", response.getWriter());

Please also provide the code which belongs in the *.vm file. I'm assuming you use $myvar ?

Kind regards
Karel.

4 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

2 votes
Answer accepted
Nadir MEZIANI
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 7, 2014

Hi,

I think that you have omitted the context in the render method

try this:

String myvar = "Hello World";
context.put("myvar",myvar);
response.setContentType("text/html;charset=utf-8");
templateRenderer.render("test.vm",context, response.getWriter());

0 votes
Karel Striegel April 7, 2014

Solved

Deleted user January 8, 2018

How did you solve it? What sort of object is "context"? Do you have a full code example?

Jackson Farnsworth
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 23, 2018

Dont suppose you found what object context is?

Deleted user January 23, 2018

Here is some sample code showing how to pass variables into a velocity template in an HttpServlet (in this case our variables control whether or not to show an OAuth button on a settings page):

Integer OAuthed = 0;
Map<String, Object> context = new HashMap<String, Object>();
context.put("OAuthed", OAuthed);
renderer.render("admin.vm", context, response.getWriter());

 We use the variables in the velocity template file (ours was called admin.vm) like this:

#if ($OAuthed == 1)
<a href="/jira/plugins/servlet/test/admin?clearOAuth=1">Clear OAuth</a>
#end

You also need to use these dependencies in your HttpServlet:

import java.util.HashMap;
import java.util.Map;
Jackson Farnsworth
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 23, 2018

That's spectacular. Thanks for the help!

0 votes
Karel Striegel April 7, 2014

Well I must be missing something, because at the moment is it not working. Instead of printing "Hello World" it is printing '$myvar'. So the code is not seen as a variable, but as text.

Regards.

0 votes
Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 6, 2014

You've answered your own question. The velocity variably $myvar can be used in the .vm file. It's really just $myvar - as you've got a string, it'll be able to parse that straight out into text.

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

TAGS
AUG Leaders

Atlassian Community Events