Calling a Java method from velocity template only if button is clicked

Hey there,

I’m implementing a version manager plugin for jira. At the moment I’m using a webworkAction-class to call my .vm file which gives me an interface on the jira page. From there I want to call a method in the webworkAction-class only when a button is clicked. Here are my questions:

1. The method is automatically called when the page loads, without clicking the button. And isn’t called then when the button is clicked. Is there a way to prevent this?

2. How can I hand over all parameters from the UI (only Strings) with the method? I always get null.

Thanks for your help!

VersionManagerWebworkAction.java:

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

    @Override
    public String execute() throws Exception {
        return "version-manager-success";
    }
    public void createNewFixVersion(String versionName) {
    	//Problem: This method is called when page loaded
    	System.out.println(versionName);
    }
}

version-manager-success.vm:

<!DOCTYPE html>
<html>
<head>
    ...
</head>
<body>
...
<input id="version" style="width:231px;" type="text" name="version">
...
<button onclick="createButtonAction()">Create</button>

<script>
function createButtonAction() {
  if(document.getElementById("version").value.length == 0){
	...
  }else{
	$versionValue = document.getElementById("version").value;
	$!action.createNewFixVersion( $versionValue );
  } 
}
</script>
</body>
</html>

@NiklasGrossmann it seems like you are trying to mix javascript and velocity, which unfortunately is not possible and is what is causing your side-effect.

If we focus on this snipper for a moment:

<script>
function createButtonAction() {
  if(document.getElementById("version").value.length == 0){
	...
  }else{
	$versionValue = document.getElementById("version").value;
	$!action.createNewFixVersion( $versionValue );
  } 
}
</script>

Upon page load, the velocity parts of your template will be parsed by the server. As such, it will execute the $!action.createNewFixVersion( $versionValue ); line. The reason that the $versionValue is null is because you are populating it using Javascript code. The velocity renderer will not understand this and will ignore it.

To communicate with your webwork action from the velocity template, you will need to do a request to the server, either via an HTML form or by using Javascript XHR request (aka AJAX).

The HTML form or XHR request can call a specific command within your webwork action. You can read more about how to configure webwork actions and commands here: https://developer.atlassian.com/server/jira/platform/jira-webwork-actions-4227099/

PS: please note that there is already an excellent Version Manager for Jira app available on the Atlassian Marketplace by @Holger, you could also consider using his!

2 Likes

Second the existing plugin – We use it to great success.

Thanks remie for your reply!

Do you have an example tutorial for handing over parameters from .vm to java?
I tried it myself but didn’t work it out yet.

Thanks :grinning: