如何仅在单击提交按钮时调用表单操作

发布于 2025-01-17 20:29:39 字数 852 浏览 0 评论 0原文

在没有任何查询参数的情况下向页面发送JSF参数时,使用以下JSF代码:

<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:jsf="http://xmlns.jcp.org/jsf">
    <head>
        <title>Search for Word</title>
    </head>
    <body>
        <div>
            <form method="get" action="#{dictionary.search()}" id="search">
                <input jsf:id="keyword" value="#{dictionary.keyword}" />
            </form>
            <button type="submit" form="search" value="Submit">Submit</button>
        </div>
    </body>
</html>

Action Dictionary.Search()即使无需单击“提交”按钮,也总是会调用。但是,直到单击提交按钮,就不会调用该操作。

问题:如何更改JSF代码而没有任何其他额外的标签库,但只有提供的JSF TAB lib,即通行元素,以便托管的bean方法dictionary.method()< /代码>仅在单击提交按钮时才调用?

When sending a jsf request without any query parameters to the page with the following jsf code:

<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:jsf="http://xmlns.jcp.org/jsf">
    <head>
        <title>Search for Word</title>
    </head>
    <body>
        <div>
            <form method="get" action="#{dictionary.search()}" id="search">
                <input jsf:id="keyword" value="#{dictionary.keyword}" />
            </form>
            <button type="submit" form="search" value="Submit">Submit</button>
        </div>
    </body>
</html>

the action dictionary.search() is always invoked even without clicking the submit button. However, the action is not expected to be invoked until the submit button is clicked.

Question: How to change the jsf code without any other extra tag libraries but only the provided jsf tab lib, i.e. the pass-through element so that the managed bean method dictionary.method() is invoked only when the submit button is clicked?

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

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

发布评论

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

评论(2

日暮斜阳 2025-01-24 20:29:39

我最终自己找到了答案:

            <form jsf:id="form">
                <input type="text" jsf:value="#{dictionary.keyword}" />
                <button jsf:action="#{dictionary.search}">Search</button>
            </form>

这里的实质是生成由h:commandButton生成的等效组件树

基于jsf 2.2教程8.5 HTML5 友好标记

要使不是 JavaServer Faces 元素的元素成为传递元素,请使用 http://xmlns.jcp.org/jsf 命名空间

整个表单必须作为 jsf 组件 传递到 jsf,即

元素必须是这样的,其中 id 的值实际上可以是随机的

也是如此;

I eventually found the answer myself:

            <form jsf:id="form">
                <input type="text" jsf:value="#{dictionary.keyword}" />
                <button jsf:action="#{dictionary.search}">Search</button>
            </form>

The nitty-gritty here is the generation of the equivalent component tree of that generated by h:commandButton

Based on the jsf 2.2 tutorial 8.5 HTML5-Friendly Markup,

To make an element that is not a JavaServer Faces element a pass-through element, specify at least one of its attributes using the http://xmlns.jcp.org/jsf namespace

the whole form has to passed through to jsf as a jsf component, the <form> element has to be like this <form jsf:id="form">, where the value of the id actually could be at random

The same goes with both the <input> and <button> element and, as to the <input> element the type attribute has to be assigned with a value, here is text

无戏配角 2025-01-24 20:29:39

首先 - 您的提交按钮必须位于表单标签内。然后尝试在按钮上设置操作,而不是在表单上设置操作,以避免提交。

希望这有帮助。

<body>
    <div>
        <h:form>
            ...
            <h:commandLink action="#{dictionary.search()}" value="Submit" />
        </h:form>
    </div>
</body>

根据@BalusC 的评论,我最好的是:

<form method="get" id="search" jsf:action="#{bean.search}" >
    <input jsf:id="keyword" value="#{bean.keyword}" />
    <input type="submit" jsf:action="#{bean.search}" value="SUBMIT"
</form>

但只有在 forminput 这两个标签上放置 jsf:action ,才有可能使其工作。

也许@BalusC 可以向我们展示正确的方法。

First - your submit button must be inside the form tag. And then try to set the action on the button, not on the form to avoid the submit.

Hope this helps.

<body>
    <div>
        <h:form>
            ...
            <h:commandLink action="#{dictionary.search()}" value="Submit" />
        </h:form>
    </div>
</body>

Up to @BalusC comment my best is:

<form method="get" id="search" jsf:action="#{bean.search}" >
    <input jsf:id="keyword" value="#{bean.keyword}" />
    <input type="submit" jsf:action="#{bean.search}" value="SUBMIT"
</form>

But only putting jsf:action on both tags, form and input, was possible make it work.

May be @BalusC can show us the correct way.

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