如何将动态生成的按钮的 ID 传递给操作方法?

发布于 2024-12-21 14:56:21 字数 107 浏览 2 评论 0原文

我创建了一个 Facelets 页面。在页面中动态生成一些按钮,每个按钮都有一个ID, 提交事件时,我想将按钮的ID传递给操作。(按钮是动态的)

但我不知道如何在JSF中传递动态参数。

I created a Facelets page. In the page dynamically generated some buttons, each button has a ID,
When submitted the event, I want to pass a button's ID to the action.(button is dynamically)

But i don't know how to pass dynamic parameters in JSF.

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

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

发布评论

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

评论(1

忆依然 2024-12-28 14:56:21

您可以通过 UIComponent#getCurrentComponent()

public void submit() {
    UIComponent button = UIComponent.getCurrentComponent(FacesContext.getCurrentInstance());
    String id = button.getId(); // or button.getClientId();
    // ...
}

或者,如果您的目标是 Servlet 3.0 / EL 2.2 容器(Tomcat 7、Glassfish 3 等),那么您可以在 EL 中调用带参数的方法

<h:commandButton action="#{bean.submit(component.id)}" /> <!-- or component.clientId -->

public void submit(String id) {
    // ...
}

You can get the button component in the action method by UIComponent#getCurrentComponent():

public void submit() {
    UIComponent button = UIComponent.getCurrentComponent(FacesContext.getCurrentInstance());
    String id = button.getId(); // or button.getClientId();
    // ...
}

Alternatively, if you're targeting a Servlet 3.0 / EL 2.2 container (Tomcat 7, Glassfish 3, etc), then you can just invoke methods with arguments in EL:

<h:commandButton action="#{bean.submit(component.id)}" /> <!-- or component.clientId -->

with

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