I want to dynamically create controls in my bean. I am using JSF 2.0
HtmlOutputTag objHtmlOutputTag = new HtmlOutputTag();
Now which property of HtmlOutputTag
should I set to set the content of HtmlOutputTag
?
I want to dynamically create controls in my bean. I am using JSF 2.0
HtmlOutputTag objHtmlOutputTag = new HtmlOutputTag();
Now which property of HtmlOutputTag
should I set to set the content of HtmlOutputTag
?
The
HtmlOutputTag
represents a tag, not a component. Rather useHtmlOutputText
. Then, you can just set thevalue
property, exactly as you would do in a real component in the JSF page. If you need it to be aValueExpression
rather than a rawvalue
, then you need to create it usingExpressionFactory#createValueExpression()
. Here's a kickoff example:where the convenience method
createValueExpression()
here look like: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.The final result in the JSF page should then look like this:
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:
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:And you define a getter/setter for
myProperty
in your backing bean. If you really want to do it programmatically (which I discourage unless you have strong arguments), here is an example with a dynamic table.