2011-09-23 80 views
3

我已经创建了一个自定义组件。我为它添加了一个动态输入文本框(来自编码函数)。如何将值自定义组件传递给托管Bean?

组件正确呈现为HTML。

但我想将文本框的值绑定到托管Bean上的某个属性。所以其他开发人员可以在他的jsp上使用他的托管bean。

我想知道,我该怎么做,以便在文本框(我的组件动态创建的)中输入的值设置为某个Managed bean属性。

回答

2

那么问题就解决了。

在encodeEnd()方法我添加的元素作为

HtmlInputHidden hidden = new HtmlInputHidden(); 
hidden.setParent(this); 
hidden.setId("someId"); 
ValueExpression ve = getValueExpression("value"); 
hidden.setValueExpression("value", ve); 
hidden.encodeBegin(context); 
hidden.encodeEnd(context); 

这似乎有一些问题。 。

然后,我改变这...

HtmlInputHidden hidden = new HtmlInputHidden(); 
hidden.setId("someId"); 
ValueExpression ve = getValueExpression("value"); 
hidden.setValueExpression("value", ve); 
this.getChildren().add(hidden); 
hidden.encodeBegin(context); 
hidden.encodeEnd(context); 

使用this.getChildren()的添加();解决了我的问题

P.S.显然在添加元素之前,如果元素已经存在,需要检查它。

3

您需要确保您的自定义组件类扩展为UIInput,并且您在渲染器的encodeEnd()方法中将组件的客户端ID编写为HTML输入元素的name属性。然后,您可以在渲染器的覆盖方法decode()中从请求参数映射中获取提交的值,并使用组件的客户端ID作为参数名称,并将其设置为UIInput#setSubmittedValue(),然后让JSF完成转换,验证和更新作业的剩余部分模型值。

@Override 
public void decode(FacesContext context, UIComponent component) { 
    // Do if necessary first validation on disabled="true" or readonly="true", if any. 

    // Then just get the submitted value by client ID as name. 
    String clientId = component.getClientId(context); 
    String submittedValue = context.getExternalContext().getRequestParameterMap().get(clientId); 
    ((UIInput) component).setSubmittedValue(submittedValue); 
} 

无关的具体问题,你是否知道在JSP的继任Facelets的新的复合材料部件的支持?我的印象是,你不一定需要用于此目的的自定义组件。或者,尽管您已经使用JSF 2.x,您是否真的被限制使用旧版JSP作为视图技术?另请参见When to use <ui:include>, tag files, composite components and/or custom components?

+0

非常感谢,但我写了上面的代码。我甚至检查了上面代码的'submittedValue'变量,它是用户输入的。但仍然没有到达Managed Bean。你能否帮我用编码方法中的代码来绑定动态控件和一些Managed bean属性?用户将自定义组件的某些“selectedValue”属性值提供为selectedValue =“#{TestBean.testProperty}”。现在,用户在动态文本框中输入的值应该设置为'testPoperty'。 – Apurv

相关问题