从UI向服务器传递参数

JSF提供了四种机制从UI向服务器传递参数:
     1.方法表达式参数
     2.<f:param>
     3.<f:setPropertyActionListener>
     4.<f:attribute>


从JSF2.0开始,方法表达式可以带参数,如:
<h:commandLink action="#{localeChanger.changeLocale('de')}">

<f:param>标签能够将一个参数添加到组件:
<f:param name="name" value="value">
其关联的组件类型不同表现也不同。如果为<h:outputText>则使用参数来填充占位符,如果添加f:param标签到命令组 件,JSF会将参数值作为请求参数传递到服务器。服务器端可以按如下方式访问参数:

/*这是第一种取得参数的方法
FacesContext context=FacesContext.getCurrentInstance();
Map<String,String> params=context.getExternalContext().getRequestParameterMap();
String param=params.get("name");

/*这是第二种取得参数值的方法
HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
String param=request.getParameter("id");

这是第三种取得参数的方法

UIComponent com = e.getComponent();
UIParameter param = (UIParameter) com.findComponent("id");
String param=param.getValue();

 

<f:attribute>标签设置组件的属性。

<f:attribute name="name" value="value">
服务器端可以按如下方式访问参数:
public void listener(ActionEvent event)
{
    UIComponent component=event.getComponent();
    Map<String,Object> attributes=component.getAttributes();
    Object attribute=attributes.get("name");
}


使用<f:setPropertyActionListener>标签,JSF在后端支撑bean中设置属性:
<f:setPropertyActionListener target="#{bean.attribute}" value="value"/>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值