How can I add new webresource to a servlet

hi!
I had some problems of add my css and js file to the volicity,this is my code:
the vm file:

the xml file:


the java code:

@Scanned
public class Login extends HttpServlet {
	private static final Logger log = LoggerFactory.getLogger(Login.class);
	@ComponentImport
	private TemplateRenderer templateRenderer;
	@ComponentImport
	private ProjectService projectService;
	@ComponentImport
	private PageBuilderService pageBuilderService;
	@ComponentImport
	private WebResourceManager webResourceManager;

	@Inject
	public Login(ProjectService projectService, TemplateRenderer templateRenderer,
			PageBuilderService pageBuilderService,WebResourceManager webResourceManager) {
		this.projectService = projectService;
		this.templateRenderer = templateRenderer;
		this.pageBuilderService = pageBuilderService;
		this.webResourceManager =webResourceManager;
	}

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		System.out.println("myClass=" + templateRenderer);
		System.out.println("projectService=" + projectService);
		// resp.setContentType("text/html");
		// resp.getWriter().write("<html><body>Hello World</body></html>");
		pageBuilderService.assembler().resources().requireWebResource("com.jingao.NubiaLogin:NubiaLogin-resources");
		System.out.println("pageBuilderService="+pageBuilderService);
		Map<String, Object> context = new HashMap<>();
		context.put("webResourceManager", webResourceManager);
		templateRenderer.render("templates/nubia-login/input.vm",context, resp.getWriter());
	}
}

the result:

the webresource doesn’t seems to be loaded.Anyone can help me to solved the problem,Thanks a lot!
I learn the code form https://developer.atlassian.com/server/jira/platform/web-resource/
Thank you!

Another way to load your webresource in your vm file is to use the following line:

$webResourceManager.requireResource("com.jingao.NubiaLogin:NubiaLogin-resources")

instead of your $webResourceManager.requireResourceForContext("NubiaLogin").

com.jingao.NubiaLogin is your plugin key and NubiaLogin-resources is the key of your web-resource.

2 Likes

Thank you very much jeffrey.peeters!
But it still not work for my code.Do you have any example code or link?
Thanks a lot!

it might be that your plugin key is wrong. You can check to see what your plugin key is in your pom.xml file. This is done by adding groupId and the artifactId fields together. In my case it looks like this:

<groupId>tig.jira.extension</groupId>
    <artifactId>tigChecklistExtension</artifactId>

which would make tig.jira.extension.tigChecklistExtension as my plugin key

Thank you jeffrey.peeters!
This is my pom.xml file info,The key is correct:
com.jingao
NubiaLogin

Through the code webResourceManager.getRequiredResources() can get the Resource path

"/jira/s/d41d8cd98f00b204e9800998ecf8427e-CDN/-3ed50n/73013/b6b48b2829824b869586ac216d119363/1.0.0-SNAPSHOT/_/download/batch/com.jingao.NubiaLogin:NubiaLogin-resources/com.jingao.NubiaLogin:NubiaLogin-resources.css

fianlly I use dynamic js code at the vm file to solve my problem,the code like:

and add the code at servlet like:
public void getLoginCss(Map<String, Object> context) {
pageBuilderService.assembler().resources().requireWebResource(“com.jingao.NubiaLogin:NubiaLogin-resources”);
Pattern pattern = Pattern.compile("<link.*NubiaLogin-resources");
Matcher matcher = pattern.matcher(webResourceManager.getRequiredResources());
String cssurl = null;
if (matcher.find()) {
cssurl = matcher.group();
pattern = Pattern.compile(“href=.*css”);
matcher = pattern.matcher(cssurl);
if (matcher.find()) {
cssurl = matcher.group();
System.out.println(“cssurl=” + cssurl);
context.put(“cssurl”, cssurl.replace(“href=”", “”));
}

	}
}

I still do not know why used $webResourceManager.requireResource(“com.jingao.NubiaLogin:NubiaLogin-resources”) does not work.

Have someone a solution for this?
I’m unable to load a webresource when rendering a velocity template from a servlet.

Don’t use the WebResourceManager. Its deprecated, you should use the PageBuilderService instead. I don’t know why everyone here suggest to use the WebResourceManager…

Take a look here:

https://jira.atlassian.com/browse/JRASERVER-38808

https://developer.atlassian.com/server/jira/platform/jira-7-0-api-changes/#atlassian-plugins-webresource-module-deprecated-and-may-be-removed-in-7-x

2 Likes