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.
Community moderators have prevented the ability to post new answers.
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());
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
How did you solve it? What sort of object is "context"? Do you have a full code example?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Dont suppose you found what object context is?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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;
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
That's spectacular. Thanks for the help!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Community moderators have prevented the ability to post new answers.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.