【问题标题】:How to get <f:attribute> value inside <p:ajax listener> method?如何在 <p:ajax listener> 方法中获取 <f:attribute> 值?
【发布时间】:2015-09-11 16:24:20
【问题描述】:

我有一个带有&lt;f:attribute&gt; 和&lt;p:ajax listener&gt; 的复选框组件。

<h:selectManyCheckbox ...>
  <p:ajax listener="#{locationHandler.setChangedSOI}" />
  <f:attribute name="Dummy" value="test" />
  ...
</h:selectManyCheckbox>

我尝试在如下监听器方法中获取test 的&lt;f:attribute&gt; 值:

public void setChangedSOI() throws Exception {
    FacesContext context = FacesContext.getCurrentInstance();
    Map<String, String> map = context.getExternalContext().getRequestParameterMap();
    String r1 = map.get("Dummy");
    System.out.println(r1);
}

但是,它打印了null。我怎样才能得到它?

【问题讨论】:

    标签: jsf attributes components


    【解决方案1】:

    组件属性不作为 HTTP 请求参数传递。组件属性设置为..呃,组件属性。 IE。它们存储在UIComponent#getAttributes()。你可以通过那张地图抓住它们。

    现在正确的问题显然是如何在 ajax 侦听器方法中获得所需的UIComponent。有两种方法:

    1. 指定AjaxBehaviorEvent 参数。它为此目的提供了一个getComponent() 方法。

      public void setChangedSOI(AjaxBehaviorEvent event) {
          UIComponent component = event.getComponent();
          String dummy = component.getAttributes().get("Dummy");
          // ...
      }
      
    2. 使用UIComponent#getCurrentComponent() 辅助方法。

      public void setChangedSOI() {
          UIComponent component = UIComponent.getCurrentComponent(FacesContext.getCurrentInstance());
          String dummy = component.getAttributes().get("Dummy");
          // ...
      }
      

    【讨论】:

    • 嗨 BalusC,在使用您的第一个选项时,我收到以下错误,2015 年 6 月 25 日下午 1:26:09 org.apache.catalina.core.StandardWrapperValve 调用严重:Servlet.service() for servlet Spring MVC Dispatcher Servlet 抛出异常 javax.el.MethodNotFoundException: /WEB-INF/certificates/locationList.xhtml @162,118 listener="#{locationHandler.setChangedSOI}": Method not found: com.csc.exceed.certificate.web。 LocationHandler@1ecd6fb.setChangedSOI() at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:109)
    • 有什么东西我们不能使用 primefaces 调用以 AjaxBehaviourEvent 作为参数的方法吗?
    • Spring MVC 调度程序 servlet 在 JSF 请求中到底在做什么?你了解/知道你在做什么吗? stackoverflow.com/questions/18744910/…
    • 我以前做过这样的事情,也使用 代替 。它在那里工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多