2015-06-25 41 views
0

我有一個複選框組件<f:attribute><p:ajax listener>如何獲得<f:attribute>值<p:ajax listener>方法?

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

我試圖讓如下聽者方法內的test<f:attribute>值:

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。我怎麼才能得到它?

回答

2

組件屬性不作爲HTTP請求參數傳遞。組件屬性設置爲.. uh,組件屬性。即它們存儲在UIComponent#getAttributes()中。您可以通過該地圖抓住他們。

現在正確的問題顯然是如何在ajax監聽器方法中獲得所需的UIComponent。有2種方式:

  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"); 
        // ... 
    } 
    
+0

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}」:找不到方法:com.csc.exceed.certificate .web.LocationHandler @ 1ecd6fb.setChangedSOI() \t at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:109) – pkn1230

+0

是否有某些我們無法用Aja調用方法的問題xBehaviourEvent作爲使用primefaces的參數? – pkn1230

+0

什麼是Spring MVC調度程序servlet在JSF請求中執行的操作?你瞭解/知道你在做什麼? http://stackoverflow.com/questions/18744910/using-jsf-as-view-technology-of-spring-mvc/ – BalusC

相關問題