Your plugin now has a sophisticated look and feel. In this last section of the tutorial you'll make your plugin fully functional. You'll add a POST
method to the admin.vm template so you can enter and retrieve user data.
When you added component import modules to your plugin, you added PluginSettingsFactory
. This import allows you to use PluginSettings
, a SAL service that manages your plugin data storage. The POST
method you add to your admin.vm template will submit the form to the database, and PluginSettings
permits you to retrieve stored submissions from the database.
POST
method to your admin.vm templateThis is one of the most common HTTP requests. In this step, you'll add a line of code to your admin.vm file.
Open your admin.vm file from src/main/resources
.
Add a POST
method in the <form>
tag.
1 2<html> <head> <title>MyServlet Admin</title> <meta name="decorator" content="atl.admin"> </head> <body> <form id="admin" class="aui" action="" method="POST"> <div class="field-group"> <label for="name">Name:</label> <input type="text" id="name" name="name" class="text"> </div> <div class="field-group"> <label for="age">Age:</label> <input type="text" id="age" name="age" class="text"> </div> <div class="field-group"> <input type="submit" value="Save" class="button"> </div> </form> </body> </html>
Save and close the file.
In this step you'll update the dependencies in your pom.xml
and MyPluginServlet
so you can store and retrieve data.
Open MyPluginServlet.java
.
Replace the contents of the class with the following.
1 2package com.atlassian.plugins.tutorial.refapp; import java.util.HashMap; import java.util.Map; import java.io.IOException; import java.net.URI; import javax.servlet.*; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.atlassian.plugin.spring.scanner.annotation.imports.ComponentImport; import com.atlassian.plugin.spring.scanner.annotation.component.Scanned; import javax.inject.Inject; import com.atlassian.sal.api.auth.LoginUriProvider; import com.atlassian.sal.api.user.UserManager; import com.atlassian.templaterenderer.TemplateRenderer; import com.atlassian.sal.api.pluginsettings.PluginSettings; import com.atlassian.sal.api.pluginsettings.PluginSettingsFactory; @Scanned public class MyPluginServlet extends HttpServlet { private static final String PLUGIN_STORAGE_KEY = "com.atlassian.plugins.tutorial.refapp.adminui"; @ComponentImport private final UserManager userManager; @ComponentImport private final LoginUriProvider loginUriProvider; @ComponentImport private final TemplateRenderer templateRenderer; @ComponentImport private final PluginSettingsFactory pluginSettingsFactory; @Inject public MyPluginServlet(UserManager userManager, LoginUriProvider loginUriProvider, TemplateRenderer templateRenderer, PluginSettingsFactory pluginSettingsFactory) { this.userManager = userManager; this.loginUriProvider = loginUriProvider; this.templateRenderer = templateRenderer; this.pluginSettingsFactory = pluginSettingsFactory; } @Override public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { String username = userManager.getRemoteUsername(request); if (username == null || !userManager.isSystemAdmin(username)) { redirectToLogin(request, response); return; } Map<String, Object> context = new HashMap<String, Object>(); PluginSettings pluginSettings = pluginSettingsFactory.createGlobalSettings(); if (pluginSettings.get(PLUGIN_STORAGE_KEY + ".name") == null){ String noName = "Enter a name here."; pluginSettings.put(PLUGIN_STORAGE_KEY +".name", noName); } if (pluginSettings.get(PLUGIN_STORAGE_KEY + ".age") == null){ String noAge = "Enter an age here."; pluginSettings.put(PLUGIN_STORAGE_KEY + ".age", noAge); } context.put("name", pluginSettings.get(PLUGIN_STORAGE_KEY + ".name")); context.put("age", pluginSettings.get(PLUGIN_STORAGE_KEY + ".age")); response.setContentType("text/html;charset=utf-8"); templateRenderer.render("admin.vm", response.getWriter()); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse response) throws ServletException, IOException { PluginSettings pluginSettings = pluginSettingsFactory.createGlobalSettings(); pluginSettings.put(PLUGIN_STORAGE_KEY + ".name", req.getParameter("name")); pluginSettings.put(PLUGIN_STORAGE_KEY + ".age", req.getParameter("age")); response.sendRedirect("test"); } private void redirectToLogin(HttpServletRequest request, HttpServletResponse response) throws IOException { response.sendRedirect(loginUriProvider.getLoginUri(getUri(request)).toASCIIString()); } private URI getUri(HttpServletRequest request) { StringBuffer builder = request.getRequestURL(); if (request.getQueryString() != null) { builder.append("?"); builder.append(request.getQueryString()); } return URI.create(builder.toString()); } // This is what your MyPluginServlet.java should look like in its final stages. }
Save and close MyPluginServlet
.
The final step is to verify that your plugin can store and retrieve data. Here you'll make an entry and confirm that you can look it up using the PLUGIN_STORAGE_KEY
. You'll perform your lookups in the Plugin Data Console.
Reload your plugin and return to localhost:2990/jira/plugins/servlet/test.
Make an entry such as Charlie/34 for Name/Age and click Save.
Navigate to the Plugin Data Editor. You can navigate directly to the editor at localhost:2990/jira/plugins/servlet/pde
Alternatively you can click Question Mark > Developer Toolbox > Plugin Data Console.
Under Plugin Settings Query, choose Global.
For the Data Key, enter com.atlassian.plugins.tutorial.refapp.adminui.name
to query the names entered, or com.atlassian.plugins.tutorial.refapp.adminui.age
to query the ages entered.
This is the PLUGIN_STORAGE_KEY
value that you coded into your MyPluginServlet
class.
Click Lookup.
You'll see your entries displayed in the Results section.
You've created a plugin complete with a GUI that accepts and stores data - no minor feat. You might consider learning more about developer tools, creating plugin tests, or perusing the FAQ:
Rate this page: