Template Renderer whats new way to do it?

Hey everyone. I was following this tutorial. But I have problem with Template Renderer. What is new way to do it ? For testing I use directly html tags to print on page. But now I wanna do it in way it does in tutorial.

public class OutPutServlet extends HttpServlet {

private static final Logger log = LoggerFactory.getLogger(OutPutServlet.class);
private static final String TEST_TEMPLATE_FILE = "/templates/test.vm";

 // Example of tutorial
    List<Issue> issues = getIssues(req);
    Map<String, Object> context = Maps.newHashMap();
    context.put("issues", issues);
    // Pass in the list of issues as the context
    templateRenderer.render(LIST_BROWSER_TEMPLATE, context, resp.getWriter());

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
    String p = "</br>List of Projects : </br> ";
    String s = req.getRemoteUser(); // ( It display user )


    if(s == null) {
        resp.getWriter().write("<html><body> You need to LogIn before using Displaying Projects and Issues </body></html>");
    } else {
        // Get all Projects
        List<Project> projects = getAllProjects();
        if (projects != null) {
            for (Project project : projects) {
                p = p + project.getName() + " - " + project.getKey() + " </br> ";
        } else
            p = "Projects == NULL ! ";

        // Print to screen
        resp.getWriter().write("<html><head><style>table, th, td {border: 1px solid black;}</style>"
                + "</head><body>Currently loggedIn USER is : "
                + s
                + "</br>"
                + p
                + "</table></body></html>");
} // End of doGet method!

protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
} // End of doPost method

//  Custom functions
private List<Project> getAllProjects () {
    return ComponentAccessor.getProjectManager().getProjects();

@Mjafko, I think the answer is in the tutorial, isn’t it? You create a template file and render it via the templateRenderer. Have you tried that? Are you saying that it didn’t work? If so, what problems did you encounter?

I did tryed. I add dependency but it looks like TemplateRenderer still cannot be found.

Since you really haven’t given me much information to go on, I can’t give you very specific help. In general terms, you’ll need to component-import the template render in your atlassian-plugin.xml file, or if you are using the Atlassian Spring Scanner, you can use the @ComponentImport annotation instead. You’ll need to have your servlet registered in your atlassian-plugin.xml file too. The TemplateRenderer will need to be a parameter of the servlet’s constructor, so that it can be injected at runtime. You’ll then save the template renderer in an instance variable for use when you render your template.

It would be helpful if you would provide details of the relevant portions of your atlassian-plugin.xml file, and the error that you are seeing when you put it all together.

1 Like

stick this in your atlassian-plugin.xml

<component-import key="templateRenderer" interface="com.atlassian.templaterenderer.TemplateRenderer" />`

and do this in your servlet

Public MyServlet(TemplateRenderer templateRenderer) {
        this.templateRenderer = templateRenderer;

protected void doGet(HttpServletResponse response) {
        templateRenderer.render("vm/my-template.vm", response.getWriter());

But the SDK creates new projects as Spring Scanner enabled, so <component-import> would be ignored. He’d have to use the @Inject and @ComponentImport annotations on his injection instead, or disable Spring Scanner.

you can replace the part about the descriptor file with the annotation in your class

Newer SDK versions force the use of annotations instead of descriptors

public class MyServlet extends HttpServlet {
private final TemplateRenderer renderer;