4 ways to pass parameter from JSF page to backi...

本文详细介绍了四种通过JSF页面将参数传递至后台组件的方法:方法表达式、f:param、f:attribute和f:setPropertyActionListener,并提供了具体的示例代码。同时讨论了在部署到Servlet容器如Tomcat时的注意事项。

As i know,there are 4 ways to pass a parameter value from JSF page to backing bean :

  1. Method expression (JSF 2.0)
  2. f:param
  3. f:attribute
  4. f:setPropertyActionListener

Let see example one by one :

1. Method expression

Since JSF 2.0, you are allow to pass parameter value in the method expression like this #{bean.method(param)}.

JSF page…

<h:commandButton action="#{user.editAction(delete)}" />

Backing bean…

@ManagedBean(name="user") @SessionScopedpublic class UserBean{  
	public String editAction(String id) { 	  //id = "delete" 	}  }
Note
If you are deploy JSF application in servlet container like Tomcat, make sure you include the “ el-impl-2.2.jar” properly. For detail, please read this article – JSF 2.0 method expression caused error in Tomcat.
2. f:param

Pass parameter value via f:param tag and get it back via request parameter in backing bean.

JSF page…

<h:commandButton action="#{user.editAction}"> 	<f:param name="action" value="delete" /> </h:commandButton>

Backing bean…

@ManagedBean(name="user") @SessionScopedpublic class UserBean{  
	public String editAction() {  
	  Map<String,String> params = 
                FacesContext.getExternalContext().getRequestParameterMap(); 	  String action = params.get("action");           //...  
	}  }

See a full f:param example here.

3. f:atribute

Pass parameter value via f:atribute tag and get it back via action listener in backing bean.

JSF page…

<h:commandButton action="#{user.editAction}" actionListener="#{user.attrListener}"> 
	<f:attribute name="action" value="delete" /> </h:commandButton>

Backing bean…

@ManagedBean(name="user") @SessionScopedpublic class UserBean{  
  String action;  
  //action listener event   public void attrListener(ActionEvent event){  
	action = (String)event.getComponent().getAttributes().get("action");  
  }  
  public String editAction() { 	//...   }	
 }

See a full f:attribute example here.

4. f:setPropertyActionListener

Pass parameter value via f:setPropertyActionListener tag, it will set the value directly into your backing bean property.

JSF page…

<h:commandButton action="#{user.editAction}" >     <f:setPropertyActionListener target="#{user.action}" value="delete" /> </h:commandButton>

Backing bean…

@ManagedBean(name="user") @SessionScopedpublic class UserBean{  
	public String action;  
	public void setAction(String action) { 		this.action = action; 	}  
	public String editAction() { 	   //now action property contains "delete" 	}	
 }

See a full f:setPropertyActionListener example here.

P.S Please share your idea, if you have any other ways :)

转载于:https://my.oschina.net/yida/blog/69615

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值