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

How to call java method from velocity?

xugab zhenskijmir March 31, 2013

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

Suggest an answer

Log in or Sign up to answer
1 vote
JPB July 15, 2014

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.

1 vote
Graeme Mitchell April 4, 2013

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

xugab zhenskijmir April 7, 2013

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

Sharath T S January 22, 2015

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

0 votes
Sharath T S February 12, 2015

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

0 votes
Hugo Bédard August 6, 2013

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

<html>
    <head>
        <title>Title</title>
    </head>
    <body>
        <input class="input" type="button" name="button" value="${line00}" onclick="${v_show}" />
    </body>
</html>
0 votes
Alex Di April 10, 2013

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?

Alex Di April 10, 2013

maybe problem in velocity?

<html>
    <head>
        <title>Title</title>
    </head>
    <body>
		<input class="input" type="button" name="button" value="$line00" onclick="$v_show" />
	</body>
</html>


Prameesha Samarakoon June 23, 2014

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

0 votes
Alex Di April 10, 2013

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

maybe i should use other superclass?

now i override method

@Override
    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="field.name" key="field"
		class="com.MyCustomField">
		<description key="field.description">Custom Field Plugin
		</description>
		<resource name="view" type="velocity"
			location="/templates/customfields/field/view.vm" />
		<resource name="edit" type="velocity"
			location="/templates/customfields/field/edit.vm" />
	</customfield-type>

0 votes
Nahn Yanootz
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 7, 2013

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

xugab zhenskijmir April 8, 2013

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

xugab zhenskijmir April 8, 2013

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

Nahn Yanootz
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 10, 2013

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

Does it extend the JiraActionSupport class?

We need more details.

Nahn Yanootz
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 10, 2013

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.

TAGS
AUG Leaders

Atlassian Community Events