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

How do I run a Java method from a simple velocity file?

Adam Steele May 4, 2015

I've looked around and seen a couple of other topics related to this, but none of the solutions presented seemed to fix my issue.  I used the command line to generate a basic plugin that adds a new project panel, and I'm attempting to call a method from the generated Java file.

My atlassian-plugin.xml includes this:

<component key="myPluginComponent" class="com.atlassian.jira.MyPluginComponentImpl" public="true">
    <interface>com.atlassian.jira.MyPluginComponent</interface>
</component>

And my sample method is stubbed in MyPluginComponent

String show();

And is implemented in MyPluginComponentImpl as:

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

I've tried calling it from Velocity using $show, $show(), $action.show, $action.show(), $myPluginComponent.show, and $myPluginComponent.show()

 

I have also tried overriding the getVelocityParameters function to add ("instance", this) and calling $instance.show and $instance.show().  So far everything I has tried has just returned my method call as a literal

For example, this just gets me:

Testing...

$show

<div class="mod-header">
	#set($message = "Testing...")
	$message
    $show
</div>


1 answer

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

2 votes
Answer accepted
Volodymyr Krupach
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.
May 4, 2015

A velocity template can call methods from its webwork action. So add getter in your webwork action that returns MyPluginComponent and then you can call any MyPluginComponent method from the template:

// webwork action class
public getMyPluginComponent() {
  return myPluginComponent; // myPluginComponent instance should be injected to the webwork action as constructor parameter
}
 
// template
<div class="mod-header">
    #set($message = $action.getMyPluginComponent().show())
    $message
</div>
Adam Steele May 5, 2015

Hmm. This is the first I've heard of webwork actions in all this. I'm checking out a couple of guides there now... I'm not sure I understand the purpose of the class outlined under component in the atlassian-plugin.xml file if you have to set up a webwork action for any Java function calls on the velocity page. What is it used for?

Volodymyr Krupach
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.
May 5, 2015

What type of the plugin are you developing?

Adam Steele May 5, 2015

The eventual goal is a Project Panel Plugin. I'm hoping to extend this to display some gadgets I developed down the line, but for now I'm just trying to understand how to make Java calls from the Velocity template that's displayed in my panel.

Volodymyr Krupach
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.
May 5, 2015

My answer was for the webwork plugin. Sorry for the confuse. For the Project Panel Plugin you are expected to override getHtml from ProjectTabPanel interface. In case you extend from GenericProjectTabPanel, it already overrides getHtml and parses velocity template like this: public String getHtml(BrowseContext ctx) { final Map<String, Object> startingParams = JiraVelocityUtils.getDefaultVelocityParams(authenticationContext); startingParams.put("i18n", authenticationContext.getI18nHelper()); startingParams.put("project", ctx.getProject()); startingParams.put("fieldVisibility", fieldVisibilityManager); return descriptor.getHtml("view", startingParams); } You can override getHtml to pass one parameter: startingParams.put("show", myPluginComponent().show());

Adam Steele May 5, 2015

There we go! Awesome, thanks for your help. :)

TAGS
AUG Leaders

Atlassian Community Events