How to call java method from velocity?

I wrote custom field plugin. Now i needed call java method from view.vm

I tried use

$action.show()
$action.show
$show()
$show

in java class i have method

public void show() {
        System.out.println("sample");
    }

but i couldn't call method.

In this method i want chande data in custom field, save to DB and update on page.

Or how do call or how best to implement this method (maybe on jQuery or other)?

7 answers

This is from an issue tab panel, but I think this will be the same on a custom field. In the java class you wish to make available, you would usually have an override to the populateVelocityParams method, something like this:-

@Override
protected void populateVelocityParams(Map params) 
{
	params.put("instance", this);
}

So in this case, if my class (this) had a method call show that I wanted to call, I would use

$instance.show()

Instance of course being the name of the parameter I put in the map

Thanks for reply, but it couldn't call my method

This Worked perfectly fine for me... Thank you Graeme!

Hi guys, I have the same problem, I need to call the method when a button is pressed, not when the velocity is loaded (that's how is working for me right now), the method is executed when the page is loaded, not when the button is pressed, have you figured out a solution for this?

Thank you.

For a method that returns a string (using your example):

This is your method in Java:

public String show() {
		return "sample";
	}

This is how you call it from velocity:

$show

For a method that returns a boolean value:

This is your method in Java:

public boolean isTrueStatement() {
		return true;
	}

This is how you handle it in velocity (without the "is" prefix):

#if($trueStatement)
	<h3>it's true</h3>
#else
	<h3>it's false</h3>
#end

A good reference is found here:

http://velocity.apache.org/engine/releases/velocity-1.5/user-guide.html#methods

thanks, but....

i have a method

public String show() {
    return "sshhooww";
}

also i add velocity param in java class

velParams.put("cls", this);

i was able call meethod from vm only that

$cls.show()

i couldn't call method without velocity param "csl"

Main purpose of my question get knowleges, how call method from button method onClick

<input type="button" name="button" value="value" onClick="$cls.show" />

i found how call method, but i face a question: why called method button.onClick in velocity when i refresh page brouser (f5)

What is the class that contains the show() method?

Does it extend the JiraActionSupport class?

We need more details.

Does your Java class extend a Custom Field Type?

Did you read through https://developer.atlassian.com/display/JIRADEV/How+to+create+a+new+Custom+Field+Type ?

I think you have to override the getVelocityParameters() method and add your object that contains the method using the velocityParameters.put() method.

If all your show() method does is return a string, then just add the returned value from show() as a variable to the velocity parameters map.

public class MyType extends WhateverCFType {

private String show(){
return "bla bla bla";
}

@Override
	public Map<String, Object> getVelocityParameters(Issue issue, CustomField field, FieldLayoutItem fieldLayoutItem) {
		final Map<String, Object> velocityParameters = super.getVelocityParameters(issue, field, fieldLayoutItem);

			velocityParameters.put("someStringVariable",show());
		}
		return velocityParameters;
	}

}

and then from velocity, you just call the someStringVariable simply by doing this:

$someStringVariable

Make sure that your atlassian-plugin.xml contains the <customfield-type>... entry with the corresponding referenced vm files.

Sorry, i lost access to my previous account. \=

yes, i see faq about create custom field, but i see

https://developer.atlassian.com/display/JIRADEV/Creating+a+Custom+Field+in+JIRA

Now i use as superclass

extends AbstractSingleFieldType&lt;String&gt;

maybe i should use other superclass?

now i override method

@Override
    public Map&lt;String, Object&gt; getVelocityParameters(Issue issue, CustomField field, FieldLayoutItem fieldLayoutItem) {
        velParams = super.getVelocityParameters(issue, field, fieldLayoutItem);

        if (!velParams.containsKey("cls")) {
            velParams.put("line00", "ButonName");
            velParams.put("cls", this);
            velParams.put("v_show", show());
        }
        return velParams;
    }
and in atlassian-plugin.xml present note:
&lt;customfield-type name="Field"
		i18n-name-key="field.name" key="field"
		class="com.MyCustomField"&gt;
		&lt;description key="field.description"&gt;Custom Field Plugin
		&lt;/description&gt;
		&lt;resource name="view" type="velocity"
			location="/templates/customfields/field/view.vm" /&gt;
		&lt;resource name="edit" type="velocity"
			location="/templates/customfields/field/edit.vm" /&gt;
	&lt;/customfield-type&gt;

maybe you can help me...

my button:

&lt;input class="button" type="button" name="button" value="$line00" onclick="$v_show" /&gt;

i set breakpoint on method show.

when i click on button -- method not called, and if i refresh page with field -- method called twice. why?

maybe problem in velocity?

&lt;html&gt;
    &lt;head&gt;
        &lt;title&gt;Title&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
		&lt;input class="input" type="button" name="button" value="$line00" onclick="$v_show" /&gt;
	&lt;/body&gt;
&lt;/html&gt;


Hi Alex,

I have a similar requirement and seem to be having the same problem, The method gets called twice but not on the button click. Did you figure out a solution for this?

Thanks

I may be wrong, but I believe your variables should be written this way when you want to add them to a tag attribute :

&lt;html&gt;
    &lt;head&gt;
        &lt;title&gt;Title&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
        &lt;input class="input" type="button" name="button" value="${line00}" onclick="${v_show}" /&gt;
    &lt;/body&gt;
&lt;/html&gt;

Guys,

You all were doing it wrong.

Velocity use a different language called VTL.

if you want to call anything from inside apostrophe("....")  you cannot just call like "$cls.show" it should be done in the following way

"$!cls.show" 

you have to add ! in between $ and cls.show

Suggest an answer

Log in or Sign up to answer
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Teagan Harbridge
Published Wednesday in Agile

Top 10 Insights from the 12th Annual State of Agile Report

...challenges organisation’s experience in their efforts to adopt and scale agile.   Many people associate agility with complete flexibility. And that’s not necessarily true, as demonstrated by this finding....

54 views 1 2
Read article

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