Grails WebFlow 与 Ajax

发布于 2024-08-18 03:38:19 字数 1421 浏览 9 评论 0原文

我正在尝试使用 Ajax 请求转换到 WebFlow 的下一个状态。但它保持在相同的状态并返回 GSP 作为该状态的响应,而我期待下一个状态的 GSP。

以下是 WebFlow 代码:

def gettingStartedAjaxFlow = {      
        flow1 {
            on("next") {                
                println "flow1"
            }.to("flow2")
            on("skip").to("flow2")
        }

        flow2 {
            on("next") {
                println "flow2"
            }.to("flow3")
            on("skip").to("flow3")
        }

        flow3 {         
            on("next"){             
                println "flow3"
            }.to("finish")
            on("skip").to("finish")

            finish {
                redirect(action:"index")
            }
        }
}

以下是我为状态转换所做的 Ajax 调用:

$.ajax({
            type: "POST",
            url: "/UN/user/gettingStartedAjax",
            success: function(data) {
                $("#wizardDiv").html(data);
            }
});

每个状态(flow1、flow2、flow3)的 GSP 都包含一个代码片段,其中包含 remoteForm & 。需要 next 并跳过提交按钮以转换到下一个状态,并因此更新“wizardDiv”div。以下是 flow1 状态的 GSP 片段:

<g:formRemote name="flow1Form" url="[controller:'user', action:'gettingStartedAjax']" update="wizardDiv">
    <p>You are in flow 1</p>
    <g:submitButton name="next" value="Next Flow" />
    <g:submitButton name="skip" value="Skip Flow" />    
</g:formRemote>

I am trying to transition to next state of a WebFlow using Ajax requests. But it stays in the same state and returns the GSP as response for that state while I am expecting the GSP for the next state.

Following is the WebFlow code:

def gettingStartedAjaxFlow = {      
        flow1 {
            on("next") {                
                println "flow1"
            }.to("flow2")
            on("skip").to("flow2")
        }

        flow2 {
            on("next") {
                println "flow2"
            }.to("flow3")
            on("skip").to("flow3")
        }

        flow3 {         
            on("next"){             
                println "flow3"
            }.to("finish")
            on("skip").to("finish")

            finish {
                redirect(action:"index")
            }
        }
}

Following is the Ajax call I am making for the state transition:

$.ajax({
            type: "POST",
            url: "/UN/user/gettingStartedAjax",
            success: function(data) {
                $("#wizardDiv").html(data);
            }
});

The GSPs for each state (flow1, flow2, flow3) contains a a code fragment having remoteForm & required next and skip submit buttons to transition to next state and as a result update the "wizardDiv" div. Following is the GSP fragment for flow1 state:

<g:formRemote name="flow1Form" url="[controller:'user', action:'gettingStartedAjax']" update="wizardDiv">
    <p>You are in flow 1</p>
    <g:submitButton name="next" value="Next Flow" />
    <g:submitButton name="skip" value="Skip Flow" />    
</g:formRemote>

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

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

发布评论

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

评论(3

夜深人未静 2024-08-25 03:38:19

我陷入了同样的问题,几乎想通了,

你需要做的是发回 Grails webflow“_flowExecutionKey”
当前状态的跟踪,

我相信您已经看到这个,这是 Google 找到的唯一不错的结果。

我向操作发送 ajax 请求,该操作填充模板并将其发回
带有输入标签,

 <input id="flowExecutionKey" name="_flowExecutionKey" value="${request.flowExecutionKey}" size="100"/>

但是您可以尝试发送一个像 JSON 一样标记为“_flowExecutionKey”的寺庙以及您想要发回的数据,

这是我的两分钱

I'm stuck on the same problem, Nearly figured it out,

what you need to do, is send back the Grails webflow "_flowExecutionKey" that keeps
track of the current state,

I'm sure you've seen this, as its the only decent result Google finds.

I send an ajax request to an action, which populates a template and sends it back
with an input tag,

 <input id="flowExecutionKey" name="_flowExecutionKey" value="${request.flowExecutionKey}" size="100"/>

But you could try send a temple back marked up like JSON with the "_flowExecutionKey" along with the data you want to send back,

That's my two cents

奶茶白久 2024-08-25 03:38:19

除了跟踪执行情况(如 Daxon 发布的那样),您还需要确保按钮名为 _eventId_next 和 _eventId_skip。 g:submitbutton 通常足够聪明,可以为您执行此操作,但它可能不在 RemoteForm 内部。

另外,我的 Web 流代码使用参数执行,而不是 flowExecutionKey - 您使用的是哪个版本的 Grails?

As well as keeping track of the execution (as Daxon posted), you'll need to make sure your buttons are named _eventId_next and _eventId_skip. g:submitbutton is normally clever enough to do this for you but it might not be inside of a remoteForm.

Also, my web flow code uses the parameter execution, not flowExecutionKey - which version of Grails are you using?

§对你不离不弃 2024-08-25 03:38:19

这是一个在 grails 2.5.3 中至少适用于一个操作的解决方案。按钮的 id 和名称会自动修改为包含“eventId”作为前缀,但这对我来说仍然不起作用,除非我手动添加 _event_id 作为输入参数。但是,我不确定这如何适用于多个可能的事件。

<g:formRemote name="flow1Form" url="[controller:'user', action:'gettingStartedAjax']" update="wizardDiv">

<input type="hidden" id="execution" name="execution" value="${request.flowExecutionKey}"/>
<input type="hidden" id="_eventId" name="_eventId" value="next"/>

<fieldset class="form">
</fieldset>

<fieldset class="buttons">
    <g:submitButton name="next" value="Next flow"/>
</fieldset>

</g:formRemote>

Here a solution that works in grails 2.5.3 at least for one single action. The id and name of the button are automatically modified to include "eventId" as prefix but this still did not work for me unless I added _event_id manually as input parameter. However, I'm not sure how this can work for multiple possible events.

<g:formRemote name="flow1Form" url="[controller:'user', action:'gettingStartedAjax']" update="wizardDiv">

<input type="hidden" id="execution" name="execution" value="${request.flowExecutionKey}"/>
<input type="hidden" id="_eventId" name="_eventId" value="next"/>

<fieldset class="form">
</fieldset>

<fieldset class="buttons">
    <g:submitButton name="next" value="Next flow"/>
</fieldset>

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