I'm writing a JIRA plugin and I need to render a velocity template that I have written. My code is in src/main/package/MyClass.java and my template is in /src/main/resources/templates/packageName/template.vm. How should I render the template from MyClass.java so I can get the String contents of it?
Community moderators have prevented the ability to post new answers.
Hello Bob.
It depends on what kind of plugin you develop.
If you develop the servlet it looks like:
public void doFilter(ServletRequest request,ServletResponse response,FilterChain chain) throws IOException, ServletException { final Map<String, Object> context = new HashMap<String, Object>(); context.put("yourVarName", "Your Value"); response.setContentType("text/html;charset=utf-8"); templateRenderer.render("templates/packageName/template.vm", context, response.getWriter()); }
But if its Web Action for example so you need to configure the atlassian-plugin.xml
Like the following:
<webwork1 key="actions" name="Name" class="java.lang.Object"> <actions> <action name="package.MyClass" alias="MyClass"> <view name="success">templates/packageName/template.vm</view> </action>
In your first example you use templateRenderer without ever declaring it. How do I get an instance of it? Also, can you give me a link that describes what a web action is? Thanks
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
TemplateRenderer you must initiate via the constructor like this: import com.atlassian.templaterenderer.TemplateRenderer; public class YourClass { private final TemplateRenderer templateRenderer; public YourClass(TemplateRenderer templateRenderer) { this.templateRenderer = templateRenderer; } } Also you need to import it in the atlassian-plugin.xml <component-import key="renderer" interface="com.atlassian.templaterenderer.velocity.one.six.VelocityTemplateRenderer"/> Here is the link for WebAction documentation. https://developer.atlassian.com/jiradev/jira-architecture/building-jira-add-ons/jira-plugins2-overview/jira-plugin-module-types/webwork-plugin-module
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Eclipse is telling me that, "The import com.atlassian.templaterenderer cannot be resolved". I have added the line to my atlassian-plugin.xml as well.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Add a dependency <dependency> <groupId>com.atlassian.templaterenderer</groupId> <artifactId>atlassian-template-renderer-api</artifactId> <version>1.3.1</version> <scope>provided</scope> </dependency> Into your pom.xml
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I added that to pom.xml and I still get the same error in Eclipse. When I build the project using the atlas-mvn command, the error I get is: "cannot find symbol class TemplateRenderer"
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Could you show your java class, atlassian-plugin.xml and pom.xml dependencies?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.