JSF中CommandButton与CommandLink传值

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

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

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

1
2
3
<h :commandLink actionListener="#{userListBean.checkUser}" value="审核通过">
    <f :param name="userId" value="#{user.userId}" />
</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
<h :commandButton actionListener="#{userListBean.resetPassword}" value="审核通过">
    <f :attribute name="userId" value="#{user.userId}"/>
</h>

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

1
2
3
4
private void resetPassword(ActionEvent actionEvent){
    long userIdString = (Long) actionEvent.getComponent().getAttributes().get("userId");
    //other code ……
}

本文是本博客原创文章,您可以自由转载,但转载请注明来源。

你可能对下面的文章感兴趣

  1. 用Python生成文件的MD5校验码
  2. JSF异常消息机制及应用

发表评论

电子邮件地址不会被公开。 必填项已用 * 标注

*

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">