【问题标题】:jsf: Passing arguments to method in backing beanjsf:将参数传递给支持 bean 中的方法
【发布时间】:2014-06-19 21:09:52
【问题描述】:

我需要将 #{bean.userProfile} 之类的参数传递给支持 bean 中的 clear(UserProfile profile) 之类的方法。

<a4j:commandButton  value="Cancel" onclick="#{rich:component('id_up')}.hide()"  execute="@this" type="reset" action="#{profilesBean.clear()}" render="id_panel">

我正在寻找在动作中编写类似这样的语法:

 action="#{profilesBean.clear(#profilesBean.selectedProfile)}"

我需要通过“selectedProfile”发送所有 userProfile 属性。

【问题讨论】:

  • action="#{profilesBean.clear(profilesBean.selectedProfile)}" 实际上应该“正常工作”,假设它是 JSF 2 或更高版本。但是如果它是同一个 bean (profilesBean) 那你为什么需要把它作为参数发送呢?

标签: java jsf richfaces


【解决方案1】:

如果您使用 EL2.2,那么您可以使用与预期非常相似的语法(第二个 # 应该被删除):

action="#{profilesBean.clear(profilesBean.selectedProfile)}"

【讨论】:

    【解决方案2】:

    从jsf页面传参到backingBean有几种方式,我一般用这种方式:

    <h:commandButton action="#{profilesBean.clear}" >
        <f:setPropertyActionListener target="#{profilesBean.selectedProfile}" value="#{profilesBean.userProfile}" />
    </h:commandButton>
    

    您可以在mkyong site中找到其他方式

    【讨论】:

      【解决方案3】:

      我更喜欢

      <h:commandButton action="#{profilesBean.clear}">
          <f:param name="selectedProfile" value="selectedProfile" />
      </h:commandButton>
      

      和对应的豆子

       public String clear() {
      
        Map<String,String> params = 
                  FacesContext.getExternalContext().getRequestParameterMap();
        String action = params.get("selectedProfile");
            //...
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-06-14
        • 1970-01-01
        • 1970-01-01
        • 2011-06-09
        • 2012-12-19
        • 2013-06-29
        • 1970-01-01
        • 2011-07-24
        相关资源
        最近更新 更多