overview | download | blog |
Headless Jumpstart Application - Part 1OverviewIn this guide, we'll take a look at creating a minimal p2web application using the CORE CloudStack module. The application will be injected into the framework as a plug-in using an Eclipse extension and will use the CloudStack MVC model to define a simple page using a controller, action, and view. PrerequisitesIf you haven't already, you'll need to download CloudStack first. Installing the headless jumpstart package
Running the headless jumpstart package
Extending the headless jumpstart packageNow that you have the jumpstart package installed and running, we'll start building a simple p2web application using the built-in MVC model:
In order to create a p2web plug-in, we need to define a controller, one or more actions under that controller, and define a velocity template file (view) for each action. By default, the framework follows a REST approach to URL naming. The controller name is used to create a web application context and the action name is used to create the action servlet. The Controller class must be derived from org.cloudstack.p2webserver.ApplicationController, which among other things sets the correct class loader for jetty. The controller class also adds the specified actions as servlets and is also responsible for setting up the default template directory. The Action classes must be derived from org.cloudstack.p2webserver.ActionServlet, which reads the Velocity template from the default template directory. The action class is responsible for providing the Velocity Context that provides the variable definitions referenced in the template. Create a new plug-in project and add CloudStack dependenciesP2Web applications are essentially a set of plug-ins so the first step is creating a new plug-in project.
Define a controller extension pointYour application contexts are injected into the framework using Eclipse extensions. CloudStack defines a controllers extension point: org.cloudstack.p2webserver.controllers for this purpose.
Let's finish defining the controller by switching to the plugin.xml tab and adding the following: <extension point="org.cloudstack.p2webserver.controllers"> <controller id="com.acme.p2webapp.dashboard" name="dashboard" class="com.acme.p2webapp.controllers.DashboardController"> <action id="show" name="show" class="com.acme.p2webapp.controllers.ShowDashboardAction"> </action> </controller> </extension> This snippet above defines a controller and a single action that resolves to the URL: /dashboard/show Define DashboardController ClassCreate the DashboardController Class under the com.acme.p2webapp.controllers package. The DashboardController class is empty in this example:
package com.acme.p2webapp.controllers;
import org.cloudstack.p2webserver.ApplicationController;
public class DashboardController extends ApplicationController {
}
Define the ShowDashboardAction classThe ShowDashboardAction class creates the Velocity Context used for the Velocity template. The Velocity template is automatically loaded by the base class. It looks for the template in the default template directory (../views) using the servlet's request URI to generate a template name. In this example, the template is loaded from ../views/dashboard/show.vm.
package com.acme.p2webapp.controllers;
import java.util.Calendar;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.context.Context;
import org.cloudstack.p2webserver.ActionServlet;
public class ShowDashboardAction extends ActionServlet {
private static final long serialVersionUID = 1L;
protected Context createContext(
javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response) {
VelocityContext context = new VelocityContext();
context.put("product", "CloudStack");
context.put("time", Calendar.getInstance().getTime().toString());
return context;
}
}
Create template fileFinally we need to create the template file show.vm under the /views/dashboard folder which you'll need to create. The velocity template file is shown below: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>$product</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <link href="/styles.css" rel="stylesheet" type="text/css" media="screen"> </head> <h2><span>Dashboard Page</span></h2> <p> Project filesYou should end up with a project structure like the following:
Running your p2web appWe need to reopen the run configuration and add your plug-in:
Now open a web browser and navigate to [your_cloudstack_page]/dashboard/show or http://localhost:57900/dashboard/show. Congrats! You've just deployed your first p2web application.
|
|
| Copyright ©2002-2008 WiredReach, Inc. All rights reserved. info@wiredreach.com | |