• 4 ways to pass parameter from JSF page to backing bean


    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")
    @SessionScoped
    public class UserBean{
     
        public String editAction(String id) {
          //id = "delete"
        }
     
    }

    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")
    @SessionScoped
    public class UserBean{
     
        public String editAction() {
     
          Map<String,String> params = 
                    FacesContext.getExternalContext().getRequestParameterMap();
          String action = params.get("action");
              //...
     
        }
     
    }

    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")
    @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

    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")
    @SessionScoped
    public class UserBean{
     
        public String action;
     
        public void setAction(String action) {
            this.action = action;
        }
     
        public String editAction() {
           //now action property contains "delete"
        }    
     
    }
  • 相关阅读:
    javaapplicationWeb application setup on Ubuntu VPS
    内容中断随想录(risc cpu的那些事)
    算法线性编程珠玑读书笔记之----->使用线性算法求解连续子序列的最大和
    classnull100
    安装javaUbuntu下安装JDK1.6,并将之设为默认的JDK
    筛选实现C++实现筛选法
    调试设置移动端Web开发环境搭建实践
    路由器交换机[置顶] 路由器和交换机的综合实验⑵
    卡数字怀念的东西:魔方
    密码配置配置SSH免密码登陆
  • 原文地址:https://www.cnblogs.com/sos-blue/p/3273068.html
Copyright © 2020-2023  润新知