I am quite new to the Velocity Template / jQuery part, therefore I would like to know how to pass data from a form’s post method to a js function. In a plugin
I am trying to initialise a Banner which has its content set by the form. The form triggers an overriden doPost method from my servlet.
Yes that is correct.
Before refresh, it refreshes so the newly posted banner can be included after the refresh.
I currently have the user input in a variable called “bannerBody”, which is also available for my do Post method:
@Override
protected void doPost(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
networkService.redirectUnauthorizedUsers(req, resp);
System.out.println(req.getParameter("bannerBody")); // this prints out exactly what the user put in in Wiki format
resp.sendRedirect("myPluginURL");
}
Also do you know a good way to convert this WikiMarkup to plain HTML as well? Because I think that is what the banner needs.
I have managed to get the Method to trigger. Main problem was that I was using an <input> tag instead of a <button> tag, changing it made the method trigger.
Also I was mixing up / misunderstanding the different possibilities here.
Submitting the form itself does what the doPost() Method from the servlet tells it to do, which is separate from the method I wanted to trigger, which was in a JavaScript file.
The solution:
First of all import the JavaScript file properly, this can be done in 2 ways in a Velocity Template:
Now you can assign an id to the submit button where you want the method to trigger.(In this case this button is in a form, but this will also work outside of forms)
What I am trying to do right now is to have the JavaScript method return a value which should override the value given to the doPost() Method:
AJS.$(document).ready(function () {
jQuery("#my-plugin-submit-button").click(function (bannerBody) {
jQuery.ajax({
contentType: 'application/json',
type: 'POST',
url: '/rest/api/1.0/render',
async: 'false',
data: '{"rendererType":"atlassian-wiki-renderer","unrenderedMarkup:bannerBody,"issueKey":""}',
success: function (response) {
bannerBody = response; // this is what I thought would update the value with what the response's return value is
},
error: function (response) {
console.log(response);
}
});
});
});
But this ends up using the following URL: http://localhost:2990/jira/plugins/servlet/company/myPlugin/rest/api/1.0/render which does not work. @tjoy as you mentioned the render method is barely documented
I would like to add to this for future reference. I have observed that in some cases where one uses a context name separated by dots like : de.my.com.context it fails to load resources properly.
Re: wrong URL using jquery.ajax (jQuery 1.8.3 - version will probably change with products and their versions)
With a / at the start of the URL string, it’ll replace the entire path in when performing the request and without the forward-slash, the path will be relative to the current location. Our products could have a context path (e.g /jira at the start of the path) so it’s best to set the request URL to something like; (in a template string) /${AJS.contextPath()}/${RENDERER_ENDPOINT}
I’m not sure which versions of the products you’re working with, but we’re moving contextPath() function to the WRM.
The web-resource key you’ll probably want to require is: "wr!com.atlassian.plugins.atlassian-plugins-webresource-plugin:context-path" and you’ll want to require 'wrm/context-path'
thank you for your answer, I am sure it might help out someone in the future.
I have since then moved on in this implementation in favour of an another, therefore am not exactly able to test your suggestion for this section.
Template being rendered from a servlet which then in turn requires the context
This plugin I’m testing with has CSS too which is loading properly. This is using the WRM webpack plugin which outputs a webresource definition with the CSS file under the same webresource. The resources are even being required by using the context name with dots in it. If you can think of anything else that’d be great, but I understand if you’ve moved on now.
thank you for trying to reproduce the issue and your effort.
I will try to provide something useful (hopefully) soon when I have the capacity. No worries about the reply speed, we both have the email notifications off : )