转:JSF中CommandButton与CommandLink传值

本文详细介绍了在JavaServer Faces (JSF) 中如何使用f:param和f:attribute标签来传递参数。具体阐述了这两种标签在h:outputText、h:commandLink及h:commandButton组件中的不同应用场景,并提供了服务器端获取这些参数值的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转自:http://www.zhlwish.com/2009/12/31/jsf_commandbutton_commandlink/

 

f:param标签能够将一个参数添加到组件。需要注意的是f:param标签的不同表现依赖于它所关联的组件类型

【1】如果为 h:outputText添加f:param标签,那么JSF实现将使用参数来填充占位符,例如{0}、{1}等。

【2】如果添加f:param标签到h:commandLink,JSF实现会将参数值作为请求参数传递到服务器,如:

<h :commandLink actionListener="#{userListBean.checkUser}" value="审核通过">
    <f :param name="userId" value="#{user.userId}" />
</h>

 

在服务器端可以使用如下方法来获取传递到服务器端的值:

public void checkUser(ActionEvent actionEvent){
    String uid = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("userId");
    // other code
}

 

但是f:param的传值方式对于h:commandButton是没有作用的,(如果你是用搜索引擎搜到这篇文章的话,相信你肯定是遇到了这个问题),如果是h:commandButton,那么可以使用f:attribute来进行传值,示例如下:

<h :commandButton actionListener="#{userListBean.resetPassword}" value="审核通过">
    <f :attribute name="userId" value="#{user.userId}"/>
</h>

 

在服务器端可以使用如下方法来获取传递到服务器端的值:

public void resetPassword(ActionEvent actionEvent){
    long userIdString = (Long) actionEvent.getComponent().getAttributes().get("userId");
    //other code ……
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值