如何在JSF中设置HtmlOutputTag的值? [英] How do I set the value of HtmlOutputTag in JSF?

查看:160
本文介绍了如何在JSF中设置HtmlOutputTag的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的bean中动态创建控件。我正在使用JSF 2.0

I want to dynamically create controls in my bean. I am using JSF 2.0

HtmlOutputTag objHtmlOutputTag = new HtmlOutputTag();

我应该设置 HtmlOutputTag 的哪个属性设置 HtmlOutputTag的内容

Now which property of HtmlOutputTag should I set to set the content of HtmlOutputTag?

推荐答案

HtmlOutputTag 表示标签,而不是组件。而是使用 HtmlOutputText 。然后,您可以设置属性,就像在JSF页面中的实际组件中一样。如果您需要它是 ValueExpression 而不是原始,那么您需要使用 ExpressionFactory #createValueExpression() 。这是一个启动示例:

The HtmlOutputTag represents a tag, not a component. Rather use HtmlOutputText. Then, you can just set the value property, exactly as you would do in a real component in the JSF page. If you need it to be a ValueExpression rather than a raw value, then you need to create it using ExpressionFactory#createValueExpression(). Here's a kickoff example:

HtmlOutputText text = new HtmlOutputText();
text.setValueExpression("value", createValueExpression("#{bean.property}", String.class));

其中方便方法 createValueExpression()这里看起来像:

where the convenience method createValueExpression() here look like:

private static ValueExpression createValueExpression(String valueExpression, Class<?> valueType) {
    FacesContext context = FacesContext.getCurrentInstance();
    return context.getApplication().getExpressionFactory()
        .createValueExpression(context.getELContext(), valueExpression, valueType);
}

在一些实用程序类中远远隐藏它,这样你就不需要一遍又一遍地重复所有代码;) valueType 参数显然应该代表该属性的实际类型。

hide it far away in some utility class so that you don't need to repeat all that code again and again ;) The valueType argument obviously should represent the actual type of the property.

JSF页面中的最终结果应如下所示:

The final result in the JSF page should then look like this:

<h:outputText value="#{bean.property}" />

这就是说,根据功能要求,可能确实有更好更清洁的方法来解决问题需求。如果你愿意,你可以详细说明一下,以便我们可以在必要时提出更好的方法。

That said, depending on the functional requirement, there may indeed be better and cleaner ways to solve the functional requirement. If you want, you can elaborate a bit more about it so that we can if necessary suggest better ways.

这篇关于如何在JSF中设置HtmlOutputTag的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆