I have a velocity template which is used by a servlet plugin (v2). In this template, I want to use functions from a custom javascript file. I created very simple javascript file with the following content:-
function sayHello() { alert("Hello World"); }
I saved this file in src/main/resources/test/test.js of my plugin project. I then added the following web-resource:-
<web-resource name="testJavaScript" i18n-name-key="test-javascript.name" key="test-javascript"> <description key="test-javascript.description">The Test Javascript Plugin</description> <resource name="test.js" type="download" location="/test/test.js"/> <context>atl.general</context> </web-resource>
From what I understand, using a context of atl.general should mean that the javascript is automatically loaded by all atl.general pages? No need to add anything else in the velocity template, i.e. <script src="?????"><script>??
When I try to call my script, I can see from the chrome developer tools that it cannot find the function, so it's not getting loaded. Do I need to reference the script using a script tag? If so, what would the URL be?
Community moderators have prevented the ability to post new answers.
Hello Graeme,
in order to use js in your velocity page you need to load the web resource into your vm.
Add this line :
<html> <head> <title>HELLO PAGE</title> <meta name="decorator" content="atl.admin">
$webResourceManager.requireResource("BASE-PATH-PLUGIN:test-javascript") </head>
where BASE-PATH-PLUGIN is for example com.jira.plugin.pluginname
Hope this helps,
Fabio
That did it, many thanks Fabio!
For others - I ran some tests and seems that if you're using a TemplateRenderer to build velocity templates this is absolutely required. When building the HTML in the response without using the TemplateRenderer with a velocity template, this is not required and the javascript is imported.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
you're welcome Graeme.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
In your velocity-template, did you set the decorator to atl.general? Like so:
<html> <head> <meta name="decorator" content="atl.general" /> ...
Maybe that will help.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You said that you're writing a servlet plugin, so your page is not a WebAction? That could be the problem. I'm only familiar with WebAction pages, where using the decorator meta-tag helps. What's your reason to use a servlet? Have you tried using a WebAction? It's a <webwork1> module in the atlassian-plugin.xml.
If you want to stick to a servlet, take a look at this question.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks for your input Andreas, but Fabio managed to give me the answer to this problem.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@andreas ebert, could you help me with a sample? I'm using a web action in a jira plugin 2, velocity template and I have the same issue. The javascript does not execute.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.