Florian
Florian

Reputation: 1182

Trigger Spring Web Flow view transition with f:ajax on JSF input field

Is it possible to trigger a web flow transition by putting the <f:ajax> tag inside a <h:inputText> to send request with each keypress?

I already tried this and the ajax request is send by the browser, received by the application and processed by JSF but the event is not observed by Web Flow.

<!-- does not trigger transition -->
<h:inputText id="search" value="#{friendsForm.searchUsername}">
   <f:ajax listener="search" 
           execute="@this" 
           render="users" 
           event="keypress"/>
</h:inputText>

<!-- does trigger transition -->
<h:commandButton value="search" action="search">
   <f:ajax listener="search" execute="search" render="users"/>
</h:commandButton>

When doing the same with a commandButton, it works as expected. Looking at the source code one can see that the button's base class UICommand invokes its default ActionListener which in the end triggers the transition. It seems that inputText does not have an action listener and thus the event is not processed there.

In the end I could work around this by programmatically clicking a hidden button with each keypress, but if I can avoid this I would do so.

Thanks in advance...

Upvotes: 0

Views: 2110

Answers (1)

Florian
Florian

Reputation: 1182

I found this post about how to trigger a transition on value change in h:selectOneMenu and the solution works quite well, even for my use case.

I defined the listener attribute on the f:ajax to point to a spring bean's method which in turn gets the RequestControlContext in order to handle a new Event that triggers the transition.

RequestContext requestContext = RequestContextHolder.getRequestContext();
RequestControlContext rec = (RequestControlContext) requestContext;
rec.handleEvent(new Event(this, "search"));  

This way I don't have to introduce a hidden button and translate the keypress event into a click event.

Upvotes: 3

Related Questions