“重新渲染”后未设置组件属性根据 AJAX 请求
目前我正在开发一些复杂的 Web 前端并使用以下方法实现它:
- JSF 1.2
- Facelets 1.1.15
- RichFaces 3.3.3.Final
我创建了一个自定义 JSF 组件,它可以使用纯 JavaScript 验证 inputText 字段。该组件只有一个属性:类型。该属性负责验证算法,该算法将在用户按下键盘按键时应用。
在创建初始视图的 restoreView
阶段,此属性由 JSF(实际上由 Facelets)设置。这意味着我有一个带有属性“type”的 setter 和 getter 的组件类。并使用 xhtml 文档中指定的值调用“类型”设置器。
如果我在 reRender 属性中指定组件对象,则每次在 RestoreView 阶段都会重新创建组件对象。但是,当重新创建它时,我所需的属性类型未设置。 它只是创建新的组件对象......仅此而已。可能我不明白一些事情,这是正常行为,但是在这种情况下如何获取属性值?
代码:
简单测试页:
<?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>
组件类:
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;
}
当我按下“发出 AJAX 请求”按钮时,我的组件被重新创建。但组件中未设置属性“type”。
主要问题始于组件渲染器中的 renderView 阶段,当调用 encodeBegin 时,它会尝试获取此属性,当然它会获取 null 而不是正确的价值。
所以,更精确的问题可能是:
如何在renderView
阶段通过AJAX请求获取组件的属性值?
任何帮助将不胜感激。
Currently i'm working on some complex web front-end and implement it using:
- JSF 1.2
- Facelets 1.1.15
- RichFaces 3.3.3.Final
I have created a custom JSF component which enables validation of inputText fields using pure JavaScript. This component have only one attribute: 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). 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.
Component object is recreated each time at restoreView phase if i specify them in reRender attribute. 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. 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.
So, the more precise question probably:
How to get attribute values of component on AJAX request at renderView
phase?
Any help will be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须重写组件中的
saveState
和restoreState
才能保存和恢复所需的属性。祝你好运!
You must override
saveState
andrestoreState
in component to save and restore needed attributes.Good Luck!