Grails 渲染不同控制器的视图

发布于 2025-01-02 23:50:58 字数 1848 浏览 1 评论 0原文

我的问题与以下帖子类似 渲染另一个控制器的视图

我有一个 TestConfigController 我的问题是什么如果验证失败并且我想渲染controller:test和view:edit而不是controller:testCOnfig和view:edit,我可以做什么

def save() {

        def testConfigInstance = new TestConfig(params)
        if (!testConfigInstance.save(flush: true)) {

/*而不是查看:“编辑”,我想查看:“/test/edit”,这不起作用*/

            render(view:"edit",  model: [testConfigInstance: testConfigInstance],id:params.test.id)
            return
        }
        println "+++++++++++++++++++++++++"
        flash.message = message(code: 'Data successfully saved', args: [message(code: 'testConfig.label', default: 'Successfully saved')])
        redirect(action: "edit", controller:"test", id:params.test.id)
    }

有任何指针吗?我已经研究过 grails 重定向 ,它没有“ model" 参数,因此无法将验证错误传递给视图 我还查看了 grails render ,它没有控制器参数这样我就可以返回到不同的控制器! 如果需要更多详细信息/代码,请告诉我

编辑 使用以下两件事之一时会发生以下情况

render(view:"/test/edit",  model: [testConfigInstance: testConfigInstance],id:params['test.id'])

上面的代码渲染页面 /test/edit 而不引用 testid 最终错误地指出“test.id”不能为空..(意味着它渲染 /test/edit 而不是 / test/edit/1)

render(view:"/test/edit/"+params['test.id'],  model: [testConfigInstance: testConfigInstance],id:params['test.id'])

上面的代码会导致以下错误

The requested resource (/EasyTha/WEB-INF/grails-app/views/test/edit/1.jsp) is not available.

上面的代码中的任何一个都只渲染“/test/edit”,最后没有 id,因此最终会出错,说 test.id 不能为空。

My question is similar to this following post
Render a view of another controller

I have a TestConfigController my question is what can I do in case validation fails and I want to render controller:test and view:edit rather then controller:testCOnfig and view:edit

def save() {

        def testConfigInstance = new TestConfig(params)
        if (!testConfigInstance.save(flush: true)) {

/*Rather then view:"edit" i want view:"/test/edit" which does not work */

            render(view:"edit",  model: [testConfigInstance: testConfigInstance],id:params.test.id)
            return
        }
        println "+++++++++++++++++++++++++"
        flash.message = message(code: 'Data successfully saved', args: [message(code: 'testConfig.label', default: 'Successfully saved')])
        redirect(action: "edit", controller:"test", id:params.test.id)
    }

Any pointers? I have already looked into grails redirect which does not have "model" param and thus can not pass the validation errors to the view
Also I have looked in to grails render which does not have controller param so that I can go back to different controller!
Please let me know if more detail/code is needed

EDIT
Following happens while using one of the two things

render(view:"/test/edit",  model: [testConfigInstance: testConfigInstance],id:params['test.id'])

The code above renders the page /test/edit with no reference to testid eventually erroring out saying "test.id" can not be null.. (means its rendering /test/edit and not /test/edit/1)

render(view:"/test/edit/"+params['test.id'],  model: [testConfigInstance: testConfigInstance],id:params['test.id'])

The code above leads to following error

The requested resource (/EasyTha/WEB-INF/grails-app/views/test/edit/1.jsp) is not available.

Either one of the above code renders just "/test/edit" no id at the end, thus eventually erroring out saying test.id can not be null.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

·深蓝 2025-01-09 23:50:58

您尝试附加到视图路径中的 id 值应该是模型映射的一部分。您在模型图中提供的值可在渲染的视图中使用。

在您尝试的第一个选项中,id 参数没有任何区别,因为渲染方法不使用任何“id”参数(重定向方法使用 id 参数生成重定向 url)。

您的代码片段应该是这样的:

render(view:"/test/edit",  model: [testConfigInstance: testConfigInstance, id:params['test.id']])

您在此处使用的渲染方法不会将您重定向到其他操作。 render 只是将解析后的 viewName 打印到输出流。例如。 render(view:"/test/edit") 只是渲染 edit.gsp 视图。它实际上并没有将您重定向到测试控制器的编辑操作。因此,仅在模型映射中传递 id 并不能让您访问视图上的 testInstance。您必须通过 id 获取 testInstance 并将其传递给模型映射中的视图

render(view:"/test/edit",  model: [testConfigInstance: testConfigInstance, testInstance: Test.get(params['test.id'] as Long)])

The value of id that you are trying to append in the view path should be a part of the model map. The values that you provide in the model map are available in the view that is rendered.

In the first option that you tried, the id parameter doesn't make any difference as the render method doesn't use any 'id' parameter (the redirect method uses the id parameter to generate the redirect url).

Your code snippet should be something like this:

render(view:"/test/edit",  model: [testConfigInstance: testConfigInstance, id:params['test.id']])

The render method that you are using here doesn't redirect you to some other action. render just prints the parsed viewName to the output stream. Eg. render(view:"/test/edit") just renders the edit.gsp view. It isn't actually redirecting you to the edit action of test controller. Therefore, just passing the id in the model map won't give you access to the testInstance on the view. You will have to get the testInstance By id and pass it to the view in the model map

render(view:"/test/edit",  model: [testConfigInstance: testConfigInstance, testInstance: Test.get(params['test.id'] as Long)])
勿挽旧人 2025-01-09 23:50:58

Anuj Arora 是对的:

如果您只想渲染任意视图,您可以使用与 grails-app/view 文件夹相关的视图的完整路径:

在您的情况下:

render(view:"/test/edit",  model: [testConfigInstance: testConfigInstance],id:params.test.id)

应该有效。

Anuj Arora is right:

If you just want to render an arbitrary view you can use the full path to the view related to the grails-app/view folder:

In your case:

render(view:"/test/edit",  model: [testConfigInstance: testConfigInstance],id:params.test.id)

should work.

十年九夏 2025-01-09 23:50:58

如果您只想渲染视图 /test/edit,那么调用 render(view:'/test/edit',...) 就可以了需要。

相反,如果您还想包含 TestControlleredit 操作的一些处理,请查看 chain() 调用。它有一个 model 参数,您可以在其中传递验证错误和 controller/action 参数以重定向到其他控制器。

If you are only wanting to render the view /test/edit, then the call render(view:'/test/edit',...) should be all you need.

If instead, you also want to include some of the processing from the TestController and edit action, then look at the chain() call. It has a model parameter where you can pass the validation errors and controller/action parameters to redirect to the other controller.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文