Adding javascript web-resource for use in velocity template used by servlet

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="" key="test-javascript">
    <description key="test-javascript.description">The Test Javascript Plugin</description>
	<resource name="test.js" type="download" location="/test/test.js"/>

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?

2 answers

1 accepted

5 votes
Answer accepted

Hello Graeme,

in order to use js in your velocity page you need to load the web resource into your vm.

Add this line :

    &lt;title&gt;HELLO PAGE&lt;/title&gt;
    &lt;meta name="decorator" content="atl.admin"&gt;

where BASE-PATH-PLUGIN is for example com.jira.plugin.pluginname

Hope this helps,


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're welcome Graeme.

Many thumbs up :-)

In your velocity-template, did you set the decorator to atl.general? Like so:

    &lt;meta name="decorator" content="atl.general" /&gt;

Maybe that will help.

Thanks for your response, but yes I have that in there

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.

Thanks for your input Andreas, but Fabio managed to give me the answer to this problem.

@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.

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted Friday in United States

Confluence Security Advisory

Good morning Members, Not sure if you are aware. Please read the following: More details: https://co...

44 views 1 0
View post

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you