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){
}
- is there any way of getting the value changed input field's new value inside the "testListener" method?
- else is this need to be fulfilled using javascirpt?
any feedback related to this is highly appreciated.
Two ways:
Just get it straight from the parent
<h:inputText>
component which you in turn can obtain fromAjaxBehaviorEvent#getComponent()
.UIInput input = (UIInput) event.getComponent(); String contentId = input.getId().substring("inputfield".length()); Object contentValue = (Content) input.getValue(); // ...
Obtain the whole
Content
object as current<ui:repeat var>
value from the request attribute map which you in turn can obtain fromExternalContext#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.
精彩评论