Controller - redirect
Summary
- 访问一个 Controller 的 Action,然后在这个 Action 中直接执行另外一个 Action,浏览器的 URL 也会显示为目标的地址。
- 官方文档:http://docs.grails.org/latest/ref/Controllers/redirect.html
- 参考文档:https://tosbourn.com/difference-between-redirect-render-rails/
Demo
- 在一个Action中直接跳转到另外一个Action中,另外的Action接受一个opportunity对象,这里直接通过id传过去就行,约定规则,就可以获取对应内容。
redirect controller: "opportunity", action: "show", id: opportunityContract.opportunity.id
Demo
- 例如当前在:Opportunity 的 show.gsp 页面,http://localhost:8080/opportunity/show/56829
- 需要创建 RightCentification 的关联对象,http://localhost:8080/rightCertification/create?opportunity=56829
- 然后在 RightCentification 的 Create Action中,获取到 Opportunity 的 show 地址。
def create()
{
params['targetUri'] = request.getHeader("referer")
respond new RightCertification(params)
}
- 然后 RightCentification 的 Create 页面,放到表单里面,再传递给后面Save Action:
<g:hiddenField name="targetUri" value="${params?.targetUri}"></g:hiddenField>
- 最后 RightCentification 的 Save Action 就可以直接跳转回Opportunity的show.gsp页面
redirect url: params['targetUri']
Demo
- 在使用 redirect 方法返回错误信息。
- 如果让 URL 产生变化,且有问题的对象信息也返回去,就得用 message。
if (workflowTypeTeam.hasErrors())
{
transactionStatus.setRollbackOnly()
def error = workflowTypeTeam.errors.allErrors.first()
flash['message'] = message(code: error.defaultMessage.toString(), args: error.arguments)
redirect(action:'create')
return
}
Demo
- 直接跳转到另外一个Action,params 中会拼到目标的URL上,类似浏览器使用 GET 方式访问了目标 Action。
redirect(action: 'create', params: [workflowTypeTeam: workflowTypeTeam])
本文来自博客园,作者:duchaoqun,转载请注明原文链接:https://www.cnblogs.com/duchaoqun/p/11834097.html