开发者

lookup for a known key-value pair in jsf

开发者 https://www.devze.com 2023-01-27 05:26 出处:网络
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.

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.

  1. 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}" />
    
  2. Use conditional operator ?: in 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.

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

精彩评论

暂无评论...
验证码 换一张
取 消