<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 431,  comments - 344,  trackbacks - 0

    Creating a Grails Plugin in NetBeans IDE

    Let's create a plugin for Grails. Grails is, after all, modular and pluggable. Here's the ultimate simple Grails plugin, just to give an idea what is involved, from start to finish. The most useful references I have found so far are these:

    Between those three, you should have enough to figure things out. I still found it hard, despite those instructions and so to avoid having to figure things out again some time in the future, I'll write absolutely everything here.

    Creating the Plugin

    1. On the command line, run this:
      grails create-plugin SamplePlugin

      Now you have a Grails plugin. However, at the same time it is just another Grails application, which means you can simply open it in NetBeans IDE. (I.e., there is no import process and no NetBeans artifacts are added to the plugin in order to be able to open it in the IDE.)

       

    2. So open the plugin in the IDE. The Projects window isn't very interesting, it just shows you the same as you would normally see for Grails applications:

      The Files window (Ctrl-2) however, shows a lot more:

      Open the "SamplePluginGrailsPlugin.groovy" file and there you see the following:

      class SamplePluginGrailsPlugin {
          def version = 0.1
          def dependsOn = [:]
          def doWithSpring = {
          // TODO Implement runtime spring config (optional)
          }
          def doWithApplicationContext = { applicationContext ->
          // TODO Implement post initialization spring config (optional)
          }
          def doWithWebDescriptor = { xml ->
          // TODO Implement additions to web.xml (optional)
          }
          def doWithDynamicMethods = { ctx ->
          // TODO Implement registering dynamic methods to classes (optional)
          }
          def onChange = { event ->
          // TODO Implement code that is executed when this class plugin class is changed
          // the event contains: event.application and event.applicationContext objects
          }
          def onApplicationChange = { event ->
          // TODO Implement code that is executed when any class in a GrailsApplication changes
          // the event contain: event.source, event.application and event.applicationContext objects
          }
          }

      I.e., you have hooks for integrating your code into meaningful places in the plugin.

       

    3. Now we'll create code that will let our plugin provide a new "constraint". (If you don't know what that is, you will know by the time you finish reading all this.) To do so, we will need to extend org.codehaus.groovy.grails.validation.AbstractConstraint, in a package within src/groovy:
      import org.codehaus.groovy.grails.validation.AbstractConstraint
          import org.springframework.validation.Errors
          class BestFrameworkConstraint extends AbstractConstraint {
          private static final String DEFAULT_MESSAGE_CODE = "default.answer.invalid.message";
          public static final String NAME = "oneCorrectResponse";
          private boolean validateConstraint
          //The parameter which the constraint is validated against:
          @Override
          public void setParameter(Object constraintParameter) {
          if (!(constraintParameter instanceof Boolean))
          throw new IllegalArgumentException("Parameter for constraint ["
          + NAME + "] of property ["
          + constraintPropertyName + "] of class ["
          + constraintOwningClass + "] must be a boolean value");
          this.validateConstraint = ((Boolean) constraintParameter).booleanValue()
          super.setParameter(constraintParameter);
          }
          //Returns the default message for the given message code in the current locale:
          @Override
          protected void processValidate(Object target, Object propertyValue, Errors errors) {
          if (validateConstraint && !validate(target, propertyValue)) {
          def args = (Object[]) [constraintPropertyName, constraintOwningClass,
          propertyValue]
          super.rejectValue(target, errors, DEFAULT_MESSAGE_CODE,
          "not." + NAME, args);
          }
          }
          //Returns whether the constraint supports being applied against the specified type:
          @Override
          boolean supports(Class type) {
          return type != null && String.class.isAssignableFrom(type);
          }
          //The name of the constraint, which the user of the plugin will use
          //when working with your plugin.
          @Override
          String getName() {
          return NAME;
          }
          //Validate this constraint against a property value,
          //In this case, ONLY "Grails" is valid, everything else will cause an error:
          @Override
          boolean validate(target, propertyValue) {
          propertyValue ==~ /^Grails$/
          }
          }

       

    4. Next, back in the Groovy plugin class that we looked at earlier, hook the above class into the plugin, using the "doWithSpring" closure to do so:
      def doWithSpring = {
          org.codehaus.groovy.grails.validation.ConstrainedProperty.registerNewConstraint(
          BestFrameworkConstraint.NAME,
          BestFrameworkConstraint.class);
          }

       

    5. Now, back on the command line, navigate to within the "SamplePlugin" folder. There, run the following:
      grails package-plugin

      Back in the IDE, examine the ZIP file that the above command created:

    That ZIP file is your Grails plugin.

    Installing the Plugin

    Now we will install our plugin in a new application.

    1. First, create a new Grails application by going to the New Project wizard (Ctrl-Shift-N) and choosing Groovy | Grails Application. Click Next and type "SampleApplication" and then click Finish.

       

    2. After the IDE has finished running the "grails create-app" command for you, you will see the new application open in the IDE. Right-click it and choose "Plugins", as shown here:

       

    3. In the Grails Plugins dialog, notice that the list gets filled with many potential plugins that you might want to install, from the Grails plugins repository. Instead, we'll install our own. Click Browse and browse to the ZIP file that we created three steps ago and notice that it appears in the text field at the bottom of the dialog:

       

    4. Click "Install" and then a progress bar appears, ending with the plugin being installed. Notice that you can also uninstall it:

       

    5. Take a look at your application and notice (in the Files window) what's happened to the plugin. It's been unzipped, plus the ZIP file is still there. And all that's been done in the "plugins" folder. Nothing else has changed, which means that uninstallation is as simple as removing the folder from the "plugins" folder:

      Thanks to "convention over configuration", Grails knows exactly where everything is—so that, for example, the "plugin.xml" file that you see above, if found within the folder structure you see above, is the indicator to Grails that a plugin is available for use.

    Using the Functionality Provided By the Plugin

    1. Let's now use our plugin. Create a domain class called "Quiz", after right-clicking the "Domain Classes" node and choosing "Create new Domain Class":

       

    2. Right-click the "Controllers" node and choose "Create new controller". Type "Quiz" and then click Finish. Use the Groovy editor to add one line for adding the scaffolding (and uncomment the other line):

       

    3. Back in the "Quiz" domain class, add your property and use the "oneCorrectResponse" constraint defined in your plugin, as shown here:

      Note: The "oneCorrectResponse" constraint that you see above is the name of the constraint defined in the plugin.

       

    4. And then add the message to the messages.properties file, which is within the "Messages Bundles" node:

       

    5. Run the application and you will see that your constraint will prevent anything other than "Grails" from being considered acceptable, when "Create" is clicked below:

    Congratulations, you've created, installed, and used your first Grails plugin!

    posted on 2008-07-28 11:35 周銳 閱讀(464) 評論(0)  編輯  收藏 所屬分類: Groovy&Grails
    主站蜘蛛池模板: 色五月五月丁香亚洲综合网| 亚洲成人影院在线观看| 水蜜桃亚洲一二三四在线| 一级毛片a女人刺激视频免费| 国产成人啪精品视频免费网| 亚洲JIZZJIZZ妇女| 日本大片在线看黄a∨免费| 亚洲AV无码乱码在线观看性色扶| 亚洲欧美成aⅴ人在线观看| 在线A级毛片无码免费真人| 亚洲AV无码男人的天堂| 国产一级大片免费看| 又长又大又粗又硬3p免费视频| 亚洲成a人无码av波多野按摩| 无码免费又爽又高潮喷水的视频 | 国产精品亚洲专区在线观看| 香蕉视频免费在线播放| 亚洲人成影院在线观看| 手机看片国产免费永久| 久久精品亚洲精品国产色婷 | 成年美女黄网站色大免费视频| 亚洲欧洲日产国码一级毛片| 黄视频在线观看免费| 国产特级淫片免费看| 午夜成人无码福利免费视频| 久久精品国产亚洲网站| 久久午夜免费视频| 亚洲免费在线视频| 色妞WWW精品免费视频| 亚洲国产精品日韩在线| 永久免费观看的毛片的网站| xxxxx做受大片视频免费| 久久久久亚洲AV片无码下载蜜桃| 人禽杂交18禁网站免费| 永久免费观看黄网站| 亚洲精品视频久久| 久久久久久毛片免费播放| 亚洲丁香色婷婷综合欲色啪| 最新中文字幕电影免费观看| 国产免费久久精品99久久| 亚洲人成免费网站|