Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
394 views
in Technique[技术] by (71.8m points)

java - How do I set the value of HtmlOutputTag in JSF?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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));

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);
}

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:

<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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...