It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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

Tomas Jakubis Jul 03, 2014

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
Tomas Jakubis Jul 06, 2014
<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
This widget could not be displayed.
This widget could not be displayed.
Community showcase
Posted in United States

Topic Tuesday: Agile

Another week, another Topic Tuesday!  Agile has been on my mind a lot lately because I've seen good implementations and bad implementations, but now as a Jira admin it matters more to me whether...

27 views 0 0
View post

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you