It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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

Graeme Mitchell Apr 15, 2014

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?

2 answers

1 accepted

5 votes
Answer accepted
Fabio Racobaldo [Herzum] Community Leader Apr 16, 2014

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;html&gt;
  &lt;head&gt;
    &lt;title&gt;HELLO PAGE&lt;/title&gt;
    &lt;meta name="decorator" content="atl.admin"&gt;
$webResourceManager.requireResource("BASE-PATH-PLUGIN:test-javascript")
&lt;/head&gt;

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

Hope this helps,

Fabio

Graeme Mitchell Apr 16, 2014

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.

Fabio Racobaldo [Herzum] Community Leader Apr 16, 2014

you're welcome Graeme.

Sandro Hörler Aug 07, 2017

Many thumbs up :-)

0 votes
Andreas Ebert Apr 15, 2014

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

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

Maybe that will help.

Graeme Mitchell Apr 15, 2014

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

Andreas Ebert Apr 16, 2014

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.

Graeme Mitchell Apr 16, 2014

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

barbosa.s.alexandre Mar 03, 2017

@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
This widget could not be displayed.
This widget could not be displayed.
Community showcase
Posted in United States

Topic Tuesday: Agile

Another week, another Topic Tuesday!  Agile has been on my mind a lot lately because I've seen good implementations and bad implementations, but now as a Jira admin it matters more to me whether...

27 views 0 0
View post

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you