ajax - JSF : How to refresh required field in ajax request

标签 ajax jsf richfaces

好的,这里是您的核心问题。

这一页。我有两个必需的“输入文本”。
更改 bean 值并重新呈现“作业”对象的命令按钮。

<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>  

这里是简单的经理:
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  
    }  

}  

我们开工吧:

在作业输入文本上写一些东西(例如“老师”)。
将姓氏留空。
节省。
出现验证错误(姓氏为必填项)。
按“将作业设置为程序员”:没有任何 react 。

查看bean值,发现是正确更新的,确实是页面上的组件没有更新!

好吧,根据我发现的 JBoss Docs:

Ajax region is a key ajax component. It limits the part of the component tree to be processed on the server side when ajax request comes. Processing means invocation during Decode, Validation and Model Update phase. Most common reasons to use a region are:

-avoiding the aborting of the JSF lifecycle processing during the validation of other form input unnecessary for given ajax request; -defining the different strategies when events will be delivered (immediate="true/false") -showing an individual indicator of an ajax status -increasing the performance of the rendering processing (selfRendered="true/false", renderRegionOnly="true/false")

The following two examples show the situation when a validation error does not allow to process an ajax input. Type the name. The outputText component should reappear after you. However, in the first case, this activity will be aborted because of the other field with required="true". You will see only the error message while the "Job" field is empty.



这是示例:
<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>  

Form1:行为不正确。我需要填写工作然后填写姓名。
Form2:行为正确。我不需要填写工作来查看正确的值。

不幸的是,使用 Ajax 区域无济于事(实际上我以一种糟糕的方式使用它......),因为我的字段都是必需的。这是主要的不同。

任何的想法?

非常感谢。

最佳答案

这个问题也在 JSF 2 中被发现,并在这个相关的答案中详细解释:How can I populate a text field using PrimeFaces AJAX after validation errors occur?

总结一下,答案就是需要清除 EditableValueHolder 的状态通过调用方法 setValue(null) ,在即将被 ajax 渲染但未包含在 ajax 执行中的组件, setSubmittedValue(null) , setLocalValueSet(false) , setValid(true) .注意:在 JSF 2 中你会使用方便的方法 resetValue() 反而。

鉴于您的“工作”输入字段具有客户端 ID prevForm:job ,这是在与“将作业设置为程序员”按钮相关联的 Action 监听器方法中清除它的方法:

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

关于ajax - JSF : How to refresh required field in ajax request,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4658853/

相关文章:

javascript - Breeze 使用 Angular $http 拦截器

javascript - AJAX 未捕获语法错误 : Unexpected Identifier

ajax - 第二次提交时,TinyMCE 在 bean 中的值(使用 jsf.ajax.addOnEvent)

ajax - 如何ajax更新PrimeFaces数据表页脚中的项目?

java - Jsf 属性未保存

java - 如何自定义<h :messages/> style in JSF 2?

javascript - 替代 AJAX 异步 : false option for form submit button click event handler

ajax - 使用g:formRemote和Grails获取responseText

java - 如何从浏览器 URL 获取正在运行的脚本页面

css - 丰富的面孔 : Is it possible to set the look of output text like input type password?