开发者

JSf2 sending ajax requests to the server of individual input elements

开发者 https://www.devze.com 2023-03-19 00:37 出处:网络
I have a dynamic set of input items that I create on the page load. I need to trigger an ajax request when each of these text boxes change its value. I need to get the changed value and the changed it

I have a dynamic set of input items that I create on the page load. I need to trigger an ajax request when each of these text boxes change its value. I need to get the changed value and the changed item's id.

<h:form> <ui:repeat value="#{aBean.inputItems}" var="content"> 
<h:inputText id="inputfield#{content.id}" value="#{content.value}" label="lbl" >
         <f:ajax execute="@form" event="valueChang开发者_如何学编程e" listener="#{aBean.testListener}" render="@this"/>
</h:inputText> 
</ui:repeat> 
</h:form>

my backing bean(aBean) has a method.

public void testListener(AjaxBehaviorEvent event){

}
  1. is there any way of getting the value changed input field's new value inside the "testListener" method?
  2. else is this need to be fulfilled using javascirpt?

any feedback related to this is highly appreciated.


Two ways:

  1. Just get it straight from the parent <h:inputText> component which you in turn can obtain from AjaxBehaviorEvent#getComponent().

    UIInput input = (UIInput) event.getComponent();
    String contentId = input.getId().substring("inputfield".length());
    Object contentValue = (Content) input.getValue();
    // ...
    
  2. Obtain the whole Content object as current <ui:repeat var> value from the request attribute map which you in turn can obtain from ExternalContext#getRequestMap().

    Map<String, Object> requestMap = FacesContext.getCurrentInstance().getExternalContext().getRequestMap();
    Content content = (Content) requestMap.get("content");
    // ...
    


1) Yes, you change the value inside the listener. It would be just:

this.property = newValue;

2) If you are doing it on the server, then no JavaScript is involved.

0

精彩评论

暂无评论...
验证码 换一张
取 消