使用来自JSF页面的参数调用bean方法 [英] Calling bean methods with arguments from JSF pages

查看:61
本文介绍了使用来自JSF页面的参数调用bean方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以调用bean方法&直接从视图中将参数传递给它们,而不需要先设置bean属性,然后使用commandButton或类似的方法调用不带参数的方法?

我有一个项目列表,每个项目都有一个动作列表.为了减少状态,我使用单个素数remoteCommand来代替多个commandButton.从视图中获取动作触发器时,我会从javascript调用remoteCommand,但是由于remoteCommand是一个但用于多个项目,因此我也需要传递该项目的ID.我想知道是否有一种方法可以将项目的ID直接作为参数传递给bean方法,而不是先将其设置为bean属性?有什么办法吗?

实际上,我正在寻找一种处理页面上多个命令按钮的更好的方法.

建议?谢谢.


将JSF 2.1.6 Mojarra与Primefaces 3.0RC1结合使用

解决方案

从EL 2.2开始,它是Servlet 3.0的一部分,因此支持传递方法参数.因此,如果您的Web应用程序在具有web.xml声明的符合Servlet 3.0规范的Servlet 3.0兼容容器(Tomcat 7,Glassfish 3等)上运行(当您使用的是JSF 2.1时,则可能是正确的,这反过来又隐含地需要Servlet 3.0) ,那么您将可以采用以下形式将方法参数传递给bean操作方法:

 <h:commandButton value="Submit" action="#{bean.submit(item.id)}" />
 

使用

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

您甚至可以像这样传递有价值的对象:

 <h:commandButton value="Submit" action="#{bean.submit(item)}" />
 

使用

public void submit(Item item) {
    // ...
}

如果您以Servlet 2.5容器为目标,则可以通过用支持相同构造的JBoss EL代替EL实现来实现相同目的.另请参见调用直接方法或EL中带有参数/变量/参数的方法.

Is it possible to call bean methods & directly pass parameters to them from the view instead of requiring to first set the bean properties and then call methods without arguments using the commandButton or similar ?

I have a list of items with each item having a list of actions. To reduce the state, I am using a single primefaces remoteCommand, in place of several commandButton(s). On getting a action trigger from the view, I would call the remoteCommand from javascript but since the remoteCommand is one but used for multiple items thus I need to pass the id of the item as well. I am wondering if there is a way to pass the id of the item to the bean method directly as an argument instead of first setting it as a bean property ? Is there any way to do so ?

Actually I am looking at a better way to deal with multiple commandButtons on a page when there's a long list of items on the page.

Suggestions ? Thanks.


Using JSF 2.1.6 Mojarra with Primefaces 3.0RC1

解决方案

Passing method arguments is supported since EL 2.2 which is part of Servlet 3.0. So if your webapp runs on a Servlet 3.0 compatible container (Tomcat 7, Glassfish 3, etc) with a web.xml declared conform Servlet 3.0 spec (which is likely true as you're using JSF 2.1 which in turn implicitly requires Servlet 3.0), then you will be able to pass method arguments to bean action methods in the following form:

<h:commandButton value="Submit" action="#{bean.submit(item.id)}" />

with

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

You can even pass fullworthy objects along like as:

<h:commandButton value="Submit" action="#{bean.submit(item)}" />

with

public void submit(Item item) {
    // ...
}

If you were targeting a Servlet 2.5 container, then you could achieve the same by replacing the EL implementation by for example JBoss EL which supports the same construct. See also Invoke direct methods or methods with arguments / variables / parameters in EL.

这篇关于使用来自JSF页面的参数调用bean方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆