简体   繁体   English

在jsf中查找已知的键值对

[英]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! 谢谢您的帮助!

I would suggest you to go for i18n. 我建议您去参加i18n。

your property file should look like. 您的属性文件应如下所示。

message_en.properties message_zh.properties

process_in_progress=Process is under prgress
process_failed=Process failed to execute.

Several ways. 几种方法。

  1. 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}" /> 
  2. Use conditional operator ?: in EL. 在EL中使用条件运算符?:

     <h:outputText value="#{bean.status == 0 ? 'Not Processed' : bean.status == 1 ? 'Being processed' : 'Finished processing'}" /> 
  3. 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.

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