Ajax 调用流处理程序中的方法 - Spring web flow 2.1.1

发布于 2024-09-24 10:53:06 字数 654 浏览 8 评论 0原文

我在流处理程序上的流(由 spring web flow 创建)内部执行 Ajax 调用时遇到问题。假设我们处于此流程的第 2 步,我想调用服务器上的一个方法,该方法会将新对象添加到页面上显示的列表中。此方法调用应由流处理程序执行,并且在不刷新的情况下,整个列表应重新显示在页面上,并且新对象应出现在此处。

简而言之,我想通过调用流程处理程序中的方法来进行部分提交。 但目前我只能通过刷新表单的整个提交来做到这一点。

我尝试过: - Spring.remoting.submitForm(...)

  • Spring.addDecoration(new Spring.AjaxEventDecoration({...})

  • DWR dojo请求...

我的环境: - Spring-webflow - 2.1.1.RELEASE

  • Spring-webmvc - 3.0.3.RELEASE

  • Spring - 3.0.3.RELEASE

  • Tiles - 2.2.1

  • Velocity - 1.6.4

我希望有人可以帮助解决这个问题。

问候

马雷克·多米尼克

I have a problem with performing Ajax call inside of flow (created by spring web flow) on a flow handler. Assuming that we are in step 2 of this flow and I would like to call a method on server which will add a new object to a list displayed on a page. This method call should be performed by flow handler and without refresh the whole list should be redisplayed on a page and new object should appear there.

In simpler words I would like to do a partial submit with call to a method in flow handler.
But for now I could do that only with the whole submitt of a form with refresh.

I have tried:
- Spring.remoting.submitForm(...)

  • Spring.addDecoration(new Spring.AjaxEventDecoration({...})

  • DWR dojo request ...

My environment:
- Spring-webflow - 2.1.1.RELEASE

  • Spring-webmvc - 3.0.3.RELEASE

  • Spring - 3.0.3.RELEASE

  • Tiles - 2.2.1

  • Velocity - 1.6.4

I hope that someone can help with this problem.

Regards

Marek Dominiak

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

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

发布评论

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

评论(1

楠木可依 2024-10-01 10:53:06

我正在使用 jQuery 做这件事。

在定义图块的主页中,我有:

<script type="text/javascript" src="/path/to/jquery/jquery-1.4.2.min.js" />

然后,在我想要调用页面片段的地方,我有:

    <script type="text/javascript"><![CDATA[
      $(document).ready(function() {

          $("#addNewFragment").click(function() {
              $.get("/app/fragments/target.page",function(data){$("#addFragmentLocation").before(data);});
          });
      });
    ]]></script>

在我想要片段出现的同一页面的下方,我有:

<span id="addFragmentLocation" />

我有一些ID 为“addNewFragment”的元素,这样当我单击它时,jQuery 函数就会执行。

我有一个名为 FragmentController.java 的控制器。它采用以下形式:

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@RequestMapping("/fragments/**")
@Controller
public class FragmentController {

// Add request mappings as you need to.
@RequestMapping(value = "/fragments/target.page", method = RequestMethod.GET)
public String getFragment(ModelMap modelMap, HttpServletRequest request, HttpServletResponse response) {
    //Add model elements as you need to.
    return "fragmentView";
}
}

最后,我在views.xml 文件中声明了一个视图,它将“fragmentView”视图映射回实际的.jspx 页面。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE tiles-definitions PUBLIC
"-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN"
"http://tiles.apache.org/dtds/tiles-config_2_0.dtd">

<tiles-definitions>

<definition name="fragmentView" template="/WEB-INF/path/to/fragments/myNewPageFragment.jspx" />

</tiles-definitions>

顺便说一句,jspx 页面自然是基于 XML 的。 jQuery 无法将 XML 插入基于 HTML 的 DOM。确保以以下方式启动它们:

 <div xmlns:jsp="http://java.sun.com/JSP/Page" >
      <jsp:output omit-xml-declaration="yes"/>
      <jsp:directive.page contentType="text/html; charset=ISO-8859-1" />

否则,您会收到一个神秘的 JavaScript 错误:

错误:未捕获的异常:[异常...“节点无法插入到层次结构中的指定点”代码:“3”nsresult:“0x80530003(NS_ERROR_DOM_HIERARCHY_REQUEST_ERR)”位置:“http://127.0.0.1: 8080/path/to/jquery/jquery-1.4.2.min.js 行:113"]

希望这有帮助!

I'm doing this exact thing using jQuery.

In my main page where the tiles are defined, I have:

<script type="text/javascript" src="/path/to/jquery/jquery-1.4.2.min.js" />

Then, in the places where I want to call a page fragment I have:

    <script type="text/javascript"><![CDATA[
      $(document).ready(function() {

          $("#addNewFragment").click(function() {
              $.get("/app/fragments/target.page",function(data){$("#addFragmentLocation").before(data);});
          });
      });
    ]]></script>

And lower in the same page where I want the fragment to appear, I have:

<span id="addFragmentLocation" />

And I have some element with an ID of "addNewFragment" so that when I click on it, the jQuery function executes.

I have a controller called FragmentController.java. It takes the form of:

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@RequestMapping("/fragments/**")
@Controller
public class FragmentController {

// Add request mappings as you need to.
@RequestMapping(value = "/fragments/target.page", method = RequestMethod.GET)
public String getFragment(ModelMap modelMap, HttpServletRequest request, HttpServletResponse response) {
    //Add model elements as you need to.
    return "fragmentView";
}
}

Finally, I have a view in the views.xml file declared which maps the "fragmentView" view back to an actual .jspx page.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE tiles-definitions PUBLIC
"-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN"
"http://tiles.apache.org/dtds/tiles-config_2_0.dtd">

<tiles-definitions>

<definition name="fragmentView" template="/WEB-INF/path/to/fragments/myNewPageFragment.jspx" />

</tiles-definitions>

As an aside, jspx pages are naturally XML based. jQuery can't insert XML into an HTML based DOM. Make sure you start them with:

 <div xmlns:jsp="http://java.sun.com/JSP/Page" >
      <jsp:output omit-xml-declaration="yes"/>
      <jsp:directive.page contentType="text/html; charset=ISO-8859-1" />

Otherwise, you get a mysterious JavaScript error:

Error: uncaught exception: [Exception... "Node cannot be inserted at the specified point in the hierarchy" code: "3" nsresult: "0x80530003 (NS_ERROR_DOM_HIERARCHY_REQUEST_ERR)" location: "http://127.0.0.1:8080/path/to/jquery/jquery-1.4.2.min.js Line: 113"]

Hope this helps!

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