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

Image reference in Velocity (Jira 5.1)

jan.dolezal August 20, 2012

Hi,

i want to refere to the image in velocity template, but i dont know how. I have this web resource:

<web-resource name="SNPluginImages" i18n-name-key="sn-plugin-images.name" key="sn-plugin-images">
    <description key="sn-plugin-images.description">The SNPluginImages Plugin</description>
    <resource name="ajax-loader.gif" type="download" location="/images/ajax-loader.gif">
        <param name="content-type" value="image/gif" />
    </resource>>
  </web-resource>

And in velocity im trying to use :

<img id="sn-value-ajax-loader-image" src="$requestContext.baseUrl/download/resources/com.intelek.serialnumberChecker:sn-plugin-images/images/ajax-loader.gif" />

But it doesnt work. What is right syntax for referes ?

Thank you.

3 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
Jobin Kuruvilla [Adaptavist]
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.
August 20, 2012

You can't use /images/ in your path unless you map the directory. Here you are mapping the image directly and hence you should try something like

<img id="sn-value-ajax-loader-image" src="/jira/download/resources/com.intelek.serialnumberChecker:sn-plugin-images/ajax-loader.gif">

It is better to map the directory as:

<resource type="download" name="images/"location="images/"/>

And do what you are doing now.

jan.dolezal August 20, 2012

Thats it ! :) Good to know, its working like shortcut. Thank you. Its working now !

0 votes
Florin Manaila
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.
August 20, 2012

It would help to know where are you trying to use the velocity template. More specifically who populates the context.

You could use JiraVelocityUtils.createVelocityParams(jiraAuthenticationContext) to get all sort of helpful stuff in the context (like the baseUrl).

For the rest of the url, I usually use the WebResourceUrlProvider to get the link based on the keys in the atlassian-plugin.xml.

HTH,

Florin.

jan.dolezal August 20, 2012

Hi Florin,

its on edit.vm on customfield.

package com.intelek.jira.customfields;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.atlassian.jira.issue.customfields.impl.TextCFType;
import com.atlassian.jira.issue.customfields.manager.GenericConfigManager;
import com.atlassian.jira.issue.customfields.persistence.CustomFieldValuePersister;
import com.atlassian.jira.issue.customfields.impl.FieldValidationException;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.fields.config.FieldConfig;
import com.atlassian.jira.issue.fields.layout.field.FieldLayoutItem;
import java.util.List;
import java.util.Map;

public class serialnumberCF extends TextCFType {
    private static final Logger log = LoggerFactory.getLogger(serialnumberCF.class);

    public serialnumberCF(CustomFieldValuePersister customFieldValuePersister, GenericConfigManager genericConfigManager) {
    super(customFieldValuePersister, genericConfigManager);
}
    
    @Override
    public Map<String, Object> getVelocityParameters(final Issue issue,
                                                     final CustomField field,
                                                     final FieldLayoutItem fieldLayoutItem) {
        final Map<String, Object> map = super.getVelocityParameters(issue, field, fieldLayoutItem);

        // This method is also called to get the default value, in
        // which case issue is null so we can't use it to add currencyLocale
        if (issue == null) {
            return map;
        }

         FieldConfig fieldConfig = field.getRelevantConfig(issue);
         //add what you need to the map here

        return map;
    }
}

Florin Manaila
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.
August 20, 2012

For the baseUrl you can get it using

map.putAll(JiraVelocityUtils.createVelocityParams(jiraAuthenticationContext));

and the WebResourceUrlProvider via constructor injection.

jan.dolezal August 20, 2012

Uh, i am not familiar with these methods and java , so its chinese for me now.

Florin Manaila
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.
August 20, 2012

So, you get the authentication context like this.

ComponentAccessor.getJiraAuthenticationContext()

And then use it to get the velocity params

map.putAll(JiraVelocityUtils.createVelocityParams(jiraAuthenticationContext));

This will allow you to use $baseurl in the velocity context, as you said before.

Then, for the second part, you'll need the WebResourceUrlProvider in your class.

private WebResourceUrlProvider urlProvider;
public serialnumberCF(CustomFieldValuePersister customFieldValuePersister, GenericConfigManager genericConfigManager, WebResourceUrlProvider urlProvider) {
    super(customFieldValuePersister, genericConfigManager);
    this.urlProvider = urlPorvider;
}

Now that you have it as a member in your class, you can use it to get the url of the resource using urlProvider.getStaticPluginResourceUrl(...). (or use Jobin's solution below for this second part, it's better)

0 votes
Igor Loskutov
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.
August 20, 2012

Try ${requestContext.baseUrl}

jan.dolezal August 20, 2012

I tried ${requestContext.baseUrl}, but it doesnt work.


Image (in firebug) is now :


<img id="sn-value-ajax-loader-image" src="/jira/download/resources/com.intelek.serialnumberChecker:sn-plugin-images/images/ajax-loader.gif">

But there is no image, and firebug is saying 404. Is atlas-run rerun needed ? or just fastdev will be enough.

TAGS
AUG Leaders

Atlassian Community Events