简体   繁体   English

如何在JSF中设置HtmlOutputTag的值?

[英]How do I set the value of HtmlOutputTag in JSF?

I want to dynamically create controls in my bean. 我想在我的bean中动态创建控件。 I am using JSF 2.0 我正在使用JSF 2.0

HtmlOutputTag objHtmlOutputTag = new HtmlOutputTag();

Now which property of HtmlOutputTag should I set to set the content of HtmlOutputTag ? 现在我应该设置HtmlOutputTag哪个属性来设置HtmlOutputTag的内容?

The HtmlOutputTag represents a tag, not a component. HtmlOutputTag表示标签,而不是组件。 Rather use HtmlOutputText . 而是使用HtmlOutputText Then, you can just set the value property, exactly as you would do in a real component in the JSF page. 然后,您可以像在JSF页面中的真实组件中那样完全设置value属性。 If you need it to be a ValueExpression rather than a raw value , then you need to create it using ExpressionFactory#createValueExpression() . 如果需要将其用作ValueExpression而不是原始value ,则需要使用ExpressionFactory#createValueExpression()创建它。 Here's a kickoff example: 这是一个启动示例:

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

where the convenience method createValueExpression() here look like: 这里的便捷方法createValueExpression()如下所示:

private static ValueExpression createValueExpression(String valueExpression, Class<?> valueType) {
    FacesContext context = FacesContext.getCurrentInstance();
    return context.getApplication().getExpressionFactory()
        .createValueExpression(context.getELContext(), valueExpression, 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. 将其隐藏在某个实用程序类中很远,这样就不必一次又一次地重复所有代码;) valueType参数显然应该表示属性的实际类型。

The final result in the JSF page should then look like this: 然后,JSF页面中的最终结果应如下所示:

<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. 如果需要,您可以对其进行详细说明,以便我们在必要时可以提出更好的方法。

As usual , my advice would be to not add/remove component dynamically. 往常一样 ,我的建议是不要动态添加/删除组件。 Solve your problem another way: 用另一种方法解决您的问题:

  • Toggle visibility of components 切换组件的可见性
  • Rebind the data belonging to a component 重新绑定属于组件的数据

Adding/removing component dynamically is always a source of trouble and chances are that you can do it another way much simpler. 动态添加/删除组件始终是麻烦的源头,而且您有可能以另一种简单得多的方式执行此操作。

The outputText component is easy to use: outputText组件易于使用:

<h:outputText value="#{BackingBean.myProperty}"/>

And you define a getter/setter for myProperty in your backing bean. 然后在后备bean中定义myProperty的getter / setter。 If you really want to do it programmatically (which I discourage unless you have strong arguments), here is an example with a dynamic table. 如果您真的想以编程方式执行此操作(除非您有强大的参数,否则我不建议这样做),下面是一个带有动态表的示例

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

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