java JSF/RichFaces:条件文本样式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2223539/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 20:08:26  来源:igfitidea点击:

JSF/RichFaces: conditional text styling

javajsfrichfaces

提问by volvox

I have a string which can be yes or no, instantiated in an object in a Java backing bean. I can't seem to find the best way to conditionally style the text red or green dependent on whether the JSF gets yes or no from the bean respectively. I'm using richfaces, but should I be using <c:if>tags?

我有一个字符串,它可以是是或否,在 Java 支持 bean 的对象中实例化。我似乎无法找到根据 JSF 分别从 bean 获得 yes 还是 no 来有条件地设置红色或绿色文本样式的最佳方法。我使用的是 Richfaces,但我应该使用<c:if>标签吗?

回答by Bozho

(in order of preference):

(按优先顺序):

  • style="color: #{yourVar == 'yes' ? 'green' : 'red'};"
  • make two <h:outputText>components with different styles, each with a different renderedattribute (one #{yourVar == 'yes'}and the other #{yourVar == 'no'})
  • define a (jstl/facelets/jsf 2.0) function that takes the var as argument and returns a style/class - styleClass="#{my:getStyleClass(yourVar)}"
  • style="color: #{yourVar == 'yes' ? 'green' : 'red'};"
  • 制作<h:outputText>具有不同样式的两个组件,每个组件具有不同的rendered属性(一个#{yourVar == 'yes'}和另一个#{yourVar == 'no'}
  • 定义一个 (jstl/facelets/jsf 2.0) 函数,它将 var 作为参数并返回一个样式/类 - styleClass="#{my:getStyleClass(yourVar)}"