【问题标题】:Redirection in Grails Web-flowGrails Web 流中的重定向
【发布时间】:2012-05-19 14:26:09
【问题描述】:

我对 Grails 网络流中的重定向有疑问。 我处于允许用户输入问题答案的视图状态。在 2 次错误尝试中,我应该能够重新引导用户从不同的控制器查看页面。我的意思是

challengeQuestionOne{
            onRender() {
                //Display question
            }
            on('next') {BuildQuestion command ->
                bindData(flow.recovery,command)
                [return the model to flow]
                if(command.hasErrors()) {
                    flow.command = command
                    return error()
                }
                if(check for status. If doesnot pass){
                    flash.message=message(code:'loginForm.account.locked', default: 'Please Contact Admin.')
                    redirect(controller : "login",action: "login")//how to redirect from here to diff controller
                }                    
                if (//compare answer entered) {

                }
                else{
                   //set status not active
                }

            }.to("challengeQuestionTwo")
            on(Exception).to("error")
            on('cancel').to('finish')                
        }

我试图从 onRender 重定向。它正在重定向到页面。但是如何在重定向页面上显示错误消息。如何将错误消息从一个控制器转发到另一个控制器??

【问题讨论】:

    标签: grails spring-webflow grails-2.0 grails-controller


    【解决方案1】:

    Ivo Houbrechts 写了一篇关于 grails webflow 的优秀教程:

    Webflow 定义了自己的 flash 范围。虽然它与标准 grails flash 范围具有相同的语义(主要目的是只存储对象直到下一个请求之后),但它是不同的范围。这意味着存储在 webflow 的 flash 范围内的对象在标准 grails 操作中不可见。

    import org.springframework.web.context.request.RequestContextHolder
    ....
    RequestContextHolder.currentRequestAttributes().flashScope.message = "YourMessage"
    

    您可以在这里阅读更多内容:

    http://livesnippets.cloudfoundry.com/docs/guide/

    【讨论】:

      【解决方案2】:

      Flash 范围在这种情况下将无法正常工作。 尝试使用另一种方法来显示错误。例如。您可以通过重定向传递参数。或者您可以抛出一些异常并在呈现的页面上进行检查,如下所示:

      <g:if test="${flowExecutionException}">
          <div class="error"><g:message code="loginForm.account.locked"/></div>
      </g:if>
      

      【讨论】:

      • Yaa ....我使用了第一种方法通过重定向发送参数。但是 flowExecutionException 看起来也不错……我会试试的
      猜你喜欢
      • 2010-12-29
      • 1970-01-01
      • 1970-01-01
      • 2012-09-08
      • 1970-01-01
      • 2016-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多