JSF中CommandButton与CommandLink传值

本文深入探讨了JSF框架中f:param标签的使用方法及其在不同组件中的表现,包括如何通过f:param传递参数至服务器端进行处理。

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

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


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

 

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

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

1
2
3
4
private void checkUser(ActionEvent actionEvent){
    String uid = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("userId");
    //othe
}

但是f:param的传值方式对于h:commandButton是没有作用的,(如果你是用搜索引擎搜到这篇文章的话,相信你肯定是遇到了这个问题),详情可以参考http://www.javaeye.com/topic/93388

如果是h:commandButton,那么可以使用f:attribute来进行传值,示例如下:

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

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

1
2
3
4
private 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、付费专栏及课程。

余额充值