Tapestry 5.2:使用表单中的数据更新区域

发布于 2024-10-09 06:34:57 字数 1652 浏览 1 评论 0原文

我正在使用 Tapestry 5.2.4 和 AJAX。

在我的 Test.tml 中,我有一个表单:

<form t:id="form">
   <t:label for="userName"/>:
   <input t:type="TextField" t:id="userName" size="30"/>
</form>

和一个显示变量“test”的区域:

<t:zone t:id="myZone" id="myZone">
    <p>show test ${test}</p>
</t:zone>

现在我尝试将表单字段“userName”的值放入带有操作链接的区域中:

<t:actionlink t:id="SomeLink" zone="myZone" context="${userName}">update</t:actionlink>

这是 java 类 Test.java:

public class Test {

    @Persist
    @Property
    private String userName;

    @Property
    private String test;
    @InjectComponent
    private Zone myZone;

    @Component
    private Form form;

    Object onActionFromSomeLink(String input) {
        test = input;
        return myZone.getBody();
    }   
}

我认为这会“获取”表单字段 userName 的值,并将其与 actionlink 一起传递给 onActionFromSomeLink 方法。该方法将变量“test”设置为输入并显示该区域。

这不起作用并引发一个我不明白的错误:

Ajax failure: Status 500 for /example/test.somelink: Request event 'action' (on component Test:somelink) was not handler;您必须在组件或其容器之一中提供匹配的事件处理程序方法。

Communication with the server failed: Request event 'action' (on component Test:somelink) was not handled; you must provide a matching event handler method in the component or in one of its containers.

如何实现一个函数,从表单获取输入然后更新区域?

干杯

I'm playing with tapestry 5.2.4 and AJAX.

In my Test.tml I have a form:

<form t:id="form">
   <t:label for="userName"/>:
   <input t:type="TextField" t:id="userName" size="30"/>
</form>

And a zone that displays the variable "test":

<t:zone t:id="myZone" id="myZone">
    <p>show test ${test}</p>
</t:zone>

Now I try to put the value of the form field "userName" into the zone with an actionlink:

<t:actionlink t:id="SomeLink" zone="myZone" context="${userName}">update</t:actionlink>

Here's the java class Test.java:

public class Test {

    @Persist
    @Property
    private String userName;

    @Property
    private String test;
    @InjectComponent
    private Zone myZone;

    @Component
    private Form form;

    Object onActionFromSomeLink(String input) {
        test = input;
        return myZone.getBody();
    }   
}

I thought this would "take" the value of the form field userName and pass it with an actionlink to the method onActionFromSomeLink. The method sets the variable "test" to input and the zone is displayed.

This does not work and throws an error I do not understand:

Ajax failure: Status 500 for /example/test.somelink: Request event 'action' (on component Test:somelink) was not handled; you must provide a matching event handler method in the component or in one of its containers.

Communication with the server failed: Request event 'action' (on component Test:somelink) was not handled; you must provide a matching event handler method in the component or in one of its containers.

How can I implement a function, that takes input from a form and then updates a zone?

Cheers

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

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

发布评论

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

评论(1

携余温的黄昏 2024-10-16 06:34:57

您使用了错误的组件。 ActionLink 呈现 HTML 链接,它根本不与表单交互。虽然您可以为链接提供上下文,但它是绝对静态的,并且不会从客户端的表单中检索值。 (context 主要用于区分对象,如果您在某处有一个项目列表,每个项目都有一个对它们执行某些操作的链接。)

您要做的是提交表单并获得该表单更新您的区域。您必须将 zone 参数添加到表单组件,并添加一些可以让您提交表单的内容:

<form t:id="form" t:zone="myZone">
    <t:label for="userName"/>:
    <input t:type="TextField" t:id="userName" size="30"/>
    <input type="submit" t:type="Submit" />
</form>

在您的类中:

@Inject
private Request request;

@OnEvent(EventConstants.SUCCESS)
Object formSubmitted(){
    //return zone content only if AJAX request, page otherwise
    if (request.isXHR()) {
        return myZone.getBody();
    } else {
        return this;
    }
}

如果您确实想使用链接来提交表单,则LinkSubmit 组件也可以让您做到这一点。

You're using the wrong component. An ActionLink renders an HTML link, it does not at all interact with forms. While you can provide a context to the link, it is absolutely static and does not retrieve values from forms on the client side. (context is useful mainly to discriminate between objects if you have a list of items somewhere with a link each that does something to them.)

What you're trying to do is to submit the form and have that update your zone. You'll have to add the zone parameter to the form component, and add something that lets you submit the form:

<form t:id="form" t:zone="myZone">
    <t:label for="userName"/>:
    <input t:type="TextField" t:id="userName" size="30"/>
    <input type="submit" t:type="Submit" />
</form>

And in your class:

@Inject
private Request request;

@OnEvent(EventConstants.SUCCESS)
Object formSubmitted(){
    //return zone content only if AJAX request, page otherwise
    if (request.isXHR()) {
        return myZone.getBody();
    } else {
        return this;
    }
}

If you really want to use a link to submit the form, the LinkSubmit component lets you do that, too.

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