Hey Everyone,
I am trying to create a JIRA plugin, and all I am trying to do at this point is to pass a variable from my Java Servlet to my Velocity Template. This is what my code looks like:
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.atlassian.sal.api.pluginsettings.PluginSettings;
import com.atlassian.sal.api.pluginsettings.PluginSettingsFactory;
import com.atlassian.plugin.spring.scanner.annotation.component.Scanned;
import com.atlassian.templaterenderer.TemplateRenderer;
import javax.inject.Inject;
import com.atlassian.plugin.spring.scanner.annotation.imports.ComponentImport;
@Scanned
public class HomeServlet extends HttpServlet
{
private static final String PLUGIN_STORAGE_KEY = "com.atlassian.plugins.dashboard.refapp.settings";
@ComponentImport
private final TemplateRenderer renderer;
@ComponentImport
private final PluginSettingsFactory pluginSettingsFactory;
@Inject
public HomeServlet(TemplateRenderer renderer, PluginSettingsFactory pluginSettingsFactory)
{
super();
this.renderer = renderer;
this.pluginSettingsFactory = pluginSettingsFactory;
}
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
Map<String, Object> context = new HashMap<String, Object>();
PluginSettings pluginSettings = pluginSettingsFactory.createGlobalSettings();
if (pluginSettings.get(PLUGIN_STORAGE_KEY + ".token") == null) {
context.put("token", pluginSettings.get(PLUGIN_STORAGE_KEY + ".token"));
}
if (pluginSettings.get(PLUGIN_STORAGE_KEY + ".email") == null) {
context.put("email", pluginSettings.get(PLUGIN_STORAGE_KEY + ".email"));
}
if (pluginSettings.get(PLUGIN_STORAGE_KEY + ".password") == null) {
context.put("password", pluginSettings.get(PLUGIN_STORAGE_KEY + ".password"));
}
System.out.println("SAVED:");
System.out.println(pluginSettings.get(PLUGIN_STORAGE_KEY + ".token"));
System.out.println(pluginSettings.get(PLUGIN_STORAGE_KEY + ".email"));
System.out.println(pluginSettings.get(PLUGIN_STORAGE_KEY + ".password"));
context.put("myvar", "Hello World!");
response.setContentType("text/html;charset=utf-8");
renderer.render("templates/dashboard.vm", context, response.getWriter());
}
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
PluginSettings pluginSettings = pluginSettingsFactory.createGlobalSettings();
pluginSettings.put(PLUGIN_STORAGE_KEY + ".token", request.getParameter("token"));
pluginSettings.put(PLUGIN_STORAGE_KEY + ".email", request.getParameter("email"));
pluginSettings.put(PLUGIN_STORAGE_KEY + ".password", request.getParameter("password"));
response.sendRedirect("dashboard");
}
}
This will render the velocity template, but I cannot seem to get it to access the variables I pass. In my println statement, it prints the variable fine. In my template, if I add the following code, the variable does not display. It only shows ‘$token’
<label>$token</label>
I got this code from the JIRA developer documentation, so I am not entirely sure why it is not working. Am I passing the context incorrectly? Am I accessing it incorrectly?
I have printed out the $context variable from the template, and that prints out an object with a bunch of classes, though, none of them are my context variables (or give me access to them).
Also, this is not to be used with Confluence. Any help will be appreciated. I have been stuck on this issue for hours.
Thanks
