Cannot access webResources from the velocity template generated by my Confluence servlet plugin

Hello everyone,

I have created a confluence macro that uses velocity templates with webResources without any issues. Now I'm trying to create a servlet page, but when I render the same velocity file, my javascript file is never included. I'm probably wrongly including webResourceManager to my servlet or velocity context, but I cannot figure out the correct way, how to do it.

Here is my velocity file:

<html>
<head>
$webResourceManager.requireResource("mygroupid.ServletTest:myresourcekey")
</head>
<body>
<h1>hello world</h1>
<p>$testname</p>
</body>
</html>

here is my javascript file, but it is never included in my servlet:

AJS.toInit(function() {alert("hello world");});

here is my working macro:

public class MacroTest implements Macro
{
	@Override
	public String execute(Map<String, String> params, String body,
			ConversionContext conversionContext) throws MacroExecutionException {
		
		Map<String, Object> context = MacroUtils.defaultVelocityContext();
		context.put("testname", "testvalue");
		return VelocityUtils.getRenderedTemplate("st.vm", context);
	}
	@Override
	public BodyType getBodyType() {
		return BodyType.NONE;
	}
	@Override
	public OutputType getOutputType() {
		return OutputType.BLOCK;
	}
}

Here is my servlet code, that renders the velocity template, but the javascript is not working:

public class ServletTest extends HttpServlet {
	
	private final TemplateRenderer renderer;
	private final WebResourceManager webResourceManager;
	
	public ServletTest(TemplateRenderer renderer, WebResourceManager webResourceManager){
		this.renderer = renderer;
		this.webResourceManager = webResourceManager;
	}
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		Map<String, Object> context = new HashMap<String, Object>();
		context.put("testname", "testvalue");
		context.put("webResourceManager", webResourceManager);
		response.setContentType("text/html;charset=utf-8");
		renderer.render("st.vm", context, response.getWriter());
    }
}

Servlet result is:

<html>
<head>

</head>
<body>
<h1>hello world</h1>
<p>testvalue</p>
</body>
</html>

atlasian-plugin.xml:

<web-resource key="myresourcekey" name="ServletTest Web Resources">
        <dependency>com.atlassian.auiplugin:ajs</dependency>
        <resource type="download" name="ServletTest.js" location="/js/ServletTest.js"/>
        <context>ServletTest</context>
    </web-resource>
    
    <!-- import from the product container -->
    <component-import key="renderer" interface="com.atlassian.templaterenderer.TemplateRenderer" />

    <xhtml-macro name="macrotest" class="mygroupid.MacroTest"
		key="Macro-test">
		<parameters>
		</parameters>
	</xhtml-macro> 
    <servlet name="servlettest" key="servlettest"
		class="mygroupid.ServletTest">
		<description>descr</description>
		<url-pattern>/servlettest</url-pattern>
	</servlet>

my dependencies:

<dependency>
			<groupId>com.atlassian.plugins</groupId>
			<artifactId>atlassian-plugins-webresource</artifactId>
			<version>3.0.2</version>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>servlet-api</artifactId>
			<version>2.4</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>org.apache.httpcomponents</groupId>
			<artifactId>httpclient</artifactId>
			<version>4.1.1</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>com.atlassian.templaterenderer</groupId>
			<artifactId>atlassian-template-renderer-api</artifactId>
			<version>1.0.5</version>
			<scope>provided</scope>
		</dependency>

1 answer

1 accepted

1 vote
Answer accepted
<meta name="decorator" content="atl.general" />

I added this to my velocity file and web resources are working now. I did not know a decorator is required for webResourceManager. Anyway, it solved my problem. I hope this helps anyone with similiar issues.

Edit: I forgot to mention, that I completely removed webResourceManager from my code. Now my servlet looks more like the one in this tutorial https://developer.atlassian.com/display/DOCS/Creating+an+Admin+Configuration+Form

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted Friday in United States

Confluence Security Advisory

Good morning Members, Not sure if you are aware. Please read the following: https://www.icloud.com/keynote/0RyJ4VEdNUpjDpGfMhfZx9fEg#Confluence_Security_Advisor More details: https://co...

61 views 1 0
View post

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