简体   繁体   English

组件属性“重新解析”后,无法对AJAX请求

[英]Component attributes doesn't set after “reRender” on AJAX request

Currently i'm working on some complex web front-end and implement it using: 目前,我正在研究一些复杂的Web前端,并使用以下方法实现它:

  • JSF 1.2 JSF 1.2
  • Facelets 1.1.15 小面1.1.15
  • RichFaces 3.3.3.Final RichFaces 3.3.3最终版

I have created a custom JSF component which enables validation of inputText fields using pure JavaScript. 我创建了一个自定义JSF组件,该组件可以使用纯JavaScript验证inputText字段。 This component have only one attribute: type. 该组件只有一个属性:type。 This attribute is responsible for validation algorithm which will be applied at time when user presses a keyboard key. 此属性负责验证算法,该算法将在用户按下键盘键时应用。

At restoreView phase when initial view is created this attribute is set by JSF (actually by Facelets). 在创建初始视图的restoreView阶段,此属性由JSF(实际上是Facelets)设置。 This means that i have a component class with setter and getter for attribute 'type'. 这意味着我有一个带有属性“类型”的设置器和获取器的组件类。 And a 'type' setter called with value specified in xhtml document. 还有一个“类型”设置器,用xhtml文档中指定的值进行调用。

Component object is recreated each time at restoreView phase if i specify them in reRender attribute. 如果我在reRender属性中指定组件对象,则每次在restoreView阶段都会重新创建组件对象。 But when it is recreated my required attribute type is not set. 但是,当重新创建它时,我的必需属性类型未设置。 It's simply creates new component objects... and it's all. 它只是创建新的组件对象……而已。 May be i don't understand something and this is normal behavior, but how to get attribute values in this case? 可能是我不了解某些内容,这是正常现象,但是在这种情况下如何获取属性值?

Code: 码:

Simple test page: 简单测试页:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:u="http://java.sun.com/jsf/facelets"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:a="http://richfaces.org/a4j"
      xmlns:r="http://richfaces.org/rich"
      xmlns:v="http://nobodyhere.ru/jsf/validation">
    <head>
        <title>Test Page</title>
    </head>
    <body>
        <h:form id="testForm">
            <h:inputText id="textInput" value="test">
                <v:keyValidator type="time"/>
            </h:inputText>
            <a:commandButton value="Make AJAX request" reRender="testForm"/>
        </h:form>
    </body>
</html>

Component class: 组件类:

public class KeyValidator extends UIComponentBase
{

    public KeyValidator()
    {
        System.out.println("new KeyValidator");
    }

    public KeyValidatorType getValidatorType()
    {
        return type;
    }

    public String getType()
    {
        return getValidatorType().toString();
    }

    public void setType(String type)
    {
        this.type = KeyValidatorType.valueOf(type.toUpperCase());
    }

    @Override
    public String getFamily()
    {
        return KeyValidator.class.getName();
    }

    private KeyValidatorType type;
}

When i press "Make AJAX request" button my component is recreated. 当我按“发出AJAX请求”按钮时,将重新创建我的组件。 But attribute 'type' is not set in component. 但是属性“类型”未在组件中设置。

The main problem starts at renderView phase in component renderer when encodeBegin is called it tries to get this attribute and of course it gets null instead of correct value. 主要的问题在开始renderView在组件渲染阶段时encodeBegin把它叫做试图让这个属性,当然,它得到null的,而不是正确的值。

So, the more precise question probably: 因此,更精确的问题可能是:
How to get attribute values of component on AJAX request at renderView phase? 如何在renderView阶段获取AJAX请求上组件的属性值?

Any help will be greatly appreciated. 任何帮助将不胜感激。

You must override saveState and restoreState in component to save and restore needed attributes. 您必须在组件中重写saveStaterestoreState才能保存和恢复所需的属性。

Good Luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM