Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

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

Graeme Mitchell April 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

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

5 votes
Answer accepted
Fabio Racobaldo _Herzum_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 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 April 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
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 16, 2014

you're welcome Graeme.

Sandro Hörler August 7, 2017

Many thumbs up :-)

0 votes
Andreas Ebert
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 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 April 15, 2014

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

Andreas Ebert
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 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 April 16, 2014

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

barbosa_s_alexandre March 3, 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.

TAGS
AUG Leaders

Atlassian Community Events