Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reset inputText after Button Click with JSF

Tags:

jsf

jsf-2

Is it possible to reset the value of an inputText after clicking on the commandButton in JSF? The inputText UIElement provides the method ResetValue so I tried something like this:

<h:inputText  id="measurementadd" binding="#{inputTextMeasurement}">
    <f:validateRegex pattern="[a-zA-Z ]*"/>
    <f:ajax event="keyup" render="measurementaddmessage submit" execute="@this"/>
<h:inputText>
<p:commandButton id="submit" action="#{Bean.addMeasurement(inputTextMeasurement.value)}" 
value="submit" update="dataTable measurementadd measurementaddmessage" 
disabled="#{empty inputTextMeasurement.value or facesContext.validationFailed }" >
    <f:ajax event="mouseup" execute="#{inputTextMeasurement.resetValue()}" />
</p:commandButton>  
<h:messages for="measurementadd" id="measurementaddmessage"/>  

But after clicking the Button the inputTextMeasurement doesn't reset it's value.

Does someone know a good workaround for this?

I'm searching for a solution without JS and JAVA, so a realization in JSF would be very cool.

like image 886
Briefkasten Avatar asked Jun 10 '26 15:06

Briefkasten


1 Answers

Your mistake is here in the execute attribute:

<f:ajax event="mouseup" execute="#{inputTextMeasurement.resetValue()}" />

The execute attribute should represent a space separated collection of client IDs to include in the process/decode of the ajax request. However, you specified a listener method there.

You need the listener attribute instead:

<f:ajax listener="#{inputTextMeasurement.resetValue()}" />

(and I omitted event as it defaults here to click which is already the right one)

Interesting detail is that the other <f:ajax> in the same piece of code used the exeucte attribute the right way.


Unrelated to the concrete problem, have you looked at <p:resetInput>? This saves an ajax listener method in the bean. Replace the whole <f:ajax> with

<p:resetInput target="measurementadd" />
like image 165
BalusC Avatar answered Jun 15 '26 07:06

BalusC