從JSF頁面傳遞參數至後端backing bean


1. 方法傳遞

JSF 2.0,可以透過這樣的方法表達參數值 #{bean.method(param)}.
JSF page…
<h:commandButton action="#{user.editAction(delete)}" />
Backing bean…
@ManagedBean(name="user")
@SessionScoped
public class UserBean{
 
 public String editAction(String id) {
   //id = "delete"
 }
 
}



2.f:param

透過f:param標籤傳遞參數值,並通過後台bean的接收參數。

JSF page…
<h:commandButton action="#{user.editAction}">
 <f:param name="action" value="delete" />
</h:commandButton>
Backing bean…
@ManagedBean(name="user")
@SessionScoped
public class UserBean{
 
 public String editAction() {
 
   Map<String,String> params = 
                FacesContext.getExternalContext().getRequestParameterMap();
   String action = params.get("action");
          //...
 
 }
 
}

3. f:atribute

透過 f:atribute 標籤傳遞參數值,並通過後台bean的接收參數。

JSF page…
<h:commandButton action="#{user.editAction}" actionListener="#{user.attrListener}"> 
 <f:attribute name="action" value="delete" />
</h:commandButton>
Backing bean…
@ManagedBean(name="user")
@SessionScoped
public class UserBean{
 
  String action;
 
  //action listener event
  public void attrListener(ActionEvent event){
 
 action = (String)event.getComponent().getAttributes().get("action");
 
  }
 
  public String editAction() {
 //...
  } 
 
}
4. f:setPropertyActionListener
透過 f:setPropertyActionListener 標籤, 直接設定值setbacking bean的屬性.
JSF page…
<h:commandButton action="#{user.editAction}" >
    <f:setPropertyActionListener target="#{user.action}" value="delete" />
</h:commandButton>
Backing bean…
@ManagedBean(name="user")
@SessionScoped
public class UserBean{
 
 public String action;
 
 public void setAction(String action) {
  this.action = action;
 }
 
 public String editAction() {
    //now action property contains "delete"
 } 
 
}







留言

這個網誌中的熱門文章

java BigDecimal 加減乘除,四捨五入,四則運算及比較

MS sqlServer資料庫移轉至MySQL-->利用MySQL WorkBench

java 數字轉字串 字串轉數字