Grails 2.3.3名称空间控制器渲染视图 [英] Grails 2.3.3 namespaced controller render view

查看:121
本文介绍了Grails 2.3.3名称空间控制器渲染视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于Grails 2.3.3,它允许根据 http://grails.org/doc/latest/guide/theWebLayer.html#namespacedControllers



所以我们有这样的包装:

  / admin / FooController 
/ public / FooController

为了保持一致,我们需要这样的视图包:

  / views / admin / foo / ... 
/ views / public / foo / ...

然而,在FooController操作中,如果你没有硬编码渲染方法。它会在

  / views / foo / index .... 

它似乎无法利用名称空间。我们必须硬编码。



任何人都有这个想法吗?

解决方案

你当然可以做到这一点。通过SérgioMichels 展示了如何使用 afterInterceptor 来渲染来自不同目录的视图。这个想法是在获取渲染之前替换默认视图。



所以你的控制器应该是这样的:

  package test .xpublic 

class FooController {
static namespace ='public'
def afterInterceptor = {model,modelAndView - >
if(modelAndView.viewName.contains('index')){
modelAndView.viewName =/ public / index
}
}
def index() {}
}

您可以富有创意并且聪明地选择正确的视图,因为每个操作都会调用 afterInterceptor

这将帮助您从您的目录(views / admin或views / public)呈现视图。但是,您还需要照顾 UrlMappings

  class UrlMappings {
static mappings = {
/ foo / admin{
controller =foo
namespace ='admin'
}

/ foo / public{
controller =foo
namespace ='public'
}
...
}

,最后在需要传递命名空间的链接上。

 < g:link controller ='foo'namespace =admin>管理员< / g:link> 

提供示例应用程序 here

For Grails 2.3.3, it allows same name controller in different package with namespaced controller according to http://grails.org/doc/latest/guide/theWebLayer.html#namespacedControllers

So we have package like this:

/admin/FooController
/public/FooController

To keep consistent we want the view package like this:

/views/admin/foo/...
/views/public/foo/...

However, in the FooController action, if you don't hard code the render method. It will find the view in the

/views/foo/index....

It seems it can't take advantage of namespace. We have to hard code.

Anyone has good idea for that?

解决方案

You can certainly do this. Take a look at this post by Sérgio Michels shows how to render views from different directories using afterInterceptor. The idea is to substitute the default view before its getting rendered.

So your controller will be something like this:

package test.xpublic

class FooController {
    static namespace = 'public'
    def afterInterceptor = { model, modelAndView ->         
        if (modelAndView.viewName.contains('index')) {
            modelAndView.viewName = "/public/index"
        }
    }
    def index() { }
}

You can be creative and make it smart to pick the right view, since afterInterceptor will be called for each action.

This will help you to render the view from your directory (views/admin or views/public). However, you also need to take care of the UrlMappings

class UrlMappings {
    static mappings = {
        "/foo/admin" {
            controller="foo"
             namespace = 'admin'
        }

        "/foo/public" {
            controller="foo"
            namespace = 'public'
        }
...
}

and finally on the links you need to pass the namespace.

<g:link controller='foo' namespace="admin" >Admin</g:link>
<g:link controller='foo' namespace="xpublic">Public</g:link>

A sample application is provided here

这篇关于Grails 2.3.3名称空间控制器渲染视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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