I’m working on a JIRA plugin that extends AbstractIssueSelectAction and I’m struggling with making custom variables accessible in my velocity templates.
Currently, I can use built-in variables like ${issue.summary} in my templates without any problems. However, I need to expose additional custom objects and variables to the velocity context but I can’t figure out how to add them to the parameter map.
I’ve tried debugging through the code but the call stack is quite deep and much of the webwork implementation appears to be obfuscated, making it difficult to understand the flow.
Here’s my action class:
public class CustomAction extends AbstractIssueSelectAction {
private final Issue relatedIssue;
public String doProcess() throws Exception {
log.debug("Executing doProcess()");
return "processdata";
}
}
And my plugin configuration:
<webwork1 key="custom_key" name="Custom Name" class="java.lang.Object">
<actions>
<action name="com.example.jira.plugins.CustomAction" alias="CustomAction">
<view name="processdata">/templates/custom-processdata.vm</view>
<view name="success">/templates/custom-success.vm</view>
</action>
</actions>
</webwork1>
In my velocity template, ${issue.summary} works fine, but I want to access ${relatedIssue.summary} where relatedIssue is a different issue object I’ve defined in my action class.
How can I make my custom variables available to the velocity template context?
Your relatedIssue field isn’t being exposed to the velocity context automatically. JIRA’s webwork framework only makes public getter methods available to templates. You need to add a public getter method in your CustomAction class: public class CustomAction extends AbstractIssueSelectAction { private Issue relatedIssue; public Issue getRelatedIssue() { return relatedIssue; } public String doProcess() throws Exception { this.relatedIssue = someMethodToGetRelatedIssue(); log.debug(“Executing doProcess()”); return “processdata”; } } Once you’ve got the getter method, ${relatedIssue.summary} should work in your velocity template. Just make sure you populate the relatedIssue field before the template renders or you’ll get null pointer exceptions.
Watch out for initialization timing too. I hit the same problem when my custom objects weren’t ready before the template rendered. Set up your relatedIssue in doExecute() or whatever gets called first - don’t wait for doProcess(). The webwork lifecycle is tricky, and if you initialize too late, the template renders before your getter has anything to return. Check your action’s constructor as well. JIRA’s dependency injection timing can mess with field initialization. I always add defensive null checks in the getter - return a default value or empty object instead of null to avoid template errors.
classic jira webwork issue! the getter approach works, but heads up - don’t put heavy processing in your getter since velocity calls it multiple times during rendering. also check that relatedIssue isn’t null when the template runs. that’s what usually breaks things.