JSF: how to update required field in ajax request

Ok, here you are the main problem.

Page. I have two required "text input". A command button that changes the value of a bean and resets the job object.

<a4j:form id="pervForm"> SURNAME:<h:inputText id="surname" label="Surname" value="#{prevManager.surname}" required="true" /> <br/> JOB:<h:inputText value="#{prevManager.job}" id="job" maxlength="10" size="10" label="#{msg.common_label_job}" required="true" /> <br/> <a4j:commandButton value="Set job to Programmer" ajaxSingle="true" reRender="job"> <a4j:actionparam name="jVal" value="Programmer" assignTo="#{prevManager.job}"/> </a4j:commandButton> <h:commandButton id="save" value="save" action="save" class="HATSBUTTON"/> </a4j:form> 

Here is a simple manager:

 public class PrevManager { private String surname; private String job; public String getSurname() { return surname; } public void setSurname(String surname) { this.surname = surname; } public String getJob() { return job; } public void setJob(String job) { this.job = job; } public String save() { //do something } } 

Do it:

Write something in the text of the assignment (for example, "teacher"). Leave an empty surname. Save. A verification error appears (last name is required). Click "Set task to programmer": nothing happens.

Checking the value of bean, I found that it was updated correctly, indeed, the component on the page does not update!

Ok, according to JBoss Docs, I found:

Ajax realm is a key component of ajax. It limits the part of the tree component that will be processed on the server when an ajax request arrives. Processing means a call during decryption, verification and updating of the model phase. The most common reasons for using an area:

- elimination of interruption of processing of the JSF life cycle during confirmation of input of another form is not required for a given ajax request; -definition of various strategies when events will be delivered (Immediate = true / false) -definition of an individual ajax status indicator - increase in rendering processing performance (SelfRendered = true / false, renderRegionOnly = "true / false")

The following two examples show situations where error validation does not allow ajax input to be processed. Enter your name. The output text of the component should appear after you. However, in the first case, this activity will be interrupted due to another field with required = "true". You will only see an error message while the "Job" field is empty.

Here is an example:

 <ui:composition xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core" xmlns:a4j="http://richfaces.org/a4j" xmlns:rich="http://richfaces.org/rich"> <style> .outergridvalidationcolumn { padding: 0px 30px 10px 0px; } </style> <a4j:outputPanel ajaxRendered="true"> <h:messages style="color:red" /> </a4j:outputPanel> <h:panelGrid columns="2" columnClasses="outergridvalidationcolumn"> <h:form id="form1"> <h:panelGrid columns="2"> <h:outputText value="Name" /> <h:inputText value="#{userBean.name}"> <a4j:support event="onkeyup" reRender="outname" /> </h:inputText> <h:outputText value="Job" /> <h:inputText required="true" id="job2" value="#{userBean.job}" /> </h:panelGrid> </h:form> <h:form id="form2"> <h:panelGrid columns="2"> <h:outputText value="Name" /> <a4j:region> <h:inputText value="#{userBean.name}"> <a4j:support event="onkeyup" reRender="outname" /> </h:inputText> </a4j:region> <h:outputText value="Job" /> <h:inputText required="true" id="job1" value="#{userBean.job}" /> </h:panelGrid> </h:form> </h:panelGrid> <h:outputText id="outname" style="font-weight:bold" value="Typed Name: #{userBean.name}" /> <br /> </ui:composition> 

Form 1: The behavior is incorrect. I need to complete the task, and then the name. Form 2: The behavior is correct. I do not need to complete the task to see the correct value.

Unfortunately, using the Ajax scope does not help (indeed, I used it poorly) ... because my fields are both REQUIRED. This is the main thing else.

Any idea?

Thank you very much.

+4
source share
3 answers

This issue is also identified in JSF 2 and is explained in detail in this answer: How to populate a text field with PrimeFaces AJAX after validation errors?

To summarize, the answer is that you need to clear the state of the EditableValueHolder component when it is presented as ajax-rendered but which is not included in ajax-execute by calling the setValue(null) , setSubmittedValue(null) , setLocalValueSet(false) , setValid(true) . Note: in JSF 2, you would use the convenience method resetValue() .

Considering that the input field "task" has the client identifier prevForm:job , here you can clear it inside the action listener method associated with the "Set task to programmer" button:

 UIInput input = (UIInput) context.getViewRoot().findComponent("prevForm:job"); input.setValue(null); input.setSubmittedValue(null); input.setLocalValueSet(false); input.setValid(true); 
+3
source

Using a4j: actionparam will set the value, but in this case it will not help you to bypass the check. Validation is performed on the component (and not on the bean property). So, when you submit, the value of the component remains empty. Hope this helps.

0
source

When you click "Set the task to the programmer", nothing happens because you are not reviewing your message component. If you do, you will see a confirmation message.

You need to rewrite the message because its <a4j:commandButton/> , and not just <h:commandButton/>

Try using a support. immediate in the "Ask the programmer task" field and check if it updates the field.

 <a4j:commandButton value="Set job to Programmer" ajaxSingle="true" reRender="job" immediate="true"> <a4j:actionparam name="jVal" value="Programmer" assignTo="#{prevManager.job}"/> </a4j:commandButton> 

* I don’t think "ajaxSingle" is needed here.

Perhaps you may get a new problem. Using immediate , the bean value will not be updated (MAYBE!). If this happens, create a bean method that manually sets the bean value.

Otherwise, if the bean value has been updated and the field is still empty, try putting the "job" field in <a4j:outputPanel/> and restart the output panel.

 <a4j:outputPanel id="myPanel"> <h:inputText value="#{prevManager.job}" id="job" maxlength="10" size="10" label="#{msg.common_label_job}" required="true" /> </a4j:outputPanel/> <a4j:commandButton value="Set job to Programmer" reRender="myPanel" immediate="true"> <a4j:actionparam name="jVal" value="Programmer" assignTo="#{prevManager.job}"/> </a4j:commandButton> 

Read more about <a4j:outputPanel/> here .

0
source

Source: https://habr.com/ru/post/1335104/


All Articles