动态创建输入文本 [英] Create inputtext dynamically
问题描述
我有下一个问题!
我想从我的支持bean中动态创建输入文本,它们将位于动态创建的选项卡中,该选项卡将在执行时构建.
I want to create inputtext dynamically from my backing bean, they would be inside of the a tabs also dynamic created which will be constructed in execution time.
我设法使用相应的input
类动态添加组件.
I manage to add the components dynamically using the input
classes corresponding.
但是我还没有设法将value标签添加到组件中,这是一种将值绑定到managedBean
本身的valueExpresion
语言.
But I have not manage to add the value tag to the component, a valueExpresion
language which binds the value to the managedBean
itself.
我找到了一些可以像这样总结的代码.
I have found some code which I can summerize like this.
@ManagedBean
@ViewScoped
public MyManagedBean(){
private TabView tabsi;
HtmlOutputLabel hol = new HtmlOutputLabel();
InputText txt2 = new InputText();
private String value;
/* getter and setters */
public void MyManagedBean{
tabsi = new TabView();
Tab tab1 = new Tab();
tab1.setTitle("Tab1");
Tab tab2 = new Tab();
tab2.setTitle("Tab2");
tabsi.getChildren().add(tab1);
tabsi.getChildren().add(tab2);
hol.setValue("label");
hol.setStyleClass("label");
txt2.setValueExpression("value",
TestController.getExpression("#{myManagedBean.value}"));
txt2.setValue(value);
tab1.getChildren().add(hol);
tab1.getChildren().add(txt2);
}
public static ValueExpression getExpression(String expression) {
FacesContext fc = FacesContext.getCurrentInstance();
ELContext ctx = fc.getELContext();
ExpressionFactory factory = fc.getApplication().getExpressionFactory();
return factory.createValueExpression(ctx, expression, Object.class);
}
public void test1() {
System.out.println(value);
}
}
我成功地管理了组件,但是无法将其绑定以设置ValueExpression
.当我从按钮调用test1函数时,它会打印null
I successfully manage to build the components but I can not bind it to set the ValueExpression
. When I call the test1 function from a button it print null
如何将值绑定到ManagedBean
???
How can I bind the value to the ManagedBean
???
推荐答案
到目前为止,我无法使用提供的信息来查明确切原因,但是这种方法至少存在三个严重问题:
I can't pinpoint the exact cause with the information provided so far, but there are at least three severe problems with this approach:
-
UIComponent
实例本质上是请求范围的.在任何情况下,都永远不要将其声明为Bean的属性,否则当在多个视图中引用同一实例时,将面临臭名昭著的重复组件ID"错误.
An
UIComponent
instance is in essence request scoped. You should never ever declare it as a property of a bean in a broader scope, or you will face infamous "Duplicate component ID" errors when the very same instance is been referenced in multiple views.
使用binding
属性引用视图作用域的bean属性会完全破坏视图作用域.每次请求都会重新创建该bean.此问题与此处详细解释的原因基本上相同: JSF2 Facelets中的JSTL ...有道理吗?
Using binding
attribute referring a view scoped bean property breaks the view scope completely. The bean is recreated every request. This problem has essentially the same grounds as explained in detail here: JSTL in JSF2 Facelets... makes sense?
以编程方式创建的UIInput
和UICommand
组件必须通过setId()
设置固定的id
,否则在应用请求值阶段,JSF将无法在请求参数图中找到它提交表单,并且固有地无法分别处理提交的值和操作方法.
A programmatically created UIInput
and UICommand
component must have a fixed id
set via setId()
, otherwise JSF won't be able to find it in the request parameter map during apply request values phase of the form submit and inherently not be able to process the submitted value and the action method respectively.
第三个问题很可能是您当前问题的确切原因,但是第一个和第二个问题可能有一定影响.
The third problem is most likely the exact cause of your current problem, but the first and the second problem may have had some influence.
无论如何,请尝试重新考虑以这种方式以编程方式创建组件的决定.应尽可能避免这种情况.例如.为什么不只使用rendered
属性,还是查看诸如JSTL <c:if>
之类的构建时间标签?
Regardless of this, try to reconsider your decision to programmatically create components this way. This should be avoided as much as possible. E.g. why don't you just use rendered
attribute, or view build time tags like JSTL <c:if>
?
这篇关于动态创建输入文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!