[英]lookup for a known key-value pair in jsf
I have an integer field in the bean I use in my JSF application. 我在JSF应用程序中使用的bean中有一个整数字段。 The integer field is showing the status of the process and it can be 0, 1 or 2. What I'd like to do is either automatically map this value to the corresponding String representation (0- not processed yet, 1- being processed ...etc) or do this in a hard-coded way using jsf.
整数字段显示进程的状态,它可以是0、1或2。我想做的是将这个值自动映射到相应的String表示形式(0-尚未处理,1-正在处理。 (..etc)或使用jsf以硬编码方式执行此操作。 I don't prefer to handle it in another way because the main jsf bean I use contains several hibernate models and it'll get complicated if I opt another way.
我不喜欢以其他方式处理它,因为我使用的主要jsf bean包含几个休眠模型,如果我选择其他方式会变得复杂。 Thanks for the help!
谢谢您的帮助!
Several ways. 几种方法。
Use rendered
attribute. 使用
rendered
属性。
<h:outputText value="Not processed" rendered="#{bean.status == 0}" /> <h:outputText value="Being processed" rendered="#{bean.status == 1}" /> <h:outputText value="Finished processing" rendered="#{bean.status == 2}" />
Use conditional operator ?:
in EL. 在EL中使用条件运算符
?:
。
<h:outputText value="#{bean.status == 0 ? 'Not Processed' : bean.status == 1 ? 'Being processed' : 'Finished processing'}" />
Use an application-wide Map<Integer, String>
somewhere. 在某个地方使用应用程序范围的
Map<Integer, String>
。
public class Bean { private static Map<Integer, String> statuses = new HashMap<Integer, String>(); static { statuses.put(0, "Not processed"); statuses.put(1, "Being processed"); statuses.put(2, "Finished processing"); } // Add getter. }
with 与
<h:outputText value="#{bean.statuses[bean.status]}" />
which does basically bean.getStatuses().get(bean.getStatus())
. 这基本上是
bean.getStatuses().get(bean.getStatus())
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.