lookup for a known key-value pair in jsf
I have an integer field in the bean I use in my JSF application. 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. 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 开发者_JAVA技巧if I opt another way. Thanks for the help!
Several ways.
Use
rendered
attribute.<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.<h:outputText value="#{bean.status == 0 ? 'Not Processed' : bean.status == 1 ? 'Being processed' : 'Finished processing'}" />
Use an application-wide
Map<Integer, String>
somewhere.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())
.
I would suggest you to go for i18n.
your property file should look like.
message_en.properties
process_in_progress=Process is under prgress
process_failed=Process failed to execute.
精彩评论