【问题标题】:Grails: How do I map a URL to a controller inside a custom plugin?Grails:如何将 URL 映射到自定义插件中的控制器?
【发布时间】:2025-12-19 17:10:12
【问题描述】:

我在一个名为TextController插件 中有一个控制器,其操作名为index

然后我在 调用应用程序 中有一个名为 test-plugin.gsp 的视图,其中包含以下包含标记:

<g:include controller="text" />

这很好用。问题是插件的控制器实际上需要通过类似包的名称来标识,例如:com.foxbomb.fxportal3.components.text,我需要这样引用它。所以我想我们尝试为插件创建一个 URL 映射,例如:

    "/components/com.foxbomb.fxportal3.components.text/" {
        controller = "text"
        action = "index"
    }        

我也不知道如何在调用应用程序中创建一个包含来尝试调用该 URL,例如(我知道您没有获得 URL 属性):

<g:include url="/components/com.foxbomb.fxportal3.components.text"/>

换句话说,我想通过它的映射来包含一个控制器/动作,而不是它的控制器/动作名称

【问题讨论】:

    标签: grails plugins tags include


    【解决方案1】:

    &lt;g:include&gt; 不允许 url 属性,这让我感到惊讶,但看看 the source of the &lt;g:include&gt; tag 应该可以很容易地在您自己的 taglib 中实现这一点(未经测试):

    import org.codehaus.groovy.grails.web.util.WebUtils
    
    class IncludeTagLib {
      def grailsUrlMappingsHolder
    
      def includeUri = { attrs, body ->
        def mapping = grailsUrlMappingsHolder.match(attrs.uri)
        out << WebUtils.includeForUrlMappingInfo(request, response, mapping,
                   attrs.model ?: [:])?.content
      }
    }    
    

    你会在 GSP 中说 &lt;g:includeUri uri="/components/com.foxbomb.fxportal3.components.text"/&gt;

    【讨论】:

    • 您好伊恩,感谢您的回答。 components 不是上下文路径。这只是我添加到映射文件的路径。仍然无法通过应用程序通过 URL 访问插件 URL。此外,您没有提到我如何解决“在具有以下 URL 映射的插件中包含控制器”。有什么想法吗?
    • @MarkvanWyk 好的,我知道你现在从哪里来。请参阅我的最新编辑以获取可能的解决方案。
    • 我现在可以吻你,但我不喜欢那样,那会有点奇怪,所以我只会给你一个 +1 和一个接受的答案。是的,你可以把它当作感谢!
    • 你知道如何阅读另一边的模型吗? (接收控制器)。 chainModel 未填充,flash 为空。
    • @MarkvanWyk 我不认为包含的请求可以直接访问原始模型,但您可以将model 属性传递给标签:&lt;g:includeUri uri="..." model="[foo:foo, bar:bar]" /&gt;。您可能甚至可以使用model="${pageScope.variables}" 将整个当前模型传递给包含的操作,您必须尝试一下,看看是否有任何问题......
    最近更新 更多