访问内部的Grails模块 [英] accessing internal Grails modules

查看:85
本文介绍了访问内部的Grails模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的团队建议对我们当前的Grails项目进行模块化处理,因此我们进行了试验。我们已经开始通过IntelliJ做到这一点:

 项目> MyProject _right-click_新> Module ..... create-app 

然后我们移动每个先前存在的控制器服务查看到它们的具体模块



当我们运行项目时(通过 MyProject run-app -reloading ),之前可访问的页面会返回错误404 。当然,我们相信这是因为项目目录被重新组织。我们尝试访问这些旧的 url

  http: // localhost:8080 / MyProject / someController 

添加到这个新的 url code>,假设它只需追加模块名称

  http:// localhost:8080 / MyProject / the-new-module-name / someController 
^ ^ ^ ^ ^ ^ ^ ^ ^ ^

但它仍然不起作用。我们如何设想运行该项目以及它的模块,我们如何访问它们?我们是否需要配置 .config .property 文件?



这是新项目目录的样子:

  MyProject 
+ .idea
+ grails-app
+ lib



- 新模块
+ .idea
+ grails-app
+ lib



+ web-app
+ wrapper



+ web-app
+ wrapper

编辑:团队希望实现这种模块化方法的原因是,我们当前的Grails项目可以进一步细分为更小,独立的项目(用于可维护性等)。现在,我们已经设法创建(并运行)这些较小的项目。巩固这些小块的问题出现了。所以我们的任务是创建一个父Grails项目(甚至可能)。

解决方案

假设InteliJ模块是Grails所理解的。它没有。



制作模块化Grails应用程序的正确方法是插件。 Grails对创建自己的插件有很大的支持。你甚至可以创建内联插件,这大大加快了开发速度。我强烈建议您阅读关于插件创建和使用的官方文档在您的Grails应用程序中。



请记住,您的插件几乎与您的应用程序(服务,域类,控制器,GSP等)具有相同的工件,但将出现在相同的应用程序下,就像使用该插件的应用程序提供工件一样。例如,如果您的插件中包含 BookController ,它将具有相同的URL映射,就好像控制器一样是您的基本应用程序的一部分。您在问题中指出,您希望为这些控制器提供由您的插件提供的不同URL。如果是这种情况,那么你需要命名空间你的控制器。您也可以在官方文档中阅读更多内容。
$ b

更新

为了帮助可视化插件如何使用模块化Grails申请:

  /〜/ MyApp 
/〜/ PluginA
/〜/ PluginB

MyApp 是主应用程序(使用<$ c $创建c> grails create-app )。它具有它自己的功能,并且包含 PluginA PluginB (均使用创建)> grails create-插件)将自己的功能添加到自己。您可以选择使用内联插件,甚至打包插件并将它们发布到您自己的内部存储库供其他应用程序使用(本例中为 MyApp )。请注意,这三个部分是分开的。它们可以彼此独立修改,在源代码管理中进行版本控制,管理,开发等。


My team suggests to modularize our current Grails project, so we experimented. We've started doing it through IntelliJ:

Projects > MyProject _right-click_ New > Module ..... create-app

Then we move each previously existing domain, controller, services, and view to their specific module.

When we run the project (through MyProject run-app -reloading), the pages that previously were accessible returns error 404. Now of course, we believe that is because the project directory is restructured. We attempt to access those pages from this old url:

http://localhost:8080/MyProject/someController

to this new url, assuming that it only needs to append the module name

http://localhost:8080/MyProject/the-new-module-name/someController
                                ^ ^ ^ ^ ^ ^ ^ ^ ^ ^

But it still doesn't work. How do we suppose to run the project along with it's module and how can we access them? Do we need to configure some .config and .property files?

This is what the new project directory looks like:

MyProject
    +.idea
    + grails-app
    + lib
    .
    .
    .
    - the-new-module
        +.idea
        + grails-app
        + lib
        .
        .
        .
        + web-app
        + wrapper
    .
    .
    .
    + web-app
    + wrapper

edit: The reason the team wants to implement this "modular" approach is that our current Grails project can still be subdivided further into smaller, independent projects (for maintainability, etc.). Now, we have manage to create (and run) these smaller projects. The problem arises on consolidating these smaller pieces. So the task is to create a parent Grails project (is that even possible).

解决方案

It would appear you're making the assumption that InteliJ "modules" are something that Grails understands. It does not.

The correct approach to making a modular Grails application is "plugins". Grails has great support for creating your own plugins. You can even create inline plugins which greatly speed up development. I highly recommend you read the official documentation regarding plugin creation and use in your Grails application.

Keep in mind that your plugin can have almost all the same artifacts as your application (services, domain classes, controllers, GSPs, etc.) but will appear under the same application as if the application using the plugin was providing the artifacts.

For example, if you have the BookController in your plugin it will have the same URL mapping as if the controller were a part of your base application. You indicate in your question that you wish to have different URLs for these controllers which are provided by your plugins. If that's the case then you need to namespace your controllers. You can read more about that in the official documentation too.

Update

In order to help visualize how plugins work with modular Grails applications:

/~/MyApp 
/~/PluginA 
/~/PluginB

MyApp is the main application (created using grails create-app). It has it's own functionality and includes both PluginA and PluginB (both created using grails create-plugin) to add their functionality to itself. You have the option of using inline plugins or even packaging the plugins and publishing them to your own internal repository for use by other applications (MyApp in this case). Notice that all three parts of this are separate. They can be modified independently of one another, versioned in your source control, managed, developed, etc.

这篇关于访问内部的Grails模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆