JSF的参数传递

1.   URL

页面上的超连接用以上方式,在backingBean中可以用
HttpServletRequest request = (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
id = request.getParameter("id");
或者:
Map requestParams =FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
id = (String) requestParams.get("id");
注意:requestParams是只读的,对其的写入操作是不允许的,所以要是往回传值的话只能用request;

 

2.  f:param

用f:param标签传参数,backing bean通过request parameter获取传过来的参数。

JSF page…

<h:commandLink action="#{user.editAction}">

        <f:param name="action"value="delete" />

</h:commandLink>

Backing bean…

@ManagedBean(name="user")

@SessionScoped

publicclass UserBean{

 

        public String editAction() {

 

         Map<String,String> params =

               FacesContext.getExternalContext().getRequestParameterMap();

         String action = params.get("action");

          //...

 

        }

 

}

 

接收方法和URL传参一样,

以上对h:commandButton 标签无效,如果h:commandButton要传参可以间接通过js调用hidden的h:commandLink传参达到目的。如果要直接传过去,可以参考f:atribute

 

 3. f:atribute

用f:atribute标签,后台通过action listener获取.

JSF page…

<h:commandButtonaction="#{user.editAction}"actionListener="#{user.attrListener}">

        <f:attribute name="action"value="delete" />

</h:commandButton>

Backing bean…

@ManagedBean(name="user")

@SessionScoped

publicclass 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标签传参数,它会直接把参数值设置到backing bean对应的属性。

JSF page…

<h:commandButtonaction="#{user.editAction}" >

    <f:setPropertyActionListenertarget="#{user.action}" value="delete" />

</h:commandButton>

Backing bean…

@ManagedBean(name="user")

@SessionScoped

publicclass UserBean{

 

        public String action;

 

        public void setAction(String action) {

               this.action = action;

        }

 

        public String editAction() {

          //now action property contains "delete"

        }      

 

}

 

CargoSmart Sample: cs_nvo_asso_list.jsp

 

 

5.用隐藏域的方法
<h:inputHidden id="parameterName" value="#{backingBean.parameterName}" />
把它放到form标签中,然后就把他当作backingbean的一个属性通过js赋值后传回给后台,

不过在backingbean中要有此属性才可以。

6.在config.xml文件中

参数传递

<navigation-rule>
    <from-view-id>/view.jsp</from-view-id>
    <navigation-case>
     <from-outcome>edit</from-outcome>
     <to-view-id>/view.jsp?objectId=#{view.objectId}</to-view-id>
      <redirect />
    </navigation-case>
  </navigation-rule>

参数接收

<property-name>invoiceId</property-name>

      <value>#{param.invoiceId}</value>

  </managed-property>

类似URL接收参数,但invoiceId 必须是backingbean的一个属性,

 

7.Method expression

JSF 2.0后的版本允许你通过方法表达式(Method expression)的方式来传参数,像这样{bean.method(param)}.

JSF page…

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

Backing bean…

@ManagedBean(name="user")

@SessionScoped

publicclass UserBean{

 

        public String editAction(String id) {

         //id = "delete"

        }

 

}

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值