Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
Community Members
Community Events
Community Groups

How to call java method from velocity?

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

I tried use


in java class i have method

public void show() {

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

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.

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:-

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 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!


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 "$" it should be done in the following way


you have to add ! in between $ and

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

        <input class="input" type="button" name="button" value="${line00}" onclick="${v_show}" />

maybe you can help me...

my button:

<input class="button" type="button" name="button" value="$line00" onclick="$v_show" />

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?

		<input class="input" type="button" name="button" value="$line00" onclick="$v_show" />

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?


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

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

Now i use as superclass

extends AbstractSingleFieldType<String>

maybe i should use other superclass?

now i override method

    public Map<String, Object> 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:
<customfield-type name="Field"
		i18n-name-key="" key="field"
		<description key="field.description">Custom Field Plugin
		<resource name="view" type="velocity"
			location="/templates/customfields/field/view.vm" />
		<resource name="edit" type="velocity"
			location="/templates/customfields/field/edit.vm" />

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:


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):

	<h3>it's true</h3>
	<h3>it's false</h3>

A good reference is found here:

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


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="$" />

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 ?

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";

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

		return velocityParameters;


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


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

Suggest an answer

Log in or Sign up to answer

Atlassian Community Events