Get a list of projectnames in velocity

Hi am new to JIRA Development and have encountered my first Problem

I need to get a List of all Jira-Projects in something like a dropdown menu

This is my Java Code which simply renders the velocity

templateRenderer.render("serviceTime.vm", response.getWriter());

In my Velocity the code looks like this

<div class="field-group">
   <label for="dProj">Project</label>
      <select class="select" id="dProj" name="Projects">
  	#foreach ($pr in $projectManager.getProjectObjects())
                  <option>$pr.getName()</option>
        #end    
     </select>
</div>

But my selectmenu remains empty

do i miss something here ?

when i do a simple check in JAVA like...

for(int i = 0;i < projects.size(); i++){
        	
        	System.out.println(" Project : " + projects.get(i).getName());
        	
        }

it works, why not in the velocity

Best Regards

1 answer

1 accepted

Your velocity template probably does not know about $projectManager. You have to add a reference to a ProjectManager to the velocity context.

What class is templateRenderer? Can it add objects to the velocity context?

I would use com.atlassian.jira.template.VelocityTemplatingEngine for that, e.g.:

import static com.atlassian.jira.template.TemplateSources.file;

VelocityTemplatingEngine templatingEngine = ComponentAccessor.getComponent(VelocityTemplatingEngine.class); // or constructor injection

Map<String, Object> velocityParams = ComponentAccessor.getVelocityParamFactory().getDefaultVelocityParams();
velocityParams.put("projectManager", ComponentAccessor.getProjectManager());

templatingEngine.render(file(pathToYourTemplate))
                .applying(velocityParams)
                .asPlainText(response.getWriter()); // or asHtml(...)

i was using com.atlassian.templaterenderer.TemplateRenderer

but i got it now thank u...did not know the velocity needs a reference

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Posted Tuesday in Uncategorized

Friday fun: how many celebrates Midsummer holiday or is this a Swedish tradition only?

Any other country that celebrates Midsummer holiday (this friday 22 June)?  

40 views 3 1
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you